├── LICENSE ├── MQTT_Client.library └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 rossmann-engineering 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MQTT_Client.library: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rossmann-engineering/CoDeSys-MQTT-library/46590f518041676dd88f72e454bf1ed293ca0dfa/MQTT_Client.library -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CoDeSys MQTT library Overview 2 | 3 | The purpose of this library is to publish PLC variables to an MQTT Broker. Suitable for industrial IoT Solutions. 4 | The library is compatible with PLCs based on CoDeSys V3 and already used in industrial applications. 5 | One single Function block is necessary, programmed in IEC61131 code. Documentation is integrated in the library. 6 | 7 | Features: 8 | 9 | - MQTT protocol Version 3.1.1 10 | - Publish either to a local MQTT-Broker or a public MQTT-Broker 11 | - Subscribe to a Topic 12 | - Well documented IEC61131 library 13 | - supports QoS0 (At-most-once) 14 | - Optional auto reconnect 15 | - Optional User and Password authentication 16 | - Last will and Testament 17 | 18 | Function block: FB_MQTTClient 19 | Library: MQTT_Client.library 20 | Namespace: MQTT_Client 21 | 22 | | Name | Datatype | Initial Value | Comment | 23 | |---|---|---|---| 24 | | i_xEnable | BOOL | TRUE | Enables the Function block. The rising Edge of this Input automatically connects to the MQTT-Broker | 25 | | i_sBrokerAddress | STRING | 'www.mqtt-dashboard.com' | IP-Address (example: 192.168.178.101) or Webaddress (www.mqtt-dashboard.com) of the MQTT-Broker| 26 | | i_uiPort | UINT | 1883 | Port of the MQTT-Broker| 27 | | i_sUsername | STRING | '' | Optional Username if required - Only change initial value if required| 28 | | i_sPassword | STRING | '' | Optional Password if required - Only change initial value if required| 29 | | i_sWillTopic | STRING | '' | Optional Topic the Will Message will be published to- leave untouched if not required| 30 | | i_sWillMessage | STRING | '' | Optional Will Message - leave untouched if not required| 31 | | i_sWillRetain | STRING | FALSE | Retain Last Will Message, if required Standard: FALSE - otherwise leave untouched| 32 | | i_xAutoReconnect | BOOL | TRUE | Automatically try to reconnect after an Exception| 33 | | i_sPayload | STRING | 'Hello MQTT-Broker from CoDeSys' | Payload to Publish| 34 | | i_sTopicPublish | STRING | 'CoDeSys' | Topic to publish to | 35 | | i_sTopicSubscribe | STRING | 'CoDeSys' | Subscribtion Topic| 36 | | i_xRetain | BOOL | TRUE | Retain Flag| 37 | | i_xPublish | BOOL | FALSE | Publish the Payload to the Topic of the MQTT-Broker| 38 | | i_xSubscribe | BOOL | FALSE | Subscribe to the Topic given in i_sTopicSubscribe| 39 | | i_sClientID | STRING | 12345678 | Sets a custom Client ID, leave at default for Automatik| 40 | | q_stLastReceivedMessage | STRING | | Message Received from a Topic| 41 | | q_stLastReceivedMessageTopic | STRING | | Topic Last received Message | 42 | | q_arsLastReceivedMessages | ARRAY [0..24] of STRING | | Last 25 Received Messages| 43 | | q_arsLastReceivedMessagesTopic | ARRAY [0..24] of STRING | | Last 25 Received Topics| 44 | | q_xReceivedMessageNotification | BOOL | | Message Received Notification| 45 | | q_sDiagMsg | STRING | | Diag Message - Current State| 46 | | q_udiState | UDINT | | Current state of the Function block| 47 | | q_xError | BOOL | | Error Flag - The Exception will be acknowledged with a rising edge at 'i_xEnable'| 48 | 49 | 50 | Function block state (output 'q_udiState'): 51 | 52 | 0: Wait for Enable (Rising Edge) 53 | 5: Create Socket 54 | 10: Connect to TCP-Server 55 | 15: Initialize Out-Buffer 56 | 20: Send Connect to the MQTT-Broker 57 | 30: Wait for CONNACK from MQTT-Broker 58 | 40: Analyze CONNACK 59 | 60: Wait for Publish (Goto 70) or Subscribe (Goto 65) or Disconnect (Enable low) (Goto 80) 60 | 63: Analyze Publish Message from Broker 61 | 65: Subscribe to Topic' 62 | 66: Wait for SUBACK from Broker 63 | 67: Analyze SUBACK 64 | 70: Publish Message (Goto to 60) 65 | 80: Send Disconnect 66 | 90: Disconnect Socket 67 | 68 | Possible Diag Messages (output 'q_sDiagMsg') 69 | 70 | - Wait for Enable 71 | - Create Socket 72 | - Connect to TCP-Server 73 | - Initialize Out-Buffer 74 | - Connect to MQTT-Broker 75 | - Wait for Connack from Broker 76 | - Analyze connack 77 | - Wait for Publish or Subscribe or Disconnect (Enable low) 78 | - Analyze Publish Message from Broker 79 | - Subscribe to Topic 80 | - Wait for SUBACK from Broker 81 | - Analyze SUBACK 82 | - Publish Message 83 | - Send Disconnect notification to server 84 | - Close TCP Socket 85 | 86 | Exceptions 87 | 88 | - Creating Socket Timed Out 89 | - Connecting to TCP-Server Timed out 90 | - Connecting to MQTT-Broker (MQTT Control Packet) Timed out 91 | - Waiting for "Connack" from MQTT-Broker Timed out 92 | - Connection Refused, unacceptable protocol version 93 | - Connection Refused, identifier rejected 94 | - Connection Refused, Server unavailable 95 | - Connection Refused, bad Username or password 96 | - Connection Refused, not autorized 97 | - Publishing Message (MQTT Control Packet) Timed out 98 | - Sending Disconnect to MQTT-Broker Timed out 99 | - Closing TCP-Socket Timed out 100 | - Suback Message reported Failure (Return code 0x80) 101 | - Waiting for "Suback" from MQTT-Broker Timed out 102 | - Subscribe (MQTT Control Packet) Timed out --------------------------------------------------------------------------------