NodeJS With Arduino
Connect arduino with nodejs and do whatever you want through JavaScript code.
Let’s start
Step 1: You need following things:
- Laptop/desktop ( Installed Nodejs, NPM, Arduino IDE )
- Arduino Kit ( Connector to connect arduino and laptop, Power will be providing through connector also)
- LED , Register, Wire
Step 2: Upload Firmata in your Arduino IDE.
- Open your arduino kit
- Goto File > Examples > Firmata > StandardFirmata
- Click on upload button on your arduino IDE. This library will upload to your arduino kit.
Step 3: Work on nodejs
- Open your laptop ( Assuming that you have already installed nodejs )
- Create a directory in any location
- Navigate to location of above directory and run below command-
npm init
( The npm init will create a file, package.json which will have all the information about this project )
Now enter the information accordinly, Saw below line for help:
“name”: “arduino_with_nodejs”,
“version”: “1.0.0”,
“description”: “This is the demo to connect nodejs to arduino”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“keywords”: [
“ShubhamArduino”
],
“author”: “Shubham Verma”,
“license”: “ISC”,
If you are getting any error then copy the below written code and replace your whole code which is written in your package.json.
{"name": "arduino_with_nodejs","version": "1.0.0","description": "This is the demo to connect nodejs to arduino","main": "index.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1"},"keywords": ["ShubhamArduino"],"author": "Shubham Verma","license": "ISC","links": {"johnny-five": "https://www.npmjs.com/package/johnny-five"},"dependencies": {"johnny-five": "^0.14.3"}}
Step 4: Install johnny-five module to interact with arduino:
Run command:
npm install johnny-five --save
The above command will create a folder, node_modules and keep all the required library inside this folder.
Step 5: Create a new file on same directory with name :
index.js
Step 6: Now copy and pastey below code into your newly created index.js file:
var five = require('johnny-five');
var board = new five.Board();board.on('ready', function () {
var led = new five.Led(8);
led.blink(1000);
});
Step 7: Connect your LED object to arduino like in images:
Step 8: Run your node app by using following command:
node index.js
Congratulations… Now your LED will be blinking after every 1 second.
Now you can do whatever to you JavaScript code with this arduino. If you want to stop the node app, press “CTRL+C” and start node app “node index.js”