I like the light-weight 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 i.e. 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, in its own, is a big topic. Its wide usage across industrial devices is well known and there are quite very useful, industry proven protocols which are using serial communication at their core. RS-232, RS-485 and many others. You can read my article about RS232 on this link. However, to keep this post focused, I am not going to discuss the communication protocol any more than it is needed.
The source code for this application can be downloaded from the link in summary section.
Setting the Scene
Here is what we are going to do. I have an Arduino board which 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 like:
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 light weight JSON file based database i.e. lowdb. This part will demo a very simple to use lowdb API which provide very easy persistence mechanism for node application.
Well, persisting this data to lowdb is ok for testing purposes, but we will take it one step further and will also persist it to elastic-search. However, persistence to elastic-search will be done in next post along with visualization part.
So, we will read sensor data from hardware and persist it to local json file based database (lowdb).
Prerequisite
You can download the source code from git repository. However, there are certain concepts, I am assuming you are already aware of. The following articles will help you incase 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 existing Arduino light sensor project for this demo. You can read about it from the following link:
This article shows how to read analog values from light sensor and I am reusing this project
2. Node.js Setup
Node installation and setup is also covered in my previous article, you can read it in the following link.
3. Connecting 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.
and here is the code which connects with Arduino and read the sensor value. Very simple, right?
Here is the output of running this code:
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 to elastic-search and later will visualize this data. This is a very typical task in IoT projects.
4. Data Persistence
Lowdb is described as following on their repo page.
Small JSON database for Node, Electron and the browser. Powered by Lodash.
The repo also provide instruction about how to install and use it. I used npm to install it.
the following code of dbservice wraps lowdb API.
Now, in server.js file, I imported dbservice and instead of writing to console. its is saving to db. Please see code below:
Output JSON file
Once, execute 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.
Summary
This concludes this post. You can download the code from this github repo. Let me know of your comments. In next post, we will move forward from this point. Till next time, happy coding.
References
- https://hexquote.com/arduino-analogread-with-light-sensor/
- https://github.com/typicode/lowdb
- https://serialport.io/
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.