LED - Manage your LED related python codes using Node-RED
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.
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.
댓글
댓글 쓰기