Node.js In IoT Part-One

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:

  1. Johnny-five.io
  2. Zetta.js
  3. Cylon.js
  4. Nodered
  5. 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?

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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)

  1. var five = require("johnny-five");  
  2. var board = new five.Board();  
  3. board.on("ready", function()  
  4. {  
  5.     var led = new five.Led(13);  
  6.     led.blink(500);  
  7. });  
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
  1. var five = require("johnny-five");  
  2. var board = new five.Board();  
  3. board.on("ready", function()  
  4. {  
  5.     // Create a new `motion` hardware instance.  
  6.     var motion = new five.Motion(7);  
  7.     // "calibrated" occurs once, at the beginning of a session,  
  8.     motion.on("calibrated", function()  
  9.     {  
  10.         console.log("calibrated");  
  11.     });  
  12.     // "motionstart" events are fired when the "calibrated"  
  13.     // proximal area is disrupted, generally by some form of movement  
  14.     motion.on("motionstart", function()  
  15.     {  
  16.         console.log("motionstart");  
  17.     });  
  18.     // "motionend" events are fired following a "motionstart" event  
  19.     // when no movement has occurred in X ms  
  20.     motion.on("motionend", function()  
  21.     {  
  22.         console.log("motionend");  
  23.     });  
  24.     // "data" events are fired at the interval set in opts.freq  
  25.     // or every 25ms. Uncomment the following to see all  
  26.     // motion detection readings.  
  27.     // motion.on("data", function(data) {  
  28.     // console.log(data);  
  29.     // });  
  30. });