Giter VIP home page Giter VIP logo

mqtt-siemens-s7-300's Introduction

MQTT-Siemens-S7-300

MQTT library block written in SCL for S7-300 with internal (PN) or external (CP) Ethernet.

This started as a port of knolleary's MQTT library for Arduino & ESP8266. The implementation is following the MQtt v3.1.1 protocol documentation (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)

Main functionallity complete, still work in Progress!

Purpose and Possibilities

MQTT is a popular communications protocol in the IoT space. Its demand on most types of networks and CPUs make it a good option for M2M applications. MQTT devices can easily be utilized to send a message anywhere in the world.

What does this mean for PLCs & Industrial Automation?

MQTT will enable a PLC to connect to the cloud without using proprietary hardware or protocols. PLC programmers can use it to build customized programs that send info to a web server, log plant data, and communicate with any other MQTT client device. Developers can use it to build customized dashboards (physical, website, or mobile device), providing valuable data for analytics and business. MQTT enabled IO could serve as an inexpensive alternative for the PLC. (although not recommended for mission critical IO and for safety reasons)

Current Test Scenario:

At this time, this code has been successfully tested on:

  • CPU312C (312-5BF04-0AB0) with CP343-1 (343-1EX30-0XE0) external Ethernet
  • CPU313C (313-5BG04-0AB0) with CP343-1 (343-1EX30-0XE0) external Ethernet
  • CPU313C (6ES7313-5BF03-0AB0) with CP343-1 (343-1EX30-0XE0) external Ethernet (PLC with only 64KB RAM!)
  • CPU314C-2 PN/DP (314-6EH04-0AB0) with CP343-1 (343-1EX30-0XE0) external Ethernet
  • CPU315-2 PN/DP (315-2EH14-0AB0) with internal Ethernet

The PLC is connected to a Mosquitto MQtt broker. All main functionallity has been testet: connect, disconnect, subscribe, unsubscribe, ping, publish. I am locally connecting to a Mosquitto broker.

Currently Limitations:

  • not all MQtt policies described in the MQtt v3.1.1 standard are exactly standard conform implemented Especially the code must be reviewed wether it conforms to all the yellowish lines in the MQtt documentation
  • subscribe and unsubscribe only for one topic at a time (you can subscribe multiple times if you need several topics)
  • the Siemens PLC Ethernet adapters can send/receive 8192 bytes max. per transmission. Please refer to the Simatic Manager help pages for the corresponding FBs/FCs
  • Qos 2 handling for incoming publish messages not implemented

Todo:

  • state machine for tcp and mqtt should be harmonized
  • MQtt policies review for the code

Requirements

It should work with most S7-300/400 CPU's. This code is written in Step7 SCL v5.3 SP1. It probably needs modification for it to compile in TIA.

Setup the Step7 project

You need to add the following Objects Library blocks to your project:

Only needed for internal ethernet support (PN): Library: Standard Library -> Communication Blocks -> Blocks

  • FB63 TSEND
  • FB64 TRCV
  • FB65 TCON
  • FB66 TDISCON

Only needed for external ethernet support (CP): Library: SIMATIC_NET_PC -> CP300

  • FC5 AG_SEND
  • FC6 AG_RECV
  • FC10 AG_CTRL

Additional Objects needed: Library: IEC Function Blocks

  • FC21 LEN
  • FC10 EQ_STRNG

Library: System Function Block

  • SFB4 TON
  • SFC6 RD_SINFO
  • SFC20 BLKMOV
  • SFC58 WR_REC
  • SFC59 RD_REC

Important: there will be an import conflict between FC10 AG_CTRL and FC10 EQ_STRING, as a solution rename one of the the FC-numbers during import

Compiling the project

Block numbers generation

There are two ways to generate the Block numbers required by the project. Block numbers can be generated automatically or they can be manually setup in the projects Symbols table. Block number auto generation can be configured by setting the “Create block numbers automatically” option in the Options -> Customize menu of the SCL editor, in the “General” tab the “Create block numbers automatically” option must be set. If block numbers must be set manually, open the “S7 Symbols.xlsx” document in the Support folder, set the block numbers according to your requirements and copy&paste them into the S7 projects Symbols table.

###Compiling the S7 MQTT project There are two Makefiles, please choose the right one for the Ethernet requirements (ProfiNet, CP) of the project:

  • Makefile-CP will compile the project with CP Ethernet support (CP, e.g. CP-343)
  • Makefile-PN will compile the project with ProfiNet (integrated) Ethernet support (PN)

