This guide aims to provide some beginning information about IoT by using a stable version of Node.js. That sounds something like, "I've downloaded Node, now what?" This tutorial answers that question and explains how to get started from the very beginning for the IoT world.
I won’t tell you how to setup Node.js but i am going tell you how to use Node.js via IoT tools (I am using Arduino Uno 3).
There are some famous IoT libraries below:
- Johnny-five.io
- Zetta.js
- Cylon.js
- Nodered
- Device.js
Why should I use Node.js when I decide to make an IoT Project?
Sorry; I have to answer Why Node.js is good before the above question. There are many great reasons to use Node.js.
It is fast
Node.js is a JavaScript runtime that uses the V8 engine developed by Google for use in Chrome. V8 compiles and executes JavaScript at lightning speeds mainly due to the fact that V8 compiles JavaScript into native machine code. For example, a function to read a file may start reading a file and return the control to execution environment immediately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or waiting for File I/O. This makes Node.js highly scalable, as it can process a high number of requess without waiting for any function to return result. (Tutorials Point)
It has lots of plugins
A set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management by using npm.
It is good at handling I/O
HTTP requests and responses are common things in traditional web projects. Actually; they’re I/O streams. Node can read or write stream from http request by using websocket easly. I/O is safer and easier to do in parallel. Node.js lets you do it using event programming which is simple, elegant and easy to use. Use Node.js for I/O, and some better suited language with proper multithreading for CPU intensive work.
It is scalable
It is scalable due to load balancing. Essentially you can have multiple jobs for node to process and it can handle it with no significant burden. It makes Node.js scalable.
Please look at the below Picture to understand ecosystem of Node.js:
Let’s answer the above question; Why should i use Node.js when i decide to make an IoT Project?
- Controlling IoT from one point
There are lots of language options in IoT world. Micro C , Python etc. But if you want to control all IoT tools from one point you need a common language because Node.js is known for its speed, scalability and efficiency.
- Effective and secure communications
Node.js has lots of plugins to manage security issues because Node.js is getting more and more mature. Also Node.js has secured protocols.
- It has intelligence
IoT needs connectivity to each other and communication with other systems (SAP, Oracle, Azure,AWS etc.) this is really costly. Most IoT products do not have enough abilities for that. We need experiences between business systems and embedded devices. Node.js gives us this ability.
- It has many frameworks
IoT needs to control from remote connected systems. Node packaged modules(NPM) have lots of frameworks to achieve it. For example; it has 80 packages for only Arduino, According to Module counts Node has more than 30000 modules.
- I/O
Devices (sensors,beacons and wearables etc.) need stream of data. It means IoT needs parallelism via a safer and easier way. IoT tends to share no data between execution threads. Node.js lets you do it using event programming which is simple, elegant and easy to use.
- It needs low resources requirements
First of all; it has single process thread, It uses one single CPU core at time because load balancing is key Word of Node.js. it has V8 standarts.
Johnny-Five.io
Johnny-Five is the JavaScript Robotics & IoT Platform. Released by Bocoup in 2012, Johnny-Five is maintained by a community of passionate software developers and hardware engineers. Over 75 developers have made contributions towards building a robust, extensible and composable ecosystem.
Hello World Sample
Microcontrollers and SoC platforms like to say "Hello World" with a simple blinking LED; the following demonstrates how to do this with the Johnny-Five framework.
Code: (Blinking Led)
- var five = require("johnny-five");
- var board = new five.Board();
- board.on("ready", function()
- {
- var led = new five.Led(13);
- led.blink(500);
- });
Sample 2
Motion detection with Passive Infrared sensor. I am going to show you how to use PIR motion Look at the codes please;
Run this example from the command line with:
node eg/motion.js Code - var five = require("johnny-five");
- var board = new five.Board();
- board.on("ready", function()
- {
-
- var motion = new five.Motion(7);
-
- motion.on("calibrated", function()
- {
- console.log("calibrated");
- });
-
-
- motion.on("motionstart", function()
- {
- console.log("motionstart");
- });
-
-
- motion.on("motionend", function()
- {
- console.log("motionend");
- });
-
-
-
-
-
-
- });