Node.js In IoT Part-Two

Controlling the physical world with software is becoming more popular day by day. Especially, during last two years, people have been combining new sets of projects with Web technologies. It means a new set of projects has emerged allowing the client side Web developers to use their skills to start controlling elements of the physical world.

The ecosystem of IoT includes computer devices, automated/ digital machines, objects, people or animals. They have interactions among them. For example; say a person wants to send data to his dog. Also, he wants to take some data from his dog. IoT starts shining here. Also, handling streams of a loT data needs a strong tool. The devices like sensors, beacons, transmitters, motors have been producing large amounts of data. Node.js is well equipped to handle this request through the streams.

In this article, I will show to you why you need to make real IOT applications with a web interface. The article has bridged the gap between the device events and Web Applications. By allowing the instructed device, the generated data will plot another great NoSQL DB tool for making statistical analysis. It calls RethinkDb. My previous article includes a simple introduction on how and why Node.js is for IoT. In this article, you will learn how to make real IoT Applications with  Node.js

I want to separate my article into  3 important points
  1.  What is RethinkDb?
  2.  Why do  I have to use johhny-five io?
  3.  Development!

What is RethinkDb?

RethinkDB is an open-source distributed JSON document database with a pleasant and powerful query language. It has great abilities. First of all, RethinkDB is the first open-source scalable database for the realtime Web. What is realtime Web? In no time the developer can tell the database to continuously push the updated query results to the Applications in realtime instead of polling for the changes. The developer can tell the database to continuously push the updated query results to the Applications in realtime. RethinkDB allows the developer to build scalable realtime apps in a fraction of the time with few engineering resources.

In this article, you will learn how to use these features to get started on your own realtime apps.

What are the advantages of RethinkDB?

Subscribe to the change notifications from the database queries.

No more polling, the database pushes changes to your app.

Reduce the amount of plumbing needed to stream the live updates.

Highly expressive query language.

Relational features like table joins.

Powerful admin UI with point and-click cluster management.

How to setup RethinkDB ?

First of all, you can visit https://www.rethinkdb.com/docs/install/ for installing. I am using Windows platform because of which, I am choosing Windows platform;

Running RethinkDB

In Windows version of RethinkDB, like Linux/OS X versions is executed from the command line. You will need to start Windows command shell.

Press Win + X and click “Command Prompt”; or

Open Start menu, click Run and type cmd ENTER.

Use cd command to go to the directory, which you unpacked rethinkdb.exe in.

  1. C:\Users\yusuf\>cd RethinkDB
  2. C:\Users\yusuf\RethinkDB\>

Now, you can start RethinkDB with its default options.

  1. C:\Users\yusuf\RethinkDB\>rethinkdb.exe

After pressing enter, you will see the screen given below.

NodeJS

How to install client side driver?

Installing JavaScript driver

Before you install a driver: install RethinkDB first. Looking for another language? See the complete list of client drivers for RethinkDB.

Installation

Prerequisites: JavaScript driver requires Node.js >= 0.10.0.

Install the driver with npm,
  1. $ npm install rethinkdb
Usage

