0% found this document useful (0 votes)
55 views19 pages

Arduino Node-Red

This tutorial presents how to connect an Arduino to Node-RED to retrieve and display sensor measurements in the form of graphs and CSV files. It explains the installation of the necessary nodes in Node-RED, the Arduino code to acquire the measurements and format them in JSON, and then the creation of a flow to visualize and save the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views19 pages

Arduino Node-Red

This tutorial presents how to connect an Arduino to Node-RED to retrieve and display sensor measurements in the form of graphs and CSV files. It explains the installation of the necessary nodes in Node-RED, the Arduino code to acquire the measurements and format them in JSON, and then the creation of a flow to visualize and save the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Getting started with Arduino and Node-RED.

Record and display measurements in


JSON from the serial port
May 30, 20192

The Arduino is very handy for retrieving measurements from various sensors (presence of
pollutants, temperature, humidity, brightness, UV index, CO or CO2 levels…). In this
In this tutorial, we will learn how to quickly create a small system.
of data recording using Node-RED. It will be very easy to visualize
the measurements in the form of a graph or export them to a CSV file (text file
with a semicolon as a data separator) to perform statistical calculations at
the help of a spreadsheet (LibreOffice, Excel, Number...).

This is a tutorial suited for beginners with Arduino code.

Summary[to mask]
1 Necessary materials
2 What you will learn in this Arduino tutorial
3 What is Node-RED?
4 Install the Node Dashboard for Node-RED
5 Install the Node Serial
6 Circuit Arduino
7 Arduino Code
o 7.1 Format the data in JSON
8 Connecting to the Arduino serial port with Node-RED
Extract the data from the JSON coming from Arduino
10 Visualize the measurements on a graph
11 Save the measurements in a csv file
12 Complete project flow
o
. 12.0.1 Share

Necessary materials
For this project, you will need:

From an Arduino Uno or a clone (any model will do). You


