[Mill MYC-J1028X development board trial] Use Node-RED to transform the MYC-J1028X development board into an industrial control gateway
[Copy link]
This post was last edited by HonestQiao on 2022-10-2 10:59
This article is long and involves many knowledge points. You need to read it patiently. The following is the directory of specific contents:
- Zero: Introduction
- 1. Install node-red
- 2. System command control LED
- 3. LED permission settings
- 4. Node-RED control system LED
- 5. System command control GPIO
- 6. Node-RED controls GPIO
- 7. Use the S3 button on the development board
- 8. Use S3 button to control LED in Node-RED
- 9. Node-RED uses the I2C interface to read the SHT30 temperature and humidity sensor data
- 10: Node-RED uses the dashboard to display temperature and humidity sensor data
- XI: Summary
Zero, Introduction:
If you are interested in checking out the official interface of the Mir MYC-J1028X core board and development board:
You will find that the introduction has a strong industrial flavor. Industry! Industry! Industry!
Today, we will build a Node-RED environment on the MYC-J1028X development board and transform the development board into an industrial Internet of Things control gateway.
Many people understand or know Node-RED, starting from smart home or small IoT control. Some people think it is just a similar graphical interface programming tool.
In fact, in the industrial Internet, Node-RED can be used for data collection, correlation trigger control, instrument presentation, etc., and can achieve good results with low-cost investment.
1. Install node-red
To install node-red, refer to the official documentation: https://nodered.org/docs/getting-started/raspberrypi
Although this document is for Raspberry Pi, it can also be used as a reference for Debian systems.
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
After executing the above command, the installation script will be automatically downloaded and the installation process will be started. For any questions during the installation process, just select y.
The installation process may take a long time, so make a cup of tea or coffee and wait:
During the first installation, you may encounter the inexplicable problem that the Node-RED core is not installed. How can you play if the core is not installed?
No matter what, just execute the installation command again and it will be fine:
If it prompts that there is a problem with the shortcut, ignore it for now, it will not affect the use.
Because it is the first installation, execute node-red admin init to initialize:
After initialization is complete, you can execute node-red start to start Node-RED.
After normal startup, you can access it through the URL http://development board IP:1880/:
Enter the account password set during initialization. After logging in, follow the settings below to switch to the Chinese interface:
After the settings are completed, you need to refresh the page to make them take effect.
2. System command control LED
Now, we can start controlling peripherals in Node-RED.
The first control logic is to turn on the light. If we can turn on the light, we are halfway successful.
By looking at the official manual, you can find out:
For this LED, the system has done device association and cannot be controlled through GPIO.
We can first use the above instructions to test whether we can control the D22-LED of the system.
echo 0 | sudo tee /sys/class/leds/d22/brightness
echo 1 | sudo tee /sys/class/leds/d22/brightness
3.LED permission settings:
By looking at the documentation of Node-RED, we can learn that there is an Exec Node that can be used to execute system commands. We can use it to execute the instructions in the manual above to control D22.
To enable Node-RED run by ordinary users to control the system LED, you need to add corresponding permission processing rules in udev, otherwise you will be prompted that you do not have permission when operating.
sudo groupadd -f -r leds
sudo usermod -a -G leds $USER
sudo vim /etc/udev/rules.d/99-leds.rules
SUBSYSTEM=="leds", ACTION=="add", RUN+="/bin/chgrp -R leds /sys%p", RUN+="/bin/chmod -R g=u /sys%p"
SUBSYSTEM=="leds", ACTION=="change", ENV{TRIGGER}!="none", RUN+="/bin/chgrp -R leds /sys%p", RUN+="/bin/chmod -R g=u /sys%p"
After adding the last two lines of settings above to /etc/udev/rules.d/99-leds.rules, you need to restart the system for them to take effect.
Friendly reminder: Similar to setting ordinary user permissions in udev, there will be many more times in the future, so be sure to set them carefully.
After rebooting (sudo reboot), you can test using normal user privileges to operate the LED:
echo 0 | tee /sys/class/leds/d22/brightness
echo 1 | tee /sys/class/leds/d22/brightness
4. Node-RED control system LED
First, in the Node-RED interface, refer to the figure below, add two inject nodes, and then add two Exec Nodes.
For the command part of Exec Node, refer to the figure below to fill in. Remember that one is echo 0 and the other is echo 1, which correspond to turning off and on the LED.
The settings of the two injection nodes are as follows:
The above logic means:
- injection 0 starts executing at the 0th second, triggering the Exec corresponding to echo 0 and turning off the LED
- injection 1 starts executing at the first second, triggering the Exec corresponding to echo 1 and lighting up the LED
- At the 2nd second, injection 0 is executed again, turning off the LED
- At 1+2 seconds, injection 1 is executed again, lighting up the LED
- Because the periodic execution is set, the above two triggers are executed alternately to achieve the effect of turning the LED on and off.
5. System command control GPIO:
The official manual explains how to operate GPIO:
Combining the development board and the schematic diagram, we can see that J18 is the pin area that we can use conveniently:
The corresponding relationship of GPIO available on J18 is as follows:
- J18 - Pin 32 - GPIO3_5: 421
- J18 - Pin 22 - GPIO3_6: 422
- J18 - Pin 29 - GPIO3_8: 424
- J18 - Pin 31 - GPIO3_9: 425
- J18 - Pin 33 - GPIO3_10:426
- J18 - Pin 36 - GPIO3_12:428
Similar to the system LED, if you want to control GPIO under ordinary users, you also need to make some settings in udev. The specific settings are as follows:
sudo groupadd -f -r gpio
sudo usermod -a -G gpio $USER
sudo vim /etc/udev/rules.d/99-gpio.rules
SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", RUN+="/bin/chgrp -R gpio /sys/class/gpio/export /sys/class/gpio/unexport", RUN+="/bin/chmod -R g=u /sys/class/gpio/export /sys/class/gpio/unexport"
SUBSYSTEM=="gpio*", KERNEL=="gpio*", ACTION=="add", RUN+="/bin/chgrp -R gpio /sys%p", RUN+="/bin/chmod -R g=u /sys%p"
SUBSYSTEM=="gpio*", KERNEL=="gpio*", ACTION=="change", ENV{TRIGGER}!="none", RUN+="/bin/chgrp -R gpio /sys%p", RUN+="/bin/chmod -R g=u /sys%p"
Write the last three lines of the above content to /etc/udev/rules.d/99-gpio.rules, and then restart the development board to take effect.
After the restart is complete, we can now test it in the command line.
First, according to the schematic diagram, connect the LED that can be driven by 3.3V to the GPIO3_9 pin.
Then test it using the following command:
echo 425 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio425/direction
for i in {1..100};do
echo $((i%2)) > /sys/class/gpio/gpio425/value
sleep 1
done
After the command is executed, the LED connected to the GPIO3_9 pin will blink once per second.
After the test control is successful, we can control it in Node-RED.
6. Node-RED controls GPIO
To control GPIO in Node-Red, you can install the OPI-GPIO plugin. Follow the steps below to install it:
After the installation is complete, Orange Pi will appear in the component selection bar on the right. Select the output pin, then connect the injection 0 and 1 set previously to the newly added control, and then follow the steps below to set the corresponding GPIO port number.
Because GPIO3_9 corresponds to 425, you can just fill in 425 for pin. However, after filling in the information, when you open Orange Pi again, the interface will return to the initial state, so be sure to fill in the information carefully.
After filling in the fields, click Deploy in the upper right corner. The LED connected to GPIO3_9 will start blinking, in sync with the D22-LED on the board.
7. Use of the S3 button on the development board:
On the development board, there is a button S3 that can be programmed by the user:
The specific location is as follows:
The S3 button uses the system /dev/input/event0 to monitor reception. For ordinary users, the following settings are required:
sudo usermod -a -G input $USER
sudo vim /etc/udev/rules.d/99-event.rules
KERNEL=="event[0-9]*", SUBSYSTEM=="input", TAG+="uaccess"
After setting, restart to take effect.
After restarting, use the evtest command to test:
After pressing the S3 button, you can receive the button message.
In order to better receive key information, we need to use Python to detect it, which requires the python3-evdev library. Install it first:
Then, use the following python script to test:
from evdev import InputDevice
from select import select
dev = InputDevice('/dev/input/event0')
while True:
r,w,x=select([dev],[],[])
for event in dev.read():
print(event)
Press the S3 button and release it, and you will receive the message:
It should be noted that the S3 button uses system Event reception, and is triggered once when pressed and released.
Press to correspond:
- event at 1664670749.634839, code 02, type 01, val 01
- event at 1664670749.634839, code 00, type 00, val 00
Release correspondence:
- event at 1664670750.350823, code 02, type 01, val 00
- event at 1664670750.350823, code 00, type 00, val 00
We can improve the program to directly output 1 when pressing the key and output 0 when releasing it:
from evdev import InputDevice
from select import select
dev = InputDevice('/dev/input/event0')
while True:
r,w,x=select([dev],[],[])
for event in dev.read():
if event.code == 2 and event.type == 1:
print("value: %d" % event.value)
Press and release the S3 button, and you will receive the following message:
At this point, we are ready to provide the key value to Node-RED for use.
8. Use the S3 button to control the LED in Node-RED:
To get the key value provided by Python in Node-RED and then control the LED, you need to use the following logic:
The control logic in Node-RED is as follows:
A detailed description is given below.
First, we adjust the above monitoring script again to make the logic of monitoring buttons meet actual needs, namely:
- According to the number of key presses, odd-numbered key presses output 1 (for lighting the LED), and even-numbered key presses output 0 (for turning off the LED)
- Default output 0
- Note: Here, 1 or 0 is output according to the odd or even number of key presses, not according to the number of times the key is pressed or released.
The specific code is as follows:
from evdev import InputDevice
from select import select
dev = InputDevice('/dev/input/event0')
keypress_times = 0
print(keypress_times % 2)
while True:
r,w,x=select([dev],[],[])
for event in dev.read():
if event.value == 1:
keypress_times = keypress_times + 1
print(keypress_times % 2)
Save the above code to /home/HonestQiao/Projects/event/evdev_key.py (the specific path should be set according to your actual situation), and then run the test:
After the test is correct, you can actually call it in Node-RED.
To start running the above monitoring script in Node-RED, you need to use the node-red-node-daemon plug-in, just install it:
After clicking, use the daemon control and set it up as follows:
Because the output of evdev_key.py is [0 or 1 with a carriage return], Node-RED needs to convert it before it can be used.
Follow the steps below to add a conversion control and set the conversion logic:
Then, add GPIO3_10 corresponding to pin 426 and associate it with the digital control:
I forgot to mention that I connected the second LED to the pin corresponding to GPIO3_10 on J18:
Then, add a debug control so that you can view the data information of the intermediate process:
Connect the three outputs of S3 and the output of the Convert Digital control to the debug control.
After clicking Deploy, you can see the debug output information on the right.
Try pressing the button and you will see that the string information output by the script has been converted into digital information, and the yellow LED connected to GPIO3_10 can also be controlled normally.
9. Node-RED uses the I2C interface to read the SHT30 temperature and humidity sensor data:
The previous parts are all about LED control, GPIO control, and button reading, which are relatively simple.
Next, let's take a slightly more advanced example, which is to use the I2C interface to read the SHT30 temperature and humidity sensor.
In the circuit schematic diagram we have seen before, there is an I2C interface on the J18 interface.
Connect the SHT30 of the I2C interface to the corresponding interface of J18:
To use the i2c interface under normal user privileges, we need to make some settings in udev:
sudo vim /etc/udev/rules.d/99-i2c.rules
KERNEL=="i2c-[0-9]*", GROUP="i2c"
Write this line to /etc/udev/rules.d/99-i2c.rules and reboot to make it take effect.
After the restart is complete, test whether SHT30 can be found under the command:
First use the i2cdetect -l command to check the available i2c interfaces:
As you can see from above, i2c-0 and i2c-1 are available for use.
Then, use i2cdetect -y 0 and i2cdetect -y 1 to view the i2c mounted devices:
In i2c-1, there is a device with address 44, which is the default i2c communication address of SHT30.
You can try disconnecting the SHT30 cable and then use i2cdetect -y 1 to see if there is any difference.
In Node-RED, to read the data of SHT3X, you can use the node-red-contrib-sht plugin:
Then, follow the steps below to add an injection control for periodic triggering, then add a SHT3X control to read SHT3X data, and finally add a debug module to output debug information:
After the logic setting is completed, click Deploy in the upper right corner, and the actual temperature and humidity information will be printed out in the debugging area.
If you blow into the temperature and humidity sensor, you will find that the read value has changed.
10: Node-RED uses the dashboard to display temperature and humidity sensor data:
Another fascinating and powerful thing about Node-RED is that it not only allows you to use a graphical interface to design the control interaction process of IoT devices, but also allows you to use the dashboard very conveniently and provide a data viewing interface.
Next, based on the temperature and humidity sensor read in the previous step, I will make another one that can view the accessed dashboard on the mobile phone interface.
First, install a dashboard control:
There are many options to choose from. I directly installed the first one, dashboard-evi. You can also try the others.
Then, on the SHT3X space, hang two changes to convert the temperature and humidity values:
The above two changes, one is used to convert the temperature value to only one decimal place, and the other is used to convert the humidity value to no decimal place.
As for the temperature and humidity values, each has a gauge to display the final data. The content can be set as follows:
Temperature gauge:
Humidity gauge:
Note that you must set the Range, that is, the value range, otherwise it may not be displayed normally.
When setting a gauge for the first time, such as the temperature gauge above, click point (4) to enter and set the group node and tab node as follows:
After the above settings, the temperature and humidity values can be output to the dashboard.
For a better effect, I added a current date and time conversion and displayed it using a text control.
First follow the steps below to add a function control, and do not write the conversion code:
function formatDate(value) {
var date = new Date(value);
var y = date.getFullYear(),
m = date.getMonth() + 1,
d = date.getDate(),
h = date.getHours(),
i = date.getMinutes(),
s = date.getSeconds();
if (m < 10) { m = '0' + m; }
if (d < 10) { d = '0' + d; }
if (h < 10) { h = '0' + h; }
if (i < 10) { i = '0' + i; }
if (s < 10) { s = '0' + s; }
var t = y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;
return t;
}
return { payload: formatDate(msg.payload) };
The formatDate() function is a standard js function that converts the current timestamp into the YYYY-mm-dd HH:MM:SS format.
This is also a powerful aspect of Node-RED. In addition to the pre-written controls, you can also use scripts to process the intermediate process.
After setting the date and time conversion, hang a text control behind it for display:
After all the designs are completed, click Deploy in the upper right corner and you can view them on your mobile phone.
On your mobile phone, visit the URL http://development board IP:1880/ui and you will see the following interface:
11. Summary:
This sharing is based on the Mir MYC-J1028X development board, combined with Node-RED, to transform it into an industrial control gateway.
Seeing this, it seems that a lot of content has been shared, but the part involving Node-RED is less than one ten-thousandth of all the functions of Node-RED, and they are all very basic uses.
When using the Mil MYC-J1028X development board, less than one percent of its actual functions are used.
Node-RED has many powerful functions that can help us quickly obtain data from external devices, write appropriate logical processing flows to organize data, present data, or control external devices.
All of this can be accomplished without requiring any special programming skills.
Based on the powerful computing power and rich peripheral interfaces of the Mir MYC-J1028X development board, combined with the industrial control gateway built by Node-RED, it can be fully applied in actual occasions and play a significant role.
Deploying it at industrial sites to collect, store, process and analyze data at the edge of the network can reduce the pressure on the cloud and data centers.
This system can simplify communication and control through flexible I/O, actively collect, analyze, filter and aggregate data to visualize field data and control logic, which can facilitate predictive maintenance and carry out real-time data processing and decision-making.
|