MQTT QoS 1 & 2

2024-10-26
2024-10-31
  • yannickasselin - 2024-10-26

    Hello,

    I am using Codesys sp20p3 and IIoT library 1.11.
    I am trying to test QoS 1 & 2 and I have some issues.

    I am using mosquitto as the broker. I am also using some other MQTT clients like Node-Red and TwinCAT. They all work as expected for every QoS levels. I only have issues with Codesys MQTT client.

    In the Codesys client, if I subscribe to a topic as QoS 1 or 2, and a client publishes to this topic, I receive the message. But if I disconnect the Codesys client from the network and a client publishes to the subscribed topic, when I reconnect Codesys client to the network, I don't receive the messages as expected. Also there does not seem to be an auto-reconnect feature. So I have to manually set the mqttClient.xEnable bit to FALSE then back to TRUE in order to reconnect to the broker, then I also have to set the subscribe.xEnable to FALSE and back to TRUE in order to re-subscribe to the topic, but even then, I don't receive the messages that were published while I was disconnected. What am I doing wrong?

    I set the cleanSession bit to FALSE and I give my client a clientID.

    With the TwinCAT MQTT Client, I do the same thing and everything works as expected. It even auto-reconnects to the broker. I would expect Codesys MQTT Client to be as reliable as TwinCAT. Maybe I am doing something wrong?

    I also noticed that when trying to publish a QoS 1 or 2 message while disconnected from the broker, it does not work. In TwinCAT, I am still able to publish. The messages are stored in the client and published once reconnected to the broker. I would expect to be able to do the same thing in Codesys.

    Is this all possible? Am I doing something wrong?

    Thank you

     

    Last edit: yannickasselin 2024-10-26
  • yannickasselin - 2024-10-29

    Anyone? Please?

    The Codesys documentation clearly states that QoS 1 & 2 are supported for Publisher and Subscriber.
    I really need to make this work for an important project.

    Thank you

     
  • eschwellinger

    eschwellinger - 2024-10-29

    give us a little more time to check.

     
  • yannickasselin - 2024-10-30

    Hello,

    After some more tests, here is what I found out.

    It seems we have to increase the uiKeepAlive and tPingInterval parameters. If I increase the uiKeepAlive value to 60s and the tPingInterval to 30s, then if I disconnect the ethernet cable from Codesys and publish some messages to which Codesys subscribes to and then reconnect within 30s, I will get the messages. If I don't reconnect the cable within 30s (ping interval), the client goes in error with "ACKNOWLEDGE_TIMEOUT". When this happens, it will not automatically reconnect after reconnecting the cable. I have to disable and re-enable the client but I will not get the published messages.

    I need to do more tests because there are a lot of weird things happening when playing with the ping interval and the keep alive. It is not clear to me what does what. I even ended up not receiving messages anymore, even if I disabled and re-enabled the mqtt client. The client did not give any error. The only way I was able to get messages again was by modifying my client ID. After that, if I try to re-use one of the old client IDs, nothing works (publish, subscribe) and I get a TCP_INIT_ERROR and sometimes a TCP_READ_ERROR.

    I need more explanation/documentation about these weird behaviors. Maybe it is the way I use it, maybe it is related to the broker (I am using Mosquitto with default parameters). When using a Node-Red client, everything works perfectly. I am trying to achieve the same behavior with Codesys but it seems impossible so far.

     
  • yannickasselin - 2024-10-31

    Using Wireshark, I can see that the broker sends the QoS 1 or 2 messages when Codesys MQTT client reconnects but they are just discarded and not acknowledged by Codesys MQTT Client. So the broker holds on to them and tries to send them back to Codesys every time the client reconnects but they are always discarded by Codesys.

    There really seems to be an issue with QoS 1 & 2 in Codesys. It really does not behaves like it should.

     

    Last edit: yannickasselin 2024-10-31

Log in to post a comment.