Node-RED data processing II: Difference between revisions

From Sensors in Schools
Jump to navigation Jump to search
Line 42: Line 42:
[[File:Screen Shot 2022-01-01 at 6.47.40 am.png]]
[[File:Screen Shot 2022-01-01 at 6.47.40 am.png]]


=== 3. Executing a Python program in Node-RED ===
=== Executing a Python program in Node-RED ===
* Start Node-RED in the Terminal with the command Node-RED
* Start Node-RED in the Terminal with the command Node-RED
* Open a web Browser and enter the URL localhost:1880
* Open a web Browser and enter the URL localhost:1880

Revision as of 20:50, 31 December 2021

Overview

  • This lesson will demonstrate how Node-Red can be used to process data from a sensor.
  • In this lesson we will learn how to process the output from a python file using the function node in Node-RED.

Learning Objectives

  • Learn how to run a Python program using the Terminal.
  • Learn how to run a Python program using the Execute node in Node-RED.
  • Learn how to extract data using the Function node in Node-RED.

learning to process data using Node-RED

  • Node-RED makes it very easy to display data using a Dashboard, but first we need to get the data we need.
  • Luckily, we have experience retrieving data from sensors using Python.
  • Reading and processing data using Node-RED is not very different.

Open the file peter_hopper_get_data5.py

  • From the main Raspberry Pi menu navigate to Programming and open Python3 IDLE.
  • Navigate to the /home/pi/peter-hopper-t3 directory. (t3 is short for term 3).
  • Open up the file peter_hopper_get_data5.py and review to make sure it is the same as the code below.

  • Run the file and check that it can retrieve sensor data and send the standard output to the Python Shell.

Running python program from the Terminal

  • Python programs can also be run from the Terminal.
  • Open a new Terminal window.
  • Navigate to the peter-hopper-t3 directory with either of these commands.
    • cd /home/pi/peter-hopper-t3 – Absolute path reference includes the full pathname
    • cd ~/peter-hopper-t3 – Absolute path reference but uses the ~ (tilde) symbol in place of /home/pi
    • cd peter-hopper-t3 - Relative path reference – assumes you are in /home/pi

  • To run the peter_hopper_get_data5.py file enter the command python3 peter_hopper_get_data5.py
  • The output will be like the output from the Python Shell.
  • The output is a String.

Executing a Python program in Node-RED

  • Start Node-RED in the Terminal with the command Node-RED
  • Open a web Browser and enter the URL localhost:1880
  • In Node-RED arrange the following nodes in the arrangement shown below.
    • inject node – displayed as a timestamp
    • exec node – execute node can run commands such as python3 peter_hopper_get_data5.py
    • debug node – displaying as msg.payload
    • json node – to convert String outputs to a JSON object

  • Double click on the exec (execute) node and enter the following parameters:
  • Command: Two commands on the one line will be executed:
    • cd /home/pi/peter-hopper-t3 – navigate to the directory peter-hopper-t3 using Absolute path notation
    • && - the AND Operator. The AND Operator will execute the second command only if the preceding command succeeds.
    • python3 peter_hopper_get_data5.py – second command
  • Note – Do not Append the msg.payload. Append should be de-selected (as shown).

  • Click on the Deploy button.
  • Ensure that the Debug Window (on the right) is selected and click on the Rubbish bin icon to clear Debug messages.
  • Run the program by clicking on the button to the right of the inject (timestamp) node.

[[]]