Contains examples showing how to use Industrial Internet of Things (IIoT) functions in PLCnext Engineer, including:
- JSON_Coder function block.
- JSON_Decoder function block.
- IIOT_MqttClient function block.
At present the function blocks used in these examples are contained in the following libraries in the PLCnext Store:
- JSON_utility_library
- MQTT_Client_Library
In the future, these will be combined into a single IIOT_Library in the PLCnext Store.
The following PLCnext Engineer project is available in the Examples
directory:
- JSON_utility_MQTT_client_EXA.pcweax
For this example, the following is required:
- Any PLCnext Control device with firmware version 2021.0 or later.
- PLCnext Engineer version 2021.0 or later.
- JSON_utility_library_1 and MQTT_Client_Library_2.
- Access to an MQTT server from the PLC, e.g. test.mosquitto.org.
-
Open the PLCnext Engineer project.
-
Check that the controller model and firmware version in the project matches the controller you are using. If necessary, replace the PLC in the project with the type of PLC that you are using.
-
In the Project Settings window, set the fields in the IP range section to suit your network. In particular, if you want to use the default Mosquitto test broker, the Default gateway must be set to the IP address of your internet router.
-
In the controller Settings window, set the fields in the Ethernet [Profinet] section to suit your controller.
-
In the Project Online Devices window, check that the PLC is online and that the Status field is OK (indicated with a check mark).
-
Download the project to the PLC and check that the PLC starts OK.
Before using the MQTT programs Publish
and/or Subscribe
, check the values of the following variables, and change them if required:
Name | Description | Default value |
---|---|---|
SERVER_ADDRESS | The address of the MQTT broker. Must be accessible from the PLC. (check using ping if necessary). |
tcp://5.196.95.208:1883 |
CLIENT_ID | The ID of the MQTT client. Must be unique on the MQTT broker. |
PLCnext Subscriber or PLCnext Publisher |
TOPIC | The topic to publish/subscribe to on the broker. | plcnext |
-
In the program instance called "JSON_Code_Decode", set the variable
udtCoderExample.udtCoder.x_EN
toTRUE
. This creates a JSON stream as a byte array. This is passed to the "MQTT_Publish" program instance via Port variables. -
In the program instance called "MQTT_Publish", set the variable
Run
to TRUE. This starts publishing the JSON stream to the Mosquitto test broker. Successful publishing can be checked by subscribing to the same topic from any machine with MQTT client software installed (e.g. themosquitto_sub
command-line tool on Linux). -
In the program instance called "MQTT_Subscribe", set the variable
Run
to TRUE. This subscribes to the topic on the Mosquitto test broker where the JSON messages are being published. -
Each time a new message is received from the server and consumed by the client, the JSON Decode process is triggered via Port variables.
-
In "JSON_Code_Decode", check the value of the structs udtJsonValue_Dot_INT and udtJsonValue_Dot_DINT. The values of the INT and DINT variables from the incoming JSON stream should appear in these structs.
The MQTT Publish program implements a simple state sequencer that progresses through the following steps:
- Idle - waiting for the start conditions.
- Connecting - waiting for the client to connect to the server.
- Running - publishing a simple message every 5 seconds, and waiting for the Run signal to disappear.
The MQTT Subscribe program implements a simple state sequencer that progresses through the following steps:
- Idle - waiting for the Run signal.
- Connecting - waiting for the client to connect to the server.
- Subscribing.
- Running - receiving messages, and waiting for the Run signal to disappear.
- Unsubscribing - waiting for the unsubscription to complete.
The example can be adjusted to use an SSL connection using the following steps:
-
Download the server certificate for the Mosquitto test broker (in PEM format) from test.mosquitto.org.
-
Copy the server certificate to the PLC. Take note of the full path to the server certificate, e.g.
/opt/plcnext/mosquitto.org.crt
-
Change the value of the SERVER_ADDRESS variable to the following:
ssl://5.196.95.208:8883
-
In the code, before calling the Connect method, set the following values:
- connOpts.xUseSslOptions := TRUE
- connOpts.udtSsl.strTrustStore :=
/opt/plcnext/mosquitto.org.crt
(* the full path to the server certificate on the PLC *) - connOpts.udtSsl.xEnableServerCertAuth := TRUE
-
Write and start the project.
The client will now use SSL to connect and communicate with the MQTT broker.