Designing and Implementing an IIoT System for Smart Factories

This is a design and implementation of an IIoT ecosystem for smart factories. The design is based on Siemens Simatic IoT2040, an intelligent industrial gateway that is connected to MODBUS sensors publishing data onto Network Platform for Internet of Everything (NETPIE).

11087
Designing-and-Implementing-an-IIoT-System-for-Smart-Factories
Illustration: © IoT For All

This article explains how to read data from Ultrasonic Ranger using Simatic IoT2040. Python acts as a communication bridge between Ranger and Simatic. The received data is then sent to NETPIE using​ NETPIE client library​ for Python.

Requirements

  1. Siemens Simatic IoT2040
  2. Ultrasonic Ranger (SRF485WPR)
  3. Power Supply (12V DC for Ranger and 24V DC for Simatic)
  4. Python 3

Ultrasonic Ranger

The Ultrasonic Ranger is used to detect the distance of the target object by transmitting a sound signal that reflects back to the transmitter after hitting the target. Ultrasonic sensors are used in a wide variety of applications both indoor and outdoor. The Ultrasonic sensor comes with 10 pins used for power supply and RS485 output signal as shown in Figure 1.

Ultrasonic Ranger image
Figure 1. Ultrasonic Ranger
Image Credit: SRF485WPR

Ultrasonic Ranger RS485 Communication

Data is read by sending frames of data to the module and then listening for the response. Each Ultrasonic Ranger has a unique 24-bit address written on the front side of the module as shown in Figure 2. The data frame you send to the SRF485WPR is shown in Figure 3.

Image of Ultrasonic Ranger address on its body
Figure 2. Ultrasonic Ranger address on its body
Image Credit: SRF485WPR
Data Frame Form chart
Figure 3. Data Frame Form
Image Credit: SRF485WPR
  • Break – It is defined as a delay in time between two consecutive command requests.
  • Command – By sending this, Ultrasonic Ranger gives back its response.
  • Address H, M, L – It is a 24-bit address of the module.
  • Data – It is the data that you wish to send to the module, zero (0x00) if nothing is required by the command.
  • CheckSum – It is the 1’s compliment (bitwise negation) of the sum of all previous bytes (not ounting the break).

Commands

Image of a chart of Ultrasonic Ranger commands
Figure 4. List of Ultrasonic Ranger commands
Image Credit: SRF485WPR

Check Sum Calculation

For example, let us consider the frame that should be sent to the Ultrasonic Ranger is represented in Figure 5. Checksum is calculated for entire frame, excluding break as described below

image of a chart that is an example of data format
Figure 5. Example of Data Format
Image Credit: SRF485WPR
  • The Check Sum is calculated as the low byte of ~(0x51+0x01+0x89+0xAB+0x00).
  • Adding all the terms would result in 0x51+0x01+0x89+0xAB+0x00 = 0x0186.
  • The bitwise notation of 0x0186 is 0xFE79.
  • We use the low byte of that, 0x79

Connections

Preparing Siemens Simatic IoT2040

Siemens Simatic IoT2040 works based on Yocto Linux that needs to be installed on an SD-card and should be inserted in Simatic. The Yocto Linux can be downloaded from the Siemens website. After downloading the image, insert the SD-card into the SD-card slot of the Linux computer and follow the instructions below.

  • On Linux computer, open the Terminal (ctrl+alt+t) and go to the location where the Yocto Linux zip files are downloaded using the command: cd Downloads. In this case, the zip files are located in Downloads.
  • Unzip the folder using the following command:

sudo unzip Example_Image_V2.2.0.zip

  • Verify the location of SD-card by running the command below:

df -h

  • Unmount the SD-card by running the command below. Replace “mmcblk0” with the SD-card location shown by your computer:

unmount /dev/mmcblk0

  • Burn the Yocto Linux image onto the SD-card, using this command:

sudo dd bs=1M if=name_of_image of=SD card_location

  • Once the image is installed onto the SD-card, place the SD-card into the SD-card slot of Simatic as shown in Figure 6.
Picture of the SD Card installation on Simatic IoT2040
Figure 6. SD Card installation on Simatic IoT2040
Image Credit: IBHSoftec

Simatic has two Ethernet ports named X1P1LAN and X2P1LAN. The first Ethernet port i.e. X1P1LAN has the default DHCP address 192.168.200.1. Using the Secure Shell (SSH), you can communicate with Simatic from your Linux computer. To achieve this, connect one end of the Ethernet cable to your computer and another end of the Ethernet cable to the X1P1LAN port of Simatic. Log in to the Simatic through the SSH.

ssh [email protected]

Installing Pip on Simatic

For reading the data from the Ultrasonic Ranger using Simatic, Python is used. By default, Python is installed on Yocto Linux. Some Python libraries that have the capability of sending data frames serially need to be installed on Simatic. For this purpose, pip is used. Pip is a package management system used to install and manage Python libraries.

  • To install pip on Simatic, the /etc/opk /opkg.conf file should be edited and add below lines for the repositories:

src iotdk-all http://iotdk.intel.com/repos/2.0/iotdk/all

src iotdk-i586 http://iotdk.intel.com/repos/2.0/iotdk/i586

src iotdk-quark http://iotdk.intel.com/repos/2.0/iotdk/quark

src iotdk-x86 http://iotdk.intel.com/repos/2.0/iotdk/x86

  • Next, the /etc/opkg/arch.conf ​file should be edited and the following lines are inserted:

arch i586 12

arch quark 13

arch x86 14

  • Next, update the package database:

opkg update

  • Now we can install a selection of useful packages:

opkg install ca-certificates python-json python-io python-re python-xmlrpc python-ctypes openssh-sshd

curl — insecure -L “https://bootstrap.pypa.io/get-pip.py” > get-pip.py

python get-pip.py

  • With pip configured, we can now install the python libraries:

pip install pyserial

pip install microgear

pip install serial

Creating the Python file

With all the necessary libraries installed, we will now start to write a Python code that reads the data from the Ultrasonic Ranger. For this, create a Python file named ranger as described below

nano ranger.py

  • Make the file executable as described below:

chmod +x ranger.py

  • Run the file using the command below:

./ ranger.py

The complete source code can be found here.

Network Platform for Internet of Everything (NETPIE)

NETPIE platform is a cloud-based platform-as-a-service that facilitates interconnecting IoT devices (“things”) together in a most seamless and transparent manner possible by pushing the complexity of connecting IoT devices from the hands of application developers or device manufacturers to the cloud. On NETPIE, create Application and generate device key. “board” ​is the Application ID created for this demonstration as shown in Figure 7.

Image of the creation of an application ID
Figure 7. Creating application ID on NETPIE

Create Data Source as shown in Figure 8.

Image of screen of how to create the data source
Figure 8. Creating Data Source

Here’s the result:

Image of the result displaying the target distance detected by Ranger on NETPIE
Figure 9. Displaying the target distance detected by Ranger on NETPIE

Conclusion

The use of Siemens Simatic IoT2040 as a gateway device that receives data from SRF485WPR was discussed in this article. With Simatic IoT2040, handling the production data in industries has become more efficient, which opens paths towards the Industrial Internet-of-Things (IIoT) applications.