Node-RED data processing
Jump to navigation
Jump to search
Overview
- The aim of this lesson is to start learning to use Node-RED which is a block-based programming language.
- The appeal of Node-RED is that individual code segments are hidden in nodes.
- Individual nodes can be connected using wires.
- This makes the program much easier to visualise and program.
- In this lesson we will learn how to import data from a python program into Node-RED.
- Data (attributes) can be grouped into something termed an Object (hence Object Oriented Programming).
- A car has attributes such as colour, top speed, number of wheels, etc.
- In a similar way it makes sense to group the data from a sensor into one group.
- We will learn how to group data from a sensor, then convert the data into a JSON Object.
- JSON objects can then be transferred between computers or applications more easily.
- In this example we will transfer a JSON Object from our Python program to Node-RED.
Learning Objectives
- Learn how to create a Python Data Object.
- Learn how to convert a Python Data Object to a JSON Object
- Learn how to extract data from the JSON Object using functions in Node-RED
Importing sensor data to Node-RED on the Raspberry Pi
- It is very easy to process and graph sensor data once it is imported into Node-RED.
- This lesson will demonstrate how sensor data can be converted into JSON and then imported within Node-RED.
Review and modify the file peter_hopper_get_data.py
- From the main Raspberry Pi menu navigate to Programming and open Python3 IDLE
- Open the saved file peter_hopper_get_data.py
- The full listing is below.
- Run the program by selecting Run > Run module.
- The program will produce output similar to the following.
- From the File Menu select File > Save As and create a new file named peter_hopper_get_data5.py
- Edit the file so that code is the same as the example below.
- most print() statement have been removed
- a Python object (dictionary) was created to help package sensor data together
the Python object was then converted to a JSON object so that the data can be easily transferred to Node-RED
- a final print() statement that will output the JSON object to Node-RED.
- Running this program should produce the following output.
- JSON data:
- is enclosed in curly {} brackets
- contains attribute:value pairs – similar to a dictionary
- each pair is separated by a comma.
Starting Node-RED
- To start Node-RED, open the Terminal and enter the command Node-RED
- Wait for approximately 15 seconds for the Node-RED server to be initialised.
- Then open a Web Browser and in the Search bar enter the URL localhost:1880
- This will bring up the main Node-RED editing interface.
Importing JSON data in Node-RED
- Using the Node-RED pallet on the left, create the following flow using nodes – inject, exec and debug.
- Only attach a debug node to the top output from the exec node (the output is called the standard output)
- Double click on the exec node to bring up the Edit exec node window.
- Modify the Command to include these entries
- cd /home/pi/peter-hopper-t3 – change directory
- && - a linker so that both commands execute
- python3 peter_hopper_get_data5.py – the python program we need to execute using python3
- Then click Done to commit changes.
- Click on Deploy when in the main editing window.
- Then click on the button on the timestamp (inject) node to send a signal to start the exec node.
- The output will appear in the debug window on the right.
- The output is in JSON format and similar to the output from the print statement in the python code.
Extracting data from a JSON output
- Construct the following flow of nodes as shown below.
- Double click on the function node and enter the following Javascript code.
- Change the name to Battery data
- Click on Done when finished.
- In the main edit window click on Deploy to commit the code.
- Then click on the button on the timestamp (inject) node to run the program.
- The debug window should produce output from both debug nodes.
- The debug node connected to the Battery data function node will only show the battery data.
- In our next lesson we will demonstrate how battery data can be connected to a chart node so that data can be graphed and shown in a Dashboard.










