Monday, July 20, 2015

IoT using Arduino, Node.js and Plotly

Internet of Things (IoT) has been a hot buzzword for the last few years. Simply put, IoT is the concept of connecting objects to a network in order to transfer data without human-to-human or human-to-computer interaction. This simple, yet powerful concept has a wide range of applications in manufacturing, healthcare and home automation, just to name a few. IoT is an interdisciplinary field, which requires working with electronics and sensors to capture data from physical objects, computer networking for data transfer and IT skills for building meaningful applications.
In this blog post, I will introduce the building blocks for creating a simple IoT application. To do this, I will use an Arduino microcontroller with a photocell (light intensity sensor), a node.js server for capturing and transferring the data, and a cloud service called plotly to visualise the data. By the end of this tutorial, we will have a functioning IoT application that you can customise to other use-cases.

1. Use case definition and architecture

To keep things simple and focused, we will build a simple application that keeps measuring light intensities using a photocell and plots these values on a graph in real-time.

Required hardware:

  • One Arduino microcontroller
  • One photocell
  • One 1k Ohms resistance
  • One bread board
  • Five jumper wires male
  • One USB cable
  • One computer (MAC or Windows) with internet connection

Architecture

To build this application, we need 3 main components:
  • A physical layer for capturing light intensities. We will implement this using an Arduino micro controller and a photocell.
  • A coordination layer used for capturing the measurements from the physical layer, and sending the measurements to our application. We will implement this using node.js.
  • An application layer for visualizing the measurements in real-time. We will implement this using a data visualization cloud service called Plotly.



Using the Node.js script

Install node.js




Download and install the latest Arduino software

Launch the Arduino IDE, and upload the 'Standard Firmata' sketch to your board.

Once uploaded, close the Arduino IDE. We're done with it. The 'Standard Firmata' sketch is all we need to upload to allow any computer with node.js to communicate with it!

In your terminal, create a folder for your sensor project and move into it

$ mkdir your-project-name
$ cd your-project-name

Use the node package manager (npm) to install the required libraries.

node.js is awesome and will only install these modules in your project directory. It's easy to keep things modular and organized!
$ npm install plotly
$ npm install johnny-five

Download one of our example scripts into your project folder:

Download the lightsensor.js script.
Open lightsensor.js, and let's go through it! First we create a new plotly object with your credentials:
var plotly = require('plotly')("hamdicharef", "vvao262d23");
The init_data object is what we send to plotly when we initialize the graph. We are sending empty arrays to act as containers for our data. You can also choose the maximum number of points to display.
Don't forget to change the 'streamtoken' to your own! Find your token https://plot.ly/settings/api
var init_data = [{
    x : [],
    y : [],
    stream : {
        token : 'streamtoken',
        maxpoints : 5000
    }
}];
Johnny-Five will automatically connect to your Arduino over serial, and start streaming all the data to Plotly!

Conclusion

In this tutorial, we learned how to build an end-to-end IoT application covering:
  1. Getting measurements from an analogue input
  2. Processing the data using node.js
  3. Visualising the data using a 3rd party service
The techniques introduced in this tutorial can be leveraged to other IoT use cases. Below are a few ideas that you can try to improve on this solution.
  • Wireless communication between Arduino and Node.js. This can be done using an Arduino Xbee shield.
  • Saving the data to a database server. This can be done by customising the node.js code to push the data to a database, for example mongodb.
  • Adding additional input to the application, for example a temperature sensor.

Reference

Link2
Link3




No comments:

Post a Comment