You can use the drivers from Node.js, as shown below.
  1. $ node  
  2. r = require('rethinkdb')  
  3. r.connect({  
  4.     host: 'localhost',  
  5.     port: 28015  
  6. }, function(err, conn) {  
  7.     if (err) throw err;  
  8.     r.db('test').tableCreate('tv_shows').run(conn, function(err, res) {  
  9.         if (err) throw err;  
  10.         console.log(res);  
  11.         r.table('tv_shows').insert({  
  12.             name: 'Star Trek TNG'  
  13.         }).run(conn, function(err, res) {  
  14.             if (err) throw err;  
  15.             console.log(res);  
  16.         });  
  17.     });  
  18. }); // This is just a sample script. Paste your real code (javascript or HTML) here.  
  19. if ('this_is' == /an_example/) {  
  20.     of_beautifier();  
  21. else {  
  22.     var a = b ? (c % d) : e[f];  
  23. }  

NodeJS

Johnnyfive.io setup

(Visit My previous article for more information: Node.js for IOT-1)

Johnny-Five is a JavaScript Robotics & IoT Platform. Released by Bocoup in 2012,Johnny-Five is maintained by a community of passionate software developers and the hardware engineers. Over 75 developers have made the contributions towards building a robust, extensible and composable ecosystem.

Microcontrollers and SoC platforms likes to say "Hello World" with a simple blinking LED; the following demonstrates how to do this with Johnny-Five framework.

  1. Install Node.js (Prefer 4.2.1 LTS).
  2. Setup your board.
  3. Run: npm install johnny-five


  1. var five = require("johnny-five");  
  2. var board = new five.Board();  
  3. board.on("ready"function() {  
  4.     var led = new five.Led(13);  
  5.     led.blink(500);  
  6. });  

Using RethinkDB, Node.js and JohnnyFive.io together

The codes given below include RethinkDB and Node.js for IoT skills together. The Application is given below and its diagram is related to pulsing from Arduino with LED's.

NodeJS

The pictures and its six steps are given below on how to write pulses to RethinkDB Server step by step

  1. Adding RethinkDB and Johnny-Five plugins as requirement
  2. Connect RethinkDb Server over 28015 number port for hosting .
  3. Create pulsetable in RethinkDB Server.
  4. Setup 3,5,6 pins on Arduino for sending pulse to LED's.
  5. Adding cutepoints and the keyframes data on array.pulse() function.
  6. Sending cutepoints and the keyframes data to pulsetable .

    NodeJS

After running; node ledarray.js, the screen will come, which is shown below.

NodeJS

Our LEDs start blinking by making a pulse.

What happens on RethinkDB?

Each of the pulses have monitoring on RethinkDb dashboard. There is a throughput plot. Frame by frame; all of the picks are flowing right to left after the pulse blinking.

NodeJS

Conclusion (http://www.tothenew.com/blog/iot-and-nodejs/)

The devices like sensors, beacons, transmitters, motors have a tendency to generate a  large volume of data thereby generating a large number of requests; Node.js is well equipped to handle these requests through the streams.

Streams offers both readable and writable channels, which helps in piping the request to the destination without temporarily storing the data. Streams are basically Unix pipes and can directly connect to the destination.

Key features of any IOT driven Application are collecting data, communicating, analyzing and acting . Node js makes a perfect partner for all the features given above.

Sockets and MQTT protocol are well suited for Node. js, which is generally used for the continuous data transmission in an IoT Application.

IoT-Application cable boards, such as Intel Edison, BeagleBone Black, and Raspberry Pi can easily install Node. js as a programming environment. Node. js comes with npm Package Manager, which contains many useful IoT modules, which can be used for the rapid and robust Application development.

Node.js is known for its speed, scalability and efficiency, making it a key player for data- intensive real time applications. This makes Node. js well suited for IoT, which relies on the data intensive real-time traffic.

IoT devices' commands are generally written in low level languages like C and C++, which are difficult to learn. Node. js comes with the power of JavaScript, which is pretty easy to learn and understand.

Node.js is an open source community and npm (Node Package Manager) contains more than 80 for Arduino controllers, Raspberry Pi, Intel IOT Edison. It contains more than 30 packages for the different sensors and bluetooth devices.

These modules makes application development fast and easy. RethinkDB is an open-source distributed JSON document database with a pleasant and powerful query language.

The developer can tell the database to continuously push the updated query results to the Applications in realtime. RethinkDB allows the developer to build scalable realtime apps in a fraction of the time with few engineering resources.

In this course, you learned how to use these features to get started on your own realtime apps.

References
  • https://www.programmableweb.com/news/why-node.js-ideal-internet-things/analysis/2014/07/31
  • http://www.tothenew.com/blog/iot-and-nodejs/

Up Next
    Ebook Download
    View all
    Learn
    View all