Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

The Raspberry Pi, designed for learning computer programming education, is a microcomputer the size of a credit card, with a system based on Linux. Since its launch, it has been favored by many computer enthusiasts and makers. Despite its small size, it is incredibly powerful!

So what sparks can be created when the Raspberry Pi collides with the most famous product in industrial control, the Siemens PLC?

This article will briefly introduce how to use Node-Red to enable S7 communication between the Raspberry Pi and Siemens PLC, and how to read and write PLC data via the MQTT server on Alibaba Cloud.

This article will be divided into four parts:

1. Configuration of Node-Red nodes in Raspberry Pi;

2. Establishing the MQTT server on Alibaba Cloud;

3. Configuration on the PLC side;

4. Testing;

Software and hardware required for this article:

1. Raspberry Pi 3B+;

2. Siemens PLC S7-1500;

3. TIA Portal V15;

4. Putty;

5. MQTT.fx;

6. Alibaba Cloud server

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration
1. Configuration of Node-Red nodes in Raspberry Pi;

Since 2015, Node-Red and Node.js have been included by default. To prevent using an older version of Node-Red, you can connect to the Raspberry Pi using Putty and run the following command to update:

update-nodejs-and-nodered

The next step is to install npm, which makes it easier to add nodes to Node-Red on the Raspberry Pi. The steps are as follows:

Before installing npm, run the following command to update the Raspberry Pi system to the latest version, then restart the Raspberry Pi:

sudo apt-get update && apt-get upgrade

After restarting, run the following command to install npm:

sudo apt-get install npm

At this point, the preliminary preparation work on the Raspberry Pi is basically complete. Connect to the Raspberry Pi using Putty and run Node-Red;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Then enter http://{Raspberry Pi’s address}:1880/ in the browser to access the Node-Red editing interface, as shown in the following image:

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Since S7 is a proprietary protocol of Siemens PLC, Node-Red does not come with this node by default. You need to install it yourself (if npm has not been installed previously, installing new nodes will be quite troublesome). As shown in the image, open the node management in Node-Red settings, search for S7, and install the second node;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

After installation, you will see the S7 node appear on the left;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

At this point, the setup of Node-Red is basically complete. Next, we will do some simple programming. The pros and cons of Node-Red programming will not be elaborated here. If you are not familiar with Node-Red programming, you can refer to the link: https://zhuanlan.zhihu.com/p/27546140;

The nodes that will be used this time include S7 in; S7 out; MQTT in; MQTT out;

First, we will set up the S7 in node:

Raspberry Pi, Siemens PLC, and Alibaba Cloud IntegrationRaspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Fill in the PLC’s IP address in the IP Address field, the port defaults to 102, and select Rack/Slot for the mode. The default rack number for the PLC is 0, and the slot number is 1;

Next, fill in the variables to be accessed, corresponding to the settings in TIA Portal;

Raspberry Pi, Siemens PLC, and Alibaba Cloud IntegrationRaspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Next, we set up the S7 out node;

The Out node setup is similar to the In node. Here we choose to modify the variable to be 1.

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

At this point, the S7 node setup is complete. Next, we will set up the MQTT in and MQTT out nodes as shown in the following images;

First, we set up the Out node:

Raspberry Pi, Siemens PLC, and Alibaba Cloud IntegrationRaspberry Pi, Siemens PLC, and Alibaba Cloud IntegrationRaspberry Pi, Siemens PLC, and Alibaba Cloud Integration

In the above image, fill in the public IP of Alibaba Cloud for the server IP, the port number is 61613 (the TCP port number), the client ID can be left blank, and the username and password are the credentials for the MQTT server deployed on Alibaba Cloud (default username is admin, password is password);

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

In the topic field, fill in the topic you want to subscribe to from the MQTT server;

All the nodes above have been configured. Connect the nodes and click the deploy button in the upper right corner to deploy Node-Red to the Raspberry Pi;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

The first part ends here.

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration
2. Establishing the MQTT server on Alibaba Cloud;

There are many MQTT servers available, and you can even write your own MQTT server. This article uses the Apollo server. Specific configuration for the Apollo server on Windows can refer to the following link:

https://blog.csdn.net/eren666/article/details/79459552

Note: The operating system for this Alibaba Cloud instance is Windows Server 2012, and the version downloaded is apache-apollo-1.7.1, with Java version 8 (the versions of Apollo and Java must match, otherwise errors will occur).

After configuration, it looks like the following image:

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

The MQTT server on Alibaba Cloud is now deployed;

The second part ends here.

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration
3. Configuration on the PLC side;

The configuration on the PLC side is relatively simple, as S7 can perform one-way communication, and no programming instructions are needed on the PLC side; you only need to:

① Set the PLC properties and check the box for “Allow PUT/GET communication access from remote objects”;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

② Create corresponding variables in the DB block and uncheck the “Optimized block access” option in the DB block properties;

Raspberry Pi, Siemens PLC, and Alibaba Cloud IntegrationRaspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Download the CPU configuration to the S7-1511 PLC, and the configuration on the PLC side is complete;

The third part ends here.

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration
4. Testing

The entire testing process is shown in the following image:

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

First, the PLC sends data to the Raspberry Pi, modifying the variables in the watch table. When the variable value changes, a message will be sent to the Raspberry Pi, which in turn sends the message to the MQTT server. The MQTT.fx client that subscribes to the message will also receive it;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Then, through the MQTT.fx client, publish the topic “raspiget” to modify the internal data of the PLC;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

The published data is 4, and we can see that variable 1 in the PLC has been changed to 4;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Finally, we can check the specific publish-subscribe count, data size, and connection count on the MQTT server;

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration

The fourth part ends here.

Raspberry Pi, Siemens PLC, and Alibaba Cloud Integration
Finally

The content of this article may not be detailed enough, and some details are worth pondering. This test is actually not a very complex function, but it is just an example. Besides S7, there are many other options available, such as TCP, Modbus TCP, and OPC, etc. When replacing the Raspberry Pi with Siemens’ IoT gateway IOT2040 and replacing the connected Alibaba Cloud with Siemens’ Mindsphere, it may yield even more exciting content!

The traditional industrial control devices and popular IT technologies are no longer in a fragmented relationship but are increasingly converging. The Internet of Things that everyone is pursuing may soon become popular in the industrial control field!

Source: Siemens Technical Support Engineer, please delete if infringing

Stay tuned for the next exciting article:

Seven Precautions After Long-Term Shutdown of Motors

Leave a Comment