{{announcement.body}}
{{announcement.title}}

IoT Data, Serial Transmission, and Persistence Using Node.js

DZone 's Guide to

IoT Data, Serial Transmission, and Persistence Using Node.js

In this post, we will see that we can use Node.js for our IoT projects very easily to persist data from an Arduino board to lowdb.

· IoT Zone ·
Free Resource

 I like the lightweight programming model of Node.js. I also have great interest in IoT and I have written several posts on that topic as well. In this post, I am going to combine both to make an IoT device with Node and you will see how natural this binding is.

The Internet of Things is filled with hardware that talks over serial ports. Serial communication, on its own, is a big topic. Its wide usage across industrial devices is well-known and there are many very useful, industry-proven protocols which use serial communication at their cores, like RS-232, RS-485, and many others (there is a link about RS232 protocol in the reference section or you can Google it). However, to keep this post focused, I am not going to discuss the communication protocol any more than it is needed.

Setting the Scene

Here is what we are going to do. I have an Arduino board that has a photo-resistor attached to it and it is reading light-intensity in the room on a specific interval. Here is how the setup looks: 

So, this setup represents our IoT device. You can have your own device setup or any other thing which is generating data. However, for this post, I am going to use this setup.

This Arduino is connected to my laptop via USB port (serial over USB). On the laptop, I have a node application and this application will read the photo-resistor values from the Arduino. So this part is about connecting Arudino and Node.js. 

Once we have this value in our Node application, we will persist it a very lightweight JSON file-based database, lowdb. This part will demo a very simple-to-use lowdb API which provide a very easy persistence mechanism for node application. 

Persisting this data to lowdb is ok for testing purposes, but we will take it one step further and will also persist it to Elasticsearch. However, persistence to Elasticsearch will be done in the next post along with the visualization part. 

So, we will read sensor data from hardware and persist it to local json file based database (lowdb). 

Prerequisites

You can download the source code from the git repository. However, there are certain concepts, I am assuming you are already aware of. The following articles will help you if you want some background information. In this post, I will move forward from these topics and only focus on the new parts.

1. Arduino Setup

I will be using an existing Arduino light sensor project for this demo. If you have some other setup, you can use that one as well. However, if you want to read this Arduino setup, you can check it on this link. This article shows how to read analog values from the light sensor and I am reusing this project.

2. Node.js Setup

Node installation and setup is also covered in my previous article on DZone; you can read it on this link.

3. Connecting a Node with Arduino

This part is very simple. We are going to use serial communication for this purpose. SerialPort package help us in achieving exactly this. Please use the following command to install this package

Installing SerialPort

Installing SerialPort


and here is the code which connects with Arduino and read the sensor value. Very simple, right? 

Connecting Arduino

Connecting Arduino


Here is the output of running this code: 

Output

Output


Ok, we are successfully reading the value from Arduino, and in next step, we can persist it using lowdb to a local JSON file. At this point, you can do whatever you want to do with this data. As I mentioned before, I am going to persist it to Elasticsearch and later will visualize this data. This is a very typical task in IoT projects.

4. Data Persistence

Lowdb is described as "a small JSON database for Node, Electron and the browser. Powered by Lodash," on their repo page.

The repo also provides instructions on how to install and use it. I used npm to install it.

Install Arduino with npm

Install Arduino with npm


the following code of dbservice wraps lowdb API. 

Wrapping the lowdb API

Wrapping the lowdb API


Now, in server.js file, I imported  dbservice and instead of writing to console, it's is saving to the database. Please see code below: 

Importing dbservice

Importing dbservice


Output JSON File

Once executed, you will see a new db.json file in the root directory of your project. This file will have the sensor readings from the Arduino.

New db.json file

New db.json file


Summary

This concludes this post. You can download the code from this github repo. Let me know if you have any comments. In next post, we will move forward from this point. Until then, happy coding.

References

Topics:
arduino ,data persistence ,internet of things ,iot ,lowdb ,node.js ,serial transmission ,tutorial

Published at DZone with permission of Jawad Hasan Shani . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}