Creating an IoT case with Node + Arduino

Otavio Augusto
5 min readMar 6, 2018

--

Lots is discussed about IoT(Internet of Things) and it is a hot topic when we talk about the present and future about internet and data. There are big conferences like IoT Evolution Expo, Linux Foundation OpenIoT Summit dedicated to talk about how IoT is changing areas like medicine, smart cities and wearing. But how can we implement and prototype a simple idea for proving the IoT concept? That’s what we will cover on this tutorial!

IoT is a term which is used for describing a device which can interact and capture data around it’s environment. After captured, it can be sent through internet, processed and transformed in relevant user information.

So here it is what we’re gonna build:

  1. A device which is able to capture the local environment temperature, using Arduino;
  2. A Node application which is responsible for listening to Arduino’s captured temperature and send it to a publish/subscribe API (Dweet.Io);
  3. A Node application that will subscribe to Dweet.io, wait for posted data and print it to a dashboard in real time!

Awesome, right :) ?

1- Hardware

It’s expected that you already have knowledge about Arduino, like capturing data with a sensor. But that’s not mandatory and won’t be a problem for understanding and developing this case! For mounting our device we will need:

  • Arduino (Uno R3, for example);
  • Jump wires;
  • Resistor;
  • NTC sensor;
  • Usb-Serial Cable;
  • A PC/Notebook

Instructions

First, you should download and install an Arduino IDE ! After downloading and installing it, we will use a protocol called Firmata for communication with microcontrollers from a software in a Computer. On this case, from Arduino to your computer.

To use Firmata protocol, it’s necessary to open the Arduino IDE and run an already existing project, which is located at the File/Examples/Firmata/StandardFirmata menu option:

After doing that, it’s necessary to load it to Arduino using the load button, the second one from left to right:

Then, your Arduino will be able to start using Firmata protocol, so it’s possible to communicate with a Computer through USB :)

Our main goal here is to make Arduino collect the local environment temperature and send it to a Node application. The following image and steps represent how we should sketch the Arduino circuit:

  1. Feed the NTC sensor with a 5 Volts from a 5V Arduino port (red wire);
  2. Use a resistor and read the temperature value from A0 analog port (yellow wire)
  3. Close the circuit at GND (black wire):

The following image represents how it should look like:

2- Application responsible for communicating with Arduino

Now we’re gonna work with a node library called Johnny-Five. It lets you fetch data and command Arduino’s ports using Firmata protocol. We will also use a lib called node-dweetio, which is a IoT and M2M tweet dashboard. It allows using a publish/subscribe model for doing that. The application code can be cloned on this repository.

It should look like this:

What happens here can be described on the follow steps:

  1. We define the temperature sensor with two parameters: pin that corresponds to the A0 Arduino analog port from where the data will be captured; threshold that represents how much should the temperature vary for triggering the event change. It’s recommended to provide a value like 4 or 5.
  2. The temperature sensor will wait for a change event that is triggered as the temperature varies accordingly with the threshold value.
  3. The value is sent as a tweet to a Dweet.io channel (Dweet defines it as a thing) called web-client-iot-case.

3- Application responsible for receiving Arduino’s data

Here we will develop the application responsible for listening to Dweet web-client-iot-case channel. As new tweets are sent to Dweet.io, whoever is listening will be able to receive this messages!

On this application we will use the socket.io library to provide real time communication with our dashboard,where incoming messages will be printed. To make the front-end simple we will use an existing bootstrap template.The application code can be cloned here. This is how it should look like:

Here we use express for running our server. The most important points on this file are:

  • the “io.on(‘connection’, ..)” which is declared for incoming sockets;
  • “dweetio.listen_for(‘web-client-iot-’, ..” , which linstens for published tweets on “web-client-iot” thing. As new messages are published, they will be emitted as a ‘sensor-data’ event. So our page can wait for this message and print it, as you can check bellow:

From this point on, it will start printing a new line for all the incoming messages which are published to Dweet.io.

We have learned how to work with our Arduino from our computer, build an application to capture data from a temperature sensor, send it to a Twitter of Internet of Things (Dweet.Io) and how to listen to Dweet tweets and print it in real time using socket.io. That’s it! We have built our very first IoT case! Thanks :)

Fonts:

Special thanks:

Tiso Alvarez Puccinelli for reviewing this tutorial!

--

--