How to get Serial Number of Raspberry pi 3 using NodeJS
Posted By : Chandan Kumar Singh | 10-Jun-2016
In this blog we will find the serial number of raspberry pi 3 which is unique. We are going to execute the linux shell command using nodejs. As raspberry pi os which is raspbian is based on linux kernel so we can run the command supported by debian linux. This code is tested on raspberry pi 3 and node v5.11.1.
Create a file called raspserial.js and copy below code and paste it in the file.
const exec = require( 'child_process' ).exec; exec('cat /proc/cpuinfo | grep Serial',(error,stdout,stderr) => { if(error){ console.error( `exec error: ${error}` ); return; } console.log( `stdout: ${stdout}` );// this is your RPI serial number console.log( `stderr: ${stderr}` ); });
Now run the js file
node raspserial.js
Explanation of code:
first line of code incuding the module of child process, after including we are ruuning the command 'cat /proc/cpuinfo | grep Serial' , the callback function '(error,stdout,stderr)' will contain actual result.
next we are checking if any error has occured then console the error and exit the program, otherwise print the standard output which is the our required Serial number of Raspberry Pi.
Thanks
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Kumar Singh
Chandan is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate. He loves technologies like Blockchain and IoT.