MQTTPubService is a practical PUBLISH service FB that makes it easy to use MQTT Publish in a program using the MQTT communications Library (SYSMAC-XR020) published by OMRON. Check out this article for more details and here for usage examples.
MQTTPubSerivce FB has a simple structure with only one MQTT connection, one Publish worker, automatic ping check, and minor monitoring. If necessary, it can be combined with an inter-POU messaging mechanism to create a pan-POU service, and a fallback mechanism can be built to increase the degree of data loss avoidance.
As far as I have confirmed, the MQTT client of the MQTT communications Library can avoid data loss with simple buffer management even if there are slight speed fluctuations in the network, as long as the transmission speed per connection is around 1 Mbps. 1 Mbps is a 4 millisecond periodic task containing 500 bytes of information per cycle. Also, if your broker allows it, you can connect up to three at the same time.
Using MQTTPubService requires the following environment.
Controller | NX1 or NX5 |
Sysmac Studio | I always recommend the latest version. |
MQTT broker | You are free to choose any broker that complies with MQTT Version 3.1.1. Unless the broker is running on very constrained hardware, the traffic will not be such that processing power becomes an issue. Avoid using public brokers. |
MQTT client | There is no problem with your favorite MQTT client. |
Network | Avoid networks with large delays between the localer and controller. A ping value of about 30 ms is recommended. |
The project was built in the following environment.
Controller | NX102-9000 Ver 1.64 |
Sysmac Studio | Ver.1.60 |
MQTT broker | Mosquitto on Docker on local PC (eclipse-mosquitto:2.0.20) |
MQTT client | MQTTX |
Network | There was an average delay of 10 ms in Ping values between the broker and the controller. |
This project has a sample program (NotifyStats) for MQTTPubService. You can use it by modifying the project and setting up the environment using the following steps.
- Check the project file hash
Nothing can be done if the project file is corrupted. - Match the project's controller format to the controller you use
Older NX1 cannot use MQTT communications Library. Please check the MQTT communications Library manual for available versions. - Change settings for "POU/Program/NotifyStats"
Match the broker address, port number, and client ID to your environment.iMqttSettings.BrokerAdr := 'YOUR_BROKER_ADR'; // '192.168.250.2'; iMqttSettings.BrokerPort := YOUR_BROKER_PORT; // 1883; iMqttSettings.ClientID := 'YOUR_CLIEN_ID';
- Change configuration/settings/controller settings/built-in EtherNet/IP port settings
Change the settings so that you can reach the broker. - Set up and start MQTT broker
Start the broker and check if you can connect with a suitable MQTT client. Also check if you can subscribe to the broker's system status. - Adjust controller clock
If the clock of the connected terminal running Sysmac Studio is appropriate, it will be reflected. If you use NTP, make sure it is synchronized. - Download project to controller
Run the program in run mode and check the event log for errors. - Subscribe to "machine/stats" with MQTT client
Verify that you can subscribe to messages and that the content is reasonable.
MQTTPubService can monitor the state and perform necessary control by referring to the State variable (MQTTPubService.State). The state includes follows.
State | Description |
---|---|
STOP | The service is stopped. |
INACTIVE | The service is running but not available. |
ACTIVE | Able to issue messages. |
NO_CONNECTION | Lost connection with broker. |
PUBLISHING | Publish a message. |
PUBLISHED | The message has been published. |
SHTDOWN | Shut down the service. |
ERROR | An exception has occurred. Please check ErrorID, ErrorIDEx. |
The state transits as follows.
---
title: MQTTPubService state transition
---
stateDiagram-v2
[*] --> STOP
STOP --> INACTIVE : Enable=True
STOP --> [*]
INACTIVE --> ACTIVE : MqttClient.Connected=True
INACTIVE --> STOP : Busy=False
INACTIVE --> SHUTDOWN : Enable=False
ACTIVE --> NO_CONNECTION : MqttClient.Connected=False
ACTIVE --> SHUTDOWN : Enable=False
ACTIVE --> ERROR : MqttClient.Error=True or MqttPing.Error=True
ACTIVE --> PUBLISHING : Publish=True and start publishing.
NO_CONNECTION --> ACTIVE : MqttClient.Connected=True
NO_CONNECTION --> ERROR : MqttClient.Error=True
NO_CONNECTION --> SHUTDOWN : Enable=False
PUBLISHING --> PUBLISHED : PubWorkerContext.State = PUBLISHED
PUBLISHING --> ERROR : An error occures in any MQTT FB.
PUBLISHED --> ACTIVE : [1 cycle]
PUBLISHED --> ERROR : [1 cycle] MqttClient.Error=True or MqttPing.Error=True
ERROR --> SHUTDOWN : [1 cycle] An error occurres in the MQTT connection
ERROR --> ACTIVE : [1 cycle] Faile to publish message
ERROR --> ERROR : If errors occur continuously
SHUTDOWN --> INACTIVE : [1 cycle]
Control using state uses the CASE statement as follows.
CASE iMqttPubService.State OF
MQTT_PUB_SRV_STATE_STOP:
// If the service is stopped, it will start immediately.
iMqttPubService.Enable := TRUE;
MQTT_PUB_SRV_STATE_ERROR:
// If an error occurs, the service will be stopped
// regardless of the content.
iMqttPubService.Enable := FALSE;
iMqttPubService.Publish := FALSE;
MQTT_PUB_SRV_STATE_INACTIVE,
MQTT_PUB_SRV_STATE_NO_CONNECTION:
DO_FALLBACK();
MQTT_PUB_SRV_STATE_ACTIVE:
IF DO_PUBLISH THEN
iMqttPubService.Request := iPubRequest;
iMqttPubService.Publish := TRUE;
END_IF;
MQTT_PUB_SRV_STATE_PUBLISHED:
// After sending the message, drop the flag.
iMqttPubService.Publish := FALSE;
END_CASE;
iMqttPubService();
I recommend checking the hash value of the acquired Sysmac Studio project.
file | sha256 |
---|---|
MQTTPubServiceNoBuffer.scm2 | 1d93296918962a32a01d3c23611ab3dfa7c2f77221b86d89d183f1bb3aeb93e6 |