
Considering implementing an IIoT System for your factory? 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
- Siemens Simatic IoT2040
- Ultrasonic Ranger (SRF485WPR)
- Power Supply (12V DC for Ranger and 24V DC for Simatic)
- 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.

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 Credit: SRF485WPR

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 complement (bitwise negation) of the sum of all previous bytes (not counting the break).
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 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.

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 root@192.168.200.1
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.

Create Data Source as shown in Figure 8.

Here’s the result:

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.
Originally published May 26, 2019. Updated January 29, 2020.