Arduino Node-Red
Arduino Node-Red
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...).
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:
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 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
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.
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);
}
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.
{"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.
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.
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
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.
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
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:
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
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