You can also use an ESP8266, but it only has one input.
analog. You can also use an ESP32
From a computer on which Node-RED and the Arduino IDE are installed.
o You can use a Windows PC, a Mac, or a Raspberry Pi.
Followthis tutorialto install Node-RED on your computer
o Readthis oneto install the Arduino IDE on Raspbian if you want
use a Raspberry Pi.
One (or multiple sensors). Here, I used two probes to measure humidity.
of the ground. A classic probeYL-69and a conductivity measuring probe (marked
1.2coated with an anti-corrosion protective paint.

Amazon.fr
WINGONEER Soil Hygrometer Water Detection Moisture Sensor Module YL...
8.49€
Amazon.fr
AZDelivery UNO R3 with a USB cable for Arduino, 100% compatible with...
9.99€
Amazon.fr
Raspberry Pi 3 Model B +
49.90€

What you will learn in this tutorial


Arduino
In this tutorial, you will learn

How to retrieve (acquire) data from an analog sensor with code


Arduino (C++)
How to send measurements over the serial port
How to format measurements in JSON for use
easily from Node-RED
Discover Node-RED
Connect to the serial port of an Arduino Uno (also works with a
ESP8266 or an ESP32)
Learn the basics of JavaScript programming under Node-RED
You can also readthis previous article.
The tutorial is also available in video on YouTube.

What is Node-RED?
Node-RED is a versatile development environment by block assembly.
functions developed by IBM. It is a more advanced project than Scratch 2. Node-RED is
today used to develop professional applications. It allows to do
of application prototyping very quickly (provided you have a bit of experience, otherwise
it's longer...). It is also an excellent intermediate learning tool between
Scratch and traditional code. It is possible to code in Javascript with the
Node function (function).
You can start by reading these tutorials if you are a beginner or do not know.
still Node-RED

Getting started with Node-Red on Raspberry Pi 3, installation, automatic startup


Install Node-RED on Raspbian Stretch Lite (tutorial with Raspberry Pi Zero
W)youArmbian on Orange Pi
Node-RED: easily install and uninstall modules with the manager
palette

Install the Node Dashboard for Node-RED


It is very easy to create a chart to visualize the measurements in
origin of the Arduino. Open the Node-RED menu (icon in the upper corner
right) to access the Palette manager

In the search field, enter the keyword dashboard.


Install the node-red-dashboard pluginGitHub pageThere are several others. It is
one of the first, it is very comprehensive and very easy to use (no notion of
necessary programming). To discover all the features of the plugin
dashboard, readthis first tutorial (button, list, switch, slider, form of
inputandthe second (gauge, graphs, notifications, HTML template).

Install the Node Serial


Since version 0.20, it is necessary to install the Serial node which allows for
communicate via the serial port. As before, search for the word
Serial key and install the official node-red-node-serialport package.
Arduino Circuit
Nothing complicated for the circuit, just power the sensor and retrieve the
signal on the analog pin of the Arduino. Here, the YL-69 (or FC-28) is connected to the
Pin A4. The second sensor on pin A5.

Arduino Code
We will simply read the signal value at regular intervals (here every 5s, 5000ms).
analog (from 0 to 1023) for each soil moisture probe. Then we send the
output to the serial monitor of the Arduino IDE.

#define wait 5000

void setup(){
// Init serial port (115200 bauds)
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
int a4 = analogRead(A4); //v1.2
int a5 = analogRead(A5); //YL-69
v1.2:
Serial.print(a5);
YL-69:
Serial.println(a4);
delay(wait);
}

Format the data in JSON


We could directly send the measurements to the serial port of the Arduino with a
separator, for example a special character (|, -, #…) but this strategy implies that
to know precisely the position of each data. No problem with one or
two data points, it becomes more complicated when there are about ten. Another problem, the
String conversion is an eternal problem in computer science.

To avoid all this problem, we will format the data and indicate to
each time it corresponds to. For this, we will use the JSON format.
The advantage is that it is supported by all modern languages. It is even the
default data structure of JavaScript, the language on which Node-RED is based.

JSON is a structured format for key-value type data. Each


data line is separated by a comma (except for the last line). A value can
to be a string (an image will be a string), a number (integer or
decimal), an array (string, number), a structure (which will contain itself
data in the form key = value). Here is an example. First online, this is
what the Arduino code will generate

{"sonde1":22.1,"sonde2":64.1,"unites":{"sonde1":"°C","sonde2":"%"}}
We can unfold the structure to make it more readable (and find an error)

{
"sonde1":22.1,
"sonde2":64.1,
"unites": {
°C
%
}
}
To verify your code, I recommend using the sitejsonlint.comwho is free.
Jsonlint indicates the line (and the cause of an error but it is not very explicit). Here it is
you should put a comma instead of the semicolon

For big projects, like this weather station with a web interface, I
I recommend using the ArduinoJSON library. It allows you to store data in
JSON format in the Arduino memory. It's very convenient for extracting
data, settings, save a history in SPIFFS memory or a card
SD.

Here, we will simplify and directly build a string and


send it to the serial port which gives the following code

#define wait 5000

void setup(){
// Initialize serial port (115200 baud)
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
int a4 = analogRead(A4); //v1.2
int a5 = analogRead(A5); //YL-69

v1_2_raw
Serial.print(a5);
YL69_raw
Serial.print(a4);
Serial.println("}");
delay(wait);
}
This is what is currently shown on the serial monitor

{"v1_2_raw":250,"YL69_raw":301}
Let's check with Jsonlint if everything is correct.

Perfect, we can continue on Node-RED now.

Connection to the Arduino serial port with


Node-RED
Log in to Node-RED from a web browser at address localhost:1880 or
from another computer (IP:1880)
The Node RED code is called Flow. We will start by connecting to the Arduino.
the help of the Node (the programming block) Serial (in the Input palette, since we want
read the measurements). Drag and drop the node onto the blank page and double click on it
on the Serial Node to open the control panel.

Click on the pencil to add a new connection. Use the magnifying glass to list
the COM ports. Here the Arduino is connected to a Raspberry Pi 3, so the path to
The Arduino is in Linux format. On Windows, it will be a COMx port.
In the Arduino code, the line of code Serial.begin(115200) initializes the
speed at 115200 bauds, indicate the speed under Baud Rate. Save, that's all
what there is to do

Find the Node Debug

Connect the Serial Node to the debug Node. To do this, hover the mouse over the square that
symbolizes the Node Serial output, an orange wire appears. Go attach it to the input of
node debug. You have just created your first flow.
Deploy the flow by clicking on Deploy and open the debug tab to visualize the
data coming from the Arduino. Wait a few seconds depending on the
timed delay in the Arduino code.

Extract the data from the JSON coming from


the Arduino
For the moment, we are retrieving a string from the serial port of the Arduino, we
it will convert it into a JSON object usable by Node-RED using the JSON node.
Place it on the flow and connect it to the serial port.

We are now going to extract each measurement with a bit of JavaScript code. Place a
node Function and connect it to the output of node JSON
Open the function and paste this javascript code.

msg.payload = msg.payload.YL69_raw;
return msg;
Node-RED transfers messages (msg) in JSON format between each node.
program). The data is found in the payload key. The first line extracts the
measurement of the YL-69 (or FC-28) sensor and overwrites the current payload.
msg.payload = msg.payload.YL69_raw;
The function returns the updated message (msg). It no longer returns only the measurement of
first sensor

Do the same for the second sensor by adding a second flow.

Visualize the measurements on a graph


Find the node chart and place it on the flow.

Connect the functions to the node chart. Open the configuration panel of the
graph. First click on the pencil to create a group. To learn more about
the organization of the group, readthis article.
Then you can modify certain settings:

Group: select the group you just created


size
label
X-axis: number of points or time period
Name: the name that appears on the flow

Save, deploy and go to localhost:1880/ui (or IP:1880/ui) to view


the measurements from your sensors connected to the Arduino
Save the measurements in a csv file
Now, if you want to exploit your data on an Excel or LibreOffice spreadsheet,
you can save them in CSV format (text file with data separator
is a semicolon). To do this, we will add a new function that will simply
send back a line where each data is separated by a semicolon. Here is the order
of columns

date in year/month/day format (supported by all spreadsheets)


time in hh:mm:ss format
value of the probe v1.2
value of the YL-69 or FC-28 probe
var date = new Date().toLocaleDateString();
var time = new Date().toLocaleTimeString();

date + ";" + time + ";" + msg.payload.v1_2_raw + ";" +


YL69_raw
output
return msg;
Find the node file and place a node input on the flow
In the control panel, indicate the destination path. By default the file
is recorded in the Node-RED directory. Check Add newline to each payload for
add a new line to the file for each new record

Deploy, after a few minutes, here is what the CSV file will look like

2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-05-21
2018-5-21
2018-05-21
2018-5-21
2018-5-21
2018-05-21

Complete flow of the project


Here is the complete flow of the project
and the corresponding code.

9435a9fc.ab96d8
in
c237b06b.eb7ca
Invalid input format. No translatable text found.
Invalid input. Please provide valid text for translation.
payload
[["2499ca17.61eff6","df59b710.dc7648","6ee454f.ffd6bac","b292e47a.faf158"]]
},
For
mat Arduino sensors Data
Date().toLocaleDateString(); var time = new
Date().toLocaleTimeString(); var output = date + ";" + time + ";" +
msg.payload.v1_2_raw + ";" + msg.payload.YL69_raw; msg.payload =
return msg;
[["62e4634a.85417c"]]},
Invalid input for translation.
/home/pi/moisture_sensors.csv
false
Ext
ract v1.2 data
msg.payload.v1_2_raw; return
msg
6ee454f.ffd6bac
ract YL-69 data
msg.payload.YL69_raw; return
msg
c4ed7b7f.b766a8
bb260e6b.77d62
o YL-69 + v1.2
data
bezier
removeOlderPoints
alse,"colors":
["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#94

{
fa
lse
Act
false

port
8
","addchar":false},
bb260e6b.77d62
Moisture
sensors
fe82c1e5.b7118
dashboard
Copy the previous code. Open the NodeRED menu then Import -> Clipboard

Paste the code and choose where you want to paste it then Import
Here it is, if you liked NodeRED and want to go even further, here is
other articles on the subject

You might also like