Click to follow us
Get more IIOT tips~
Huangke Industrial Raspberry Pi
High Performance
Multiple Interfaces
Wide Temperature – Adapt to Harsh Environments
★★★★★
Huangke · Industrial IoT
1. Introduction
OPC UA is an open communication protocol based on Ethernet, also known as a popular communication protocol in Industry 4.0, aimed at bridging OT and IT networks, providing the possibility for devices in different networks to access and operate each other with a unified data architecture and method, while offering sufficient scalability and security support for the constantly changing communication needs of different industries.
The definition of OPC UA is entirely at the application layer of the TCP/IP five-layer model, meaning that implementing this protocol does not require dedicated chips or any physical layer changes.
So, how can we enable existing devices to support the OPC UA protocol? This article takes Huangke Industrial Raspberry Pi as an example to share the method of implementing OPC UA protocol support.
The Huangke Industrial Raspberry Pi RevPi does not come with the OPC UA protocol built-in, but we can use the relevant SDK to build a simple server and then output data from industrial protocols (such as Modbus) in OPC UA format.
Huangke · Industrial IoT
2. Preparation Work
Hardware
One PC;
One Huangke Industrial Raspberry Pi;
24V DC power supply;
Network cable;
Software
On PC:
SSH connection software, such as putty;
Chrome-based browser;
Huangke · Industrial IoT
3. Operation Steps
1. Power on the Industrial Raspberry Pi, connect the network cable, and use Advanced IP Scanner or similar software to scan for the IP address of the Raspberry Pi, or check the DHCP client list in the router’s backend to determine the IP address of the Raspberry Pi.
2. Use SSH software to establish a connection with the Industrial Raspberry Pi, the login account is pi, and the default password will be printed on the sticker on the side of the machine, with each machine having a different default password. If the screen appears as shown below, it means the login was successful. The terminal interface later in this article, unless otherwise specified, comes from the SSH session with the Industrial Raspberry Pi.
3. For simplicity, this article uses the open-source OPC UA SDK in Python as an example, assuming the reader has a basic understanding of Python. If not, please learn the basic syntax of Python first. The GitHub repository link for this SDK is as follows:
https://github.com/FreeOpcUa/python-opcua
The python3 environment is pre-installed in the Industrial Raspberry Pi, and we just need to install the SDK library, with the installation command as follows:
pip3 install opcua
The installation speed depends on the network environment. If it is too slow or there are errors while downloading from the network, you can consider changing pip3 to a domestic source before installing this library; this will not be explained here. The installation result is as follows:
4. Next, open the IP address of the Raspberry Pi in the browser and log in. This time the username is changed to admin, and the password is the same as the SSH password, written on the machine’s sticker.
5. After logging in, configure Modbus (this article still takes ModbusTCP protocol as an example). For configuration methods, please refer to previous articles or contact Guangzhou Huangke for relevant guiding documents; this will not be repeated here. The entire connection configuration is shown in the table below:
6. After the configuration is saved and effective, return to the SSH interface and use the following command to verify whether the Modbus connection has been successfully established:
piTest -r Input_Word_1
If the configuration is correct, this command should continuously read the value stored in the Input_Word_1 variable, which is the value of the Holding Registers at the Modbus slave variable address 1, until Ctrl+C is pressed, as shown in the figure below.
Input_Word_1 can also be replaced with other RevPi variable names for reading tests. Input and output variables can be read using this command; we can also try writing variables using the following command:
piTest -w Output_Word_2,666
Output_Word_2 can also be replaced with other RevPi variable names, but this command is only meaningful if the variable is a writable variable. Note that the comma between the variable name and the value to be written is in English, not in Chinese.
7. So far, the Modbus connection part has been tested. We are ready to start Python programming. First, we need to confirm the relevant information of the variables we set earlier by executing the following terminal commands.
piTest -v Input_Word_1
piTest -v Input_Word_2
piTest -v Input_Word_3
piTest -v Output_Word_1
piTest -v Output_Word_2
These return messages are very important. Taking the first one as an example, we can analyze it line by line. First, the first line obviously just returns our variable name; the second line, offset: 11, means that the variable’s offset is 11. We know that in Linux, everything is a file, and our RevPi variables also exist in files (specifically, the complete path of the image file is /dev/piControl0). The offset of the first byte of the variable, or the distance from the beginning of the file, is the offset – 11 (bytes) here; the third line, length: 16, this length unit is bit, since Modbus’s Holding Registers are 16-bit registers, the stored variable also uses a length of 16 bits; the fourth line, bit: 0, this is actually the bit offset. For some variables (such as DIO channel variables), the length is only 1 bit, so at this time, we need to know which bit it is. For variables with a length not equal to 1, the bit offset is always 0.
8. With the file offset and file path, we can use standard Python file IO to extract data. Taking the variable Input_Word_1 as an example, we just learned that its offset is 11 and its length is 16 bits. Therefore, what we need to do is to open the file, move the cursor to position 11, read a length of 2 bytes (16 bits), and then parse these two bytes in little-endian mode (all variable values longer than 1 byte are stored in little-endian mode). After reading, remember to close the file. The Python code is as follows:
f = open(“/dev/piControl0″,”rb”,0)
f.seek(11)
tempVar = int.from_bytes(f.read(2),byteorder=’little’,signed=False)
f.close()
print(tempVar)
9. We have successfully used Python to obtain the value of the RevPi variable. Next, we will write the code for the OPC UA server. In fact, the entire code mainly refers to the minimal server example code in the GitHub project, with the source code link:
https://github.com/FreeOpcUa/python-opcua/blob/master/examples/server-minimal.py
The main change is that we need to use add_variable multiple times to create several variable nodes to hold the values of our RevPi variables, and then in the main loop, periodically perform file IO to sample the current values of each RevPi variable and assign them to the corresponding variable nodes. The complete source code is as follows; please change the URL in set_endpoint to the IP of your RevPi and the port number you want to use. The offsets in addrList should also be modified according to the actual situation:
……
Due to space constraints, the remaining code is not placed here. Please contact the Huangke Industrial IoT team to obtain it.
10. Copy the above code into a terminal file editor (for example, nano or vi), name it as a py script, for example, modbus2opcua.py, and then give the script execution permission with the following command.
chmod +x modbus2opcua.py
Then you can execute this script, as shown in the figure:
11. At this point, open the OPC UA client. This article takes Matrikon OPC UA Explorer as an example (this software is free; you can contact Guangzhou Huangke for the installation package) to connect to the OPC UA server created by the Raspberry Pi and add the variables to the monitoring list. You can now access the data read from Modbus.
12. At this point, if we modify the values in Modbus, the OPC UA client can also synchronize to obtain the changed values.
13. var4 and var5 read the last value written to the variable on the Raspberry Pi, so we try to write a new value to var4 in the terminal to verify the write.
Verification successful!
Huangke · Industrial IoT
4. Conclusion
The Industrial Raspberry Pi can help us turn different industrial data and IO point values into unified Industrial Raspberry Pi variables. What we need to do is essentially to add the corresponding variable nodes in the address space before creating the OPC UA server, and then bind the values of the Industrial Raspberry Pi variables to the values of the variable nodes.
The simplicity of Python allows us to create a simple OPC UA server from scratch with about fifty lines of code, but the functionality is largely limited, and various node attribute settings are incomplete. Moreover, Python’s runtime efficiency and performance are low, making it suitable only for a small number of points and low-speed refreshing for principle verification, personal learning, and other application scenarios.
If in a real industrial production environment that pursues stability and efficiency, I recommend using a commercial C/C++ language SDK, which is currently available for sale from Guangzhou Huangke. If needed, please contact us; this commercial SDK has been successfully cross-compiled and tested for operation in the Huangke Industrial Raspberry Pi environment.
To learn more technical cases, please visit the Huangke official website: https://www.hohuln.com/opc-ua-solution/
Huangke–Industrial IoT
Huangke is a high-tech company with over 3 years of experience in the industrial IoT IIoT industry. Huangke collaborates with top companies in the world, including EXOR, Eurotech, Unitronics, Matrikon, KUNBUS, VDOO, Esper, etc., providing advanced high-end Industrial 4.0 industrial touch screens, high-end edge computers, IoT development frameworks, PLC and HMI integrated machines, OPC UA, industrial-grade Raspberry Pi, VTSCADA, VDOO device security analysis and protection platforms, and one-stop management platforms for Android devices and other solutions. All members of the IoT division are professionally trained and certified, with an average of over 3 years of technical experience, consistently earning excellent customer reputation. We actively participate in industry associations and have made significant contributions to promoting the popularization of advanced technologies. To date, Huangke has provided various solutions from hardware to software for many users in the industry and has participated in and assisted many OEM equipment development and migration projects, as well as intelligent factory and Industry 4.0 upgrade projects for end users.
Appendix: Industrial Raspberry Pi Application Case Collection
Case 1: Vibration Monitoring of Small Hydroelectric Turbines
Case 2: Intelligent Riveting Tools
Case 3: Automation of Post-Printing Equipment
Case 4: CloudRail.Box Helps Quickly Realize IIOT
Case 5: Railway Water Injection System Transformation (Includes Practical Teaching Video)
Case 6: Application in the Retail Industry
Case 7: Machine Data Recording
Case 8: Water Treatment Equipment
For more cases, please click here
Click the “Read the original text” below to see more
Leave a Comment
Your email address will not be published. Required fields are marked *