Important: only compile the Makefile that suits your requirements.

Imporant: You must call the MQTT function block in your OB1 program loop.

Example programm

Optional: You may compile the MQTT_Example.scl

Network Configuration

The MQTT FB can use the internal Ethernet adapter of a CPU (PN, choose MQTT_Main_PN.scl) or an external Ethernet adapter (CP, choose MQTT_Main_CP.scl).

Remarks for internal Ethernet (PN) adapter configuration:

  • The IP-Address of the internal adapter has to be configured within the hardware configuration tool (HW Config), click on the PN-IO object
  • The remote IP and Port parameters are configured via parameters for the MQTT Function Block (ipBlock1-4,ipPort)
  • you must set the MQTT Functionblock parameter connectionID to a desired value, f.e. 1 Example for a MQTT FB call in OB1 configured for internal Ethernet (PN) usage: MQTT.DB71(net_config := DB_NET_CONFIG, connectionID := 1);

Remarks for external Ethernet (CP) adapter configuration:

  • The IP-Address of the internal adapter has to be configured within the hardware configuration tool (HW Config), click on the PN-IO object
  • The connection must be configured in Simatic Manager "Connections"
  • you must set the MQTT Functionblock parameter connectionID to the connection ID configured in Simatic Manager "Connections"
  • you must set the MQTT Functionblock parameter cpLADDR to the address of the CP module. Example for a MQTT FB call in OB1 configured for external Ethernet (CP)usage: MQTT.DB71(net_config := DB_NET_CONFIG, connectionID := 1, cpLADDR := W#16#100);

Setup memory footprint

Setting up buffers

To reduce the memory usage of the MQtt DBs you can set the receive and transmit buffer sizes.

  • Set tcpRecBuf array size in mqttData DB and TCP_RECVBUFFERSIZE in mqttGlobals to the same value (f.e. 8192) of your choice.
  • Set TCP_MAXRECVSIZE in mqttGlobals DB to a value equal or smaller than TCP_RECVBUFFERSIZE
  • Set tcpSendBuf array size in mqttData DB to a value of your choice
  • Set buffer array size in mqttData DB to a value of your choice, but not smaller than the largest value of TCP_MAXRECVSIZE or tcpSendBuf, whoever is larger

Keep in mind: data from tcpRecBuf is transfered to buffer and also data from buffer is tranfered to tcpSendBuf within the Code. So match the sizes appropriately.

Check connection status

You can check the connection status by monitoring two Flags in mqttData DB:

mqttData.ethTCPConnected : this Boolean will show you the state of the TCP connection to the broker

mqttData._state : will show you the connection status of the MQtt connection to the broker. Check for value > 0 to check if MQTT is connected. I recommend to check this status before trying to send a message.

mqttData.mqttErrorCode : will hold the last MQtt error code

mqttData.tcp_sendBufferFull : will be true if the send buffer is full. Important: this also indicates that the last message could not be added to the send buffer, the last message therefore was discarded. To avoid problems with the send buffer, give it an appropriate size to hold multiple messages and also only send if mqttData._state > 0.

Example

Included is an example application function block (FB70) that is typically called from OB1. Inputs for this block can trigger a MQTT broker connect, publish a message or subscribe to a MQTT channel.

There is also some example code for calculating a payload CRC to check the data integrity. The used CRC_GEN function can be found in the oscat.de PLC library.

Notes

Porting C++ to Siemens SCL has taught many differences between systems. Differences include:

  • The inability to make blocking calls in SCL.
  • No dynamically allocated memory.

One reason for this is a PLC is a real-time system. A consequence is that the entire program code, from the first to last line, must complete in a certain amount of time. (Typically <10ms) The workaround I make is using a FB with a state machine that can wait for a procedure to finish before moving on.

After working with SCL for a while it feels that the language is primitive in many ways in comparison with most PC languages. I realise that some of it is because of program limitation for safety and resource conservation, but general program principles like DRY, OOP, etc. are sometimes difficult to achieve if not impossible.

Another annoying difference is the idea of having symbol entries for symbolic block names. On one side I want to refactor and break behaviour up in more functions like in general programming. But in Siemens I don't want to create a new symbol entry for each FC that I make. Also, this is probably not encouraged in PLC programming anyway as it increases the maximum size of the call stack in every scan cycle and increases the scan time. This makes it difficult to make small sized program code that is also performant.

Furthermore, the low-level programming and memory limitation reminds of microcontroller programming.

mqtt-siemens-s7-300's People

Contributors

carstenmaul avatar christofgroschke avatar feilner avatar roanbrand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mqtt-siemens-s7-300's Issues

Can't connect with mosquitto (CPU313C and CP343-1 Lean)

Hi, I'm using the hardware that has been used in the test of the maker.

Unfortunately, it does not connect. What can I do possibly wrong?

The IP-Address of the internal adapter has to be configured within the hardware configuration tool (HW Config), click on the PN-IO object
IP address is 192.168.0.1 of the CP module with subnet 255.255.255.0
The connection must be configured in Simatic Manager "Connections"
Has been made, see attachment
you must set the MQTT Functionblock parameter connectionID to the connection ID configured in Simatic Manager "Connections"
What is the connection ID? I used now the backplane connection, see attachment
you must set the MQTT Functionblock parameter cpLADDR to the address of the CP module. Example for a MQTT FB call in OB1 configured for external Ethernet (CP)usage: MQTT.DB71(net_config := DB_NET_CONFIG, connectionID := 1, cpLADDR := W#16#100);
I compiled the example and changed connectionID to 3 (because it is the backplane connection MPI address, is that correct?). I kept cpLADDR the same, because start address is 256 and that 16#100

Did I do something wrong?

Connections
StartAddress
MPIaddressAsConnectionID

I can only send 4 digits

hi, is this project still used by anyone?

i got it working but i can only send max 4 digits.

image

he send 4 digits of my message (array of byte) and after that connection lost and the username + password of the mqtt server.

my problem is i want to use a s7-315 plc. the new official siemens blocks works great on an s7-1500. so the only working blocks for s7-3xx i found were these here.

Mix of MQTT connection state and MQTT connect error codes leads to conflict?

Hello,

in the function block MQtt within this code block
CASE (intState) OF 0 : // Wait for received packet start IF ((Data.runTime - Data.lastInActivity) >= Globals.MQTT_SOCKET_TIMEOUT) THEN Data._state := Globals.MQTT_CONNECTION_TIMEOUT; ELSIF available() THEN myPacketReader.iBegin := true; intState := 1; END_IF; 1 : // Read complete packet IF (myPacketReader.xDone) THEN IF (myPacketReader.result = 4) THEN IF Data.buffer[3] = 0 THEN Data.lastInActivity := Data.runTime; Data.pingOutstanding := false; Data._state := Globals.MQTT_CONNECTED; // here Data._state is used for a defined state. ELSE Data._state := BYTE_TO_INT(Data.buffer[3]); // here Data.state is used to store error codes returned by a connack packet END_IF; // ELSE maybe need equivalent/workaround to client->stop() END_IF; END_IF; END_CASE;

Please look at the Data._state assignmends within CASE (intState) OF = 1 code block.
There are two Data._state assignments.
The first assignment assigns a defined state
Data._state := Globals.MQTT_CONNECTED
The second assignment assigns an connack error code to Data._state:
Data._state := BYTE_TO_INT(Data.buffer[3])

Data.buffer[3] holds the connack error code returned by a connack mqtt package.

The problem is:
within Globals. the state "1" is assigned as Globals.MQTT_CONNECTED
within the MQtt docomentation this value is already taken by an connack error code:
0x01 Connection Refused, unacceptable protocol version
So if the error returned is 0x01, in my opion the code will set Data._state to Globals.MQTT_CONNECTED, which is wrong state.

Please refer to http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349257
(MQtt documentation section 3.2.2.3 "Connect Return code")

Cannot compile makefile

Hi,

I know this is now an inactive topic for some time but I would really like to use this functionality in my project but im having problems compiling all blocks successfully.

I can't figure out how to run the make file to generate the project so im doing it manually which is probably why im coming up with the issues.

Can you please let me know how to generate the project with the make file?.

Thanks in advance.

Regards,

Paul.

Guide to get this working?

Hello..
First of all - awesome project.... :)
i'm trying to understand the basics in this program - after this i wanna try to port it to the TIA Portal and the S7-1500/1200 (Seems like the program allmost can be compiled under a 1500 CPU) Has any of ya guys tried to compile this under the TIA portal and a 1500/1200 Controller?

Is there any guide or sample Siemens project where i can see how it works?

What's the difference between PUBLISH, WRITE and WRITESTRING - how to use the WRITE if i need to send a header and a value to a MQTT broker?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.