[Required Skills] Do it yourself-based on C# to realize remote access to Siemens PLC by mobile APP (source code attached)

 

For the source code of the MQTT server and MQTT client involved in the article, you can click on the official account above and send the keyword after paying attention: MQTT communication source code .

With the advent of the 5G era, the Internet of Things has begun to accelerate and gradually integrate into our lives. The interconnection of all things will no longer be just a matter of paper.

Nowadays, major Internet of Things platforms are emerging in an endless stream, and they seem to be far away from us. In fact, as long as we dare to research and dare to discover, the interconnection of everything is within easy reach.

Today, I will introduce to you how to build a simple and practical IoT platform framework without the help of a third-party IoT platform as an ordinary .NET developer.

1. Internet of Things Framework

This Internet of Things framework is based on the upper computer framework and combined with the Internet of Things communication protocol MQTT. The host computer itself integrates functions such as collection, archiving and logging. On this basis, it only needs to connect to the MQTT server and publish relevant data to the specified topic Topic. Related WEB and APP only need to subscribe to the corresponding topic. You can get the data.

The overall software framework is shown below:

                                                                                  Chart 1 Realization of the overall software framework

    As can be seen from the above figure, the overall software is built based on the MQTT protocol, and the MQTT IoT topology diagram built according to this software framework is shown in the following figure:

                                                                       Figure 2 Topology diagram of MQTT IoT platform

2. Framework implementation

For the convenience of testing, Siemens S7-1200 PLC is selected as the lower computer, and Siemens S7 is used for communication. The upper computer communicates with the PLC first, and at the same time connects to the MQTT server, and publishes data to the specified topic.

(1) First create a new Windows Forms application and download xktComm.dll from NuGet:

                                                                                   Chart 3 xktComm download

(2) Develop UI interface development, according to the function design as shown in the figure below:

                                                                              Chart 4 MQTT client UI interface

(3) After the UI interface design is completed, the code is written. The open source mqtt library mqttnet is used here, and it is still downloaded and installed through NuGet. The version selected here is 2.8.2. Choose a different version. There will be some differences in use:

                                                                                     Chart 5 MQTTnet download

(4) First realize the basic MQTT connection server and disconnection:

(5) Create log processing object and method body based on commission:

 

(6) Determine and create communication variable entity classes and collections:

     In the actual development process, this is based on CMSPro. This case is used for testing. Take reading the following four variables as an example (variable data and structure may be more complicated than this in actual application), as shown in the following figure:

                                                                                 Table 1 List of communication variables

Serial number

Variable name

Variable address

Variable type

1

Outlet pressure

DB100.DBD0

Float

2

Inlet pressure

DB100.DBD4

Float

3

output temperature

DB100.DBD8

Float

4

Inlet temperature

DB100.DBD12

Float

    According to this structure, create an entity class, as shown in the following figure:

    (7) Nuget added Newtonsoft.Json and introduced a JSON conversion class to facilitate subsequent conversion of objects into JSON format strings for transmission.

    (8) Establish a connection between xktComm.dll and Siemens PLC:

    (9) Timely release real-time data to the specified subject through the timer:

    (10) Open the MQTT server on the cloud server:

                                                                                Chart 6 MQTT server start

    (11) Run the MQTT client, connect to the server, and click to start timing:

                                                                           Figure 7 Run MQTT client

    (12) After the connection is successful, you can observe that the MQTT server has received the information from the MQTT client:

                                                                          Figure 8 MQTT server receiving messages

Three, framework application

    After the data has been uploaded to the MQTT server, if you need to obtain the data, you only need to develop an MQTT and subscribe to that topic. In this way, the MQTT server will automatically push the relevant information to the client software. You can use MQTT first. Test fx. MQTT.fx is an MQTT client tool based on Eclipse Paho and written in Java language. It supports subscribing and publishing messages through Topic. Open MQTT.fx, enter the correct server address, port number, user and password, and subscribe to the topic thinger, you can receive the data sent by the server in real time, as shown in the following figure:

                                                                           Chart 9 MQTT.fx application

    At the same time, we can also obtain data by developing relevant WEB pages or APPs. The following is a data display of a simple APP:

                                                                                Chart 10 TIA Portal data

 

                                                                                   Chart 11 APP data display

Four, overall summary

    This article is mainly based on the MQTT protocol to realize the process of uploading data from the local PLC controller. Based on the Alibaba Cloud server, you can access the real-time data of the local PLC from anywhere, and realize the remote upload of data. This article is mainly aimed at electrical and industrial control personnel. The Internet of Things is bound to be the future development trend. It is recommended that all PLC engineers and electrical engineers should master a programming language, which may not be used now, but will always be used in the future.

    For the source code of the MQTT server and MQTT client involved in the article, you can click on the official account above and send the keyword after paying attention: MQTT communication source code .

 

WinForm universal automatic update AutoUpdater project actual combat

●  Siemens PLC-S7 communication practice developed by upper computer

●  Develop industrial dashboard controls based on GDI+ technology

●The  upper computer C# communicates with Siemens PLC through OPCUA

●  Build S7 communication simulation environment based on S7-PLCSIM Advanced

●  Based on C# to upload local data to cloud server

●  OPCUA+MQTT builds a general framework for the Internet of Things

●  Mitsubishi Q series PLC communication practice developed by upper computer

●In-  depth analysis of the open TCP communication of Siemens PLC

●  Single-chip communication practice of host computer development

 

                                                                                         Welcome to follow Douyin

 

 

 

 

Guess you like

Origin blog.csdn.net/xiketangAndy/article/details/106788920