LED - Manage your LED related python codes using Node-RED

So far, many Python examples have been created to use the LED matrix. And most of them worked by using ssh remote access or directly connecting a local monitor and keyboard to the Raspberry Pi. However, there is one major drawback to this type of work. There is no scenario management function. Most LED signboards show multiple sentences or images repeatedly in sequence, depending on the conditions. Therefore, we need a tool to properly run the Python files for controlling our LEDs. A suitable tool for this purpose is Node-RED.


Node-RED logo
Node-RED is a flow-based development tool for visual programming developed originally by IBM for wiring together hardware devices, APIs and online services as part of the Internet of Things.[2]
Node-RED provides a web browser-based flow editor, which can be used to create JavaScript functions. Elements of applications can be saved or shared for re-use. The runtime is built on Node.js. The flows created in Node-RED are stored using JSON. Since version 0.14, MQTT nodes can make properly configured TLS connections.[3]
In 2016, IBM contributed Node-RED as an open source JS Foundation project.[4][5][6]

<From https://en.wikipedia.org/wiki/Node-RED>


Installing Node-RED on the DietPi

Node RED is already installed on the Raspbian desktop version. So if you are using a Raspbian desktop, there is no need to install it. I am currently using DietPi as the Raspberry Pi OS. Therefore, I will explain the installation method based on DietPi Buster.

dietpi-software and Node-RED

run the "dietpi-software" and search for the node-red.


Then check the Node-RED, press the ok button.


Finally you should install the selected s/w.

 

Setup the Node-RED

It is convenient to start the Node-RED process as a service at boot time.


systemctl enable node-red.service
node-red-start


But when you check the node-red process, the owner of process is not root!


root@DietPi:/etc/systemd/system# ps -ef|grep node
nodered    745     1  0 21:10 ?        00:00:07 node-red
root      1707   849  0 23:43 pts/0    00:00:00 grep node

The Henner Zeller library we use requires root privileges. Therefore, we must modify node-red to run as root.
Edit the "/etc/systemd/system/node-red.service" file like this.


[Service]
#User=nodered
User=root
ExecStart=/usr/local/bin/node-red -u /mnt/dietpi_userdata/node-red

Then restart the service. node-red.service


systemctl restart node-red.service

Let's check again the owner of the node-red process.


root@DietPi:/usr/local/src/lotto# ps -ef|grep node-red
root      2940     1  5 02:20 ?        00:00:05 node-red
root      2961   849  0 02:21 pts/0    00:00:00 grep node-red

The owner of node-red is now root.

Connecting the Node-RED from your PC

The default connection port of Node-RED is 1880. So check your Raspberry Pi IP address and access it from your PC's browser.  If connected as shown in the following figure, it is normal. (Just enter 192.168.11.89:1880 in the address bar.)



Now, get the inject node and the exec node from the palette and connect them as shown in the following figure.



The exec node is responsible for running the process in Raspberry Pi. Inadvertent use can be dangerous, and the current configuration is running with root privileges, so incorrect use of the exec node can cause Raspberry Pi to go wrong. The configuration of two nodes is as follows.

You can see that we are calling the Python code we created in the previous example. We are also passing a new string in the text parameter that we want to display on the LED.

Be Careful : The command line you use must use an absolute path. Otherwise, you may get an error message stating that the file cannot be found.


Test

Save the scenario flow created by pressing the "Deploy" button. And when the left button of the timestamp node is pressed, an event is generated and the node is executed as the event is delivered in the opened order.


You can add as many nodes as you like, and use switch nodes, function nodes, etc. to create different scenarios for different cases. You can also go back to the beginning and repeat infinitely, as shown in the following figure.

Debugging

If the event is triggered by pressing the timestamp button in Node-RED in the picture above, but the LED does not light up, you can find the cause by using the debugging function of Node-RED. When you move the mouse cursor to the beginning of the three output nodes as shown in the figure, you can see the names of the nodes.


And you can see the description of this node by selecting the info tab on the right.

If you are new to Node-RED, this can be a bit difficult. If so, there are many great companions or videos about Node-RED, so you can get help with how to use it.
As shown in the figure below, take 2 debug nodes and connect them to outputs 1 and 2 of LED1 and 2 exec nodes.



And to artificially generate an error, we will change the command command as follows.



Then click the Deploy button to update the flow and select the debug tab at the top right.
Now trigger the Node-RED event again.


Notice that there are two outputs on the LED1 debug node. The first one displays the stdout output of the LED1 exec node, and the second one displays the stderr output of the LED1 exec node. You can see in the stderr output that msg.payload shows an error message stating that the python file cannot be found. If you use the debug node properly as above, you can easily find the error.


OpenCV Error

I've mentioned in other posts about setting environment variables to use OpenCV 4.X.
See Install OpenCV 4.1.1 to Raspberry Pi at https://iot-for-maker.blogspot.com/2020/01/led-5-lets-make-large-led-display-part_21.html. In this article, I saved the waning variables in a .bashrc file. Saving environment variables in .bashrc file works fine in Terminal. However, Node-RED, which acts as a systemd service, does not work on the terminal, so it cannot read this environment variable. The error message below is also a problem.


Edit the /etc/systemd/system/node-red.service file and add environment variables as follows.


[Unit]
Description=Node-RED (DietPi)

[Service]
Environment="LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1"
#User=nodered
User=root
ExecStart=/usr/local/bin/node-red -u /mnt/dietpi_userdata/node-red

[Install]
WantedBy=multi-user.target

Then restart the Node-RED service.


root@DietPi:/usr/local/src/lotto# systemctl daemon-reload
root@DietPi:/usr/local/src/lotto# systemctl restart node-red.service

Now, even with OpenCV, the above error will not occur.

Wrapping up

Node-RED is a very powerful visualization programming tool. Using this tool, you can create various scenarios for LED control. Although not introduced in this article, if you look at the nodes of the palette, you can import external data using various methods such as SNS, Database, TCP / IP, and http. With proper use of Node-RED and our Python program, we can control the LED matrix nicely.




댓글

이 블로그의 인기 게시물

LED-12. Displaying HDMI Contents

LED - 5. Raspberry Pi 4 + DietPi Buster + Electrodragon HAT - Part 1

LED-11. Make a very big size 384 X 512 RGB Matrix #6(final)