Node-RED introduction

From Sensors in Schools
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.

Learning Objectives

  • Learn how to start node-red on the Raspberry Pi.
  • Learn how to run Python programs in Node-RED.

Introduction to Node-RED on the raspberry Pi

  • Node-RED is a powerful and intuitive programming language pre-installed on the Raspberry Pi.
  • This lesson will serve as an introduction to the use of this platform.
  • In this lesson we will learn how to execute python programs.

Run the python 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 send output to the Python Shell.
  • A typical output in the Shell is presented below.

  • Note the very first line that shows:
    • /home/pi/peter-hopper-t3/ – the full path of the file
    • peter_hopper_get_data.py – the file name
  • We will need to use this information later when coding Node-RED.

Finding the path and name of a file in the Terminal

  • Open the Terminal.
  • Enter the command ls to list all directory contents.
  • Then enter the command pwd – which gives the path to the present working directory
  • In this example the path to the pi directory is /home/pi

  • To change directory and enter the peter-hopper-t3 enter the command cd peter-hopper-t3
  • To list all the contents of the peter-hopper-t3 directory enter ls
  • To determine the full path to this directory enter pwd
  • Notice that the path is the same as the output in the Python Shell.

  • To run the peter_hopper_get_data.py file enter python3 peter_hopper_get_data.py

  • The program output that we typically see in the Python Shell is now in the Terminal.

Starting Node-RED

  • To start Node-RED open the Terminal and enter the command Node-RED

  • You should see an output similar to below.
  • I have some additional programs already running in Node-RED, so my display will be different.

  • Before shutting down the Raspberry Pi you should always stop Node-RED.
  • Open a new Terminal window and enter node-red-stop
  • If you need to restart Node-RED enter node-red or node-red-start

Opening the Node-RED interface using a Web Browser

  • Open the default web browser on the Raspberry Pi
  • In the Search bar enter the URL localhost:1880

  • This will open the Node-RED interface.
  • Note that you need to start the Node-RED server in the Terminal first.

Creating your first flow

  • Within Node-RED there is a pallet of different programming nodes.
  • Drag the inject node and the debug node into the Editor window.
    • Note that the title of the inject node changes from inject to timestamp.
    • The name of the debug node also changes from debug to msg.payload
  • Connect both nodes with a wire using the mouse.
  • Note that there is a small blue circle on the top of each node. This means that there has been a change and program needs to be Deployed using the red Deploy button.
  • Click on Deploy to run this small program.
  • On the right-hand side of the Node-RED window click on the debug tab so that the Debug window is displayed. The Debug window is like the Python Shell and will display program outputs coming from the Debug nodes.

  • When the Deploy button is clicked the button becomes greyed out and the blue circles on the nodes disappear.

  • To run the program click on the grey button on the right-hand side of the inject node (named timestamp).
  • By clicking on the button, you inject a package of data.
  • The data is packaged in a message (msg) and put into a pigeonhole named payload.
  • A timestamp in computers is a representation of the amount of time that has passed since 1st January 1970.
  • In this case the timestamp is the number of milliseconds from 1st January 1970 – 1634681792235.
  • You can test this by pressing the inject button once every second to see how the number increments.

Executing Python programs using Node-RED exec node

  • Drag the inject, debug and exec nodes into the Editor window and connect them with wires as shown.

  • The exec node has three outputs:
    • first – standard output like the output in the Python Shell or Terminal + return code
    • second – error output + return code
    • third – return code (rc). Any return code above 0 is an error.
  • Double click on the exec node to bring up the Edit exec node window.

  • In the Command line enter the following two part statement:
    • cd /home/pi/peter-hopper-t3 – change directory giving absolute path reference
    • python3 peter_hopper_get_data.py – run the program using python3
  • Set all other settings as shown then click on the red button Done.

  • When you return to the editor window you may need to re-arrange your nodes.
  • Then click on the Deploy button to run your program.

  • To run the program, click on the grey button the inject node.
  • This will inject a timestamp message (msg.payload).
  • This has the effect of triggering the exec code and making your python program run.
  • On the right hand side in the debug window you will see the standard output from your program and any error messages.

  • In the debug window, clicking on the arrow on the left of the standard output will expand the msg.payload.
  • In this example we can see the standard output from the python program.
  • The return code is 0 which indicates no errors.

  • Congratulations for completing your first program in Node-RED.
  • In future lessons we will learn how to chart data using the Node-RED Dashboard.