Mosaic MQTT Staying Connected

Hi everyone.

I'm dabbling in using some of the modules, and was intrigued with the MQTT Module.

I've written some triggers so Mosaic can enqueue a trigger with Publishing a Topic and Payload as the Trigger Variables.

So I've tested that and got it to work, as well as a trigger that when connected it asks to subscribe to a topic, which I can use to parse.

So testing on the bench it works pretty well.

The problem I'm running into though is how to keep the MQTT Socket connected. Once i leave and the Mosaic sits idle, it seems to disconnect, and doesn't seem to connect right away after a while.

What sort of cues or other actions should I try to implement to make sure the Mosaic, at least reconnects when a new message needs to be sent, or preferably to remain up the whole time and publish the MQTT Topic and Payload as soon as it gets a new one? That way if I send it a payload to a topic it is subscribed to, it will respond?

Also on a similar note, can anyone confirm that it's not possible to set a Last Will Message? I was hoping to use that feature, but that doesn't seem possible with the current MQTT Module.

  • hi, 

    ' Once i leave and the Mosaic sits idle, it seems to disconnect, and doesn't seem to connect right away after a while.

    That seems to be in line with the documentation:
    The Keep Alive time defines how long a connection to the MQTT Broker persists, if no requests are made from the Client. The default is 60 seconds. If the connection times out, a reconnection will be tried when sending requests from actions or if a Connect To Broker action is called. The minimum is set as 10 seconds as it may take a few seconds to establish a TCP connection, send a connection request and receive a reply from the Broker. The Broker Disconnected trigger can be used to re-initialise a connection and re-subscribe to topics if necessary.

    'What sort of cues or other actions should I try to implement to make sure the Mosaic, at least reconnects when a new message needs to be sent, or preferably to remain up the whole time and publish the MQTT Topic and Payload as soon as it gets a new one? That way if I send it a payload to a topic it is subscribed to, it will respond?'

    I think to remember you need to send a 'start ping' action at or shortly after startup. (this will send a keep alive at the specified interval if I read the docs correct)

    I concur Last Will messages currently can't be set.

  • Thanks for the confirmation on Last Will.

    So what I have right now is a Startup Trigger that uses the Action Delay Module to fire a Softkey to connect to the broker. I'm using the default 60 Second Keep Alive.

    Then I have an On Broker Connected Trigger that runs a small script that will publish to a Status Topic saying OK, then it subscribes to a Topic for Remote Commands, and starts an MQTT Ping set to interval every 27 seconds.

    There is also a Disconnected Broker Trigger that also fires the Connect to Broker Action.

    Then I have some scripts that tie into the touchscreen, which allow publishing the selection to a topic I'm monitoring.

    so if I update the Mosaic, then start interacting with it, I see the Monitor Topic has the strings from the touchscreen.

    But once it is left alone for a bit, it disconnects and despite the trigger that on Disconnect it should try to reconnect.

    Perhaps I need to delay the reconnect attempt? I'm not sure what else I can do to improve it. I have it Ping the Broker twice before the keep alive signal. Am i crossing some sort of wire here?

  • Have you tried adding a 'Ping Broker' action that you fire at startup? (Or when the broker connects, but then I would advise to also add an action to Stop Ping when broker disconnects) 

  • hmmm. I do have the MQTT Ping start when the Broker Connects. I don't think I put a Stop Ping in the disconnect. Maybe that was causing a problem.

    My disconnect trigger only enqueues a soft trigger that starts the Connect to Broker Action. So I can put a Stop Ping Action there, to Stop the current Ping if the Broker Disconnects.

    How about Ping interval? I know the Keep Alive time says it should get a message every 60 seconds, but maybe I should make the ping Repeat like 10 seconds? just so i make sure several Pings should have been sent before we consider the connection abandoned? It's currently 27 Seconds.

  • If you can share your file that might be easiest. What is your broker?
    I'm no MQTT expert; but I was involved in a large project that integrated a 19" controller with MQTT, and that worked fine. 

  • I'm Using Hive MQTT Pay as You Go Service. It's for an MTPC.

    The design is a little bloated, would it be okay if I sent you one where i recreate just the MQTT triggers?

  • Sorry I never sent you the design, but I think I also realized something.

    I was rewatching some videos about MQTT, and I realized one of the things they said was that the MQTT needs to have unique client IDs.

    My problem is this a system that uses Replications. It uses the same file to talk to 3 of the same systems.

    I wonder if that has something to do with it.

    I left the clien ID blank when I setup the module, but I wonder if it only generates one key when I compile, and the replications end up being the same random key it generated once.

    I have another script that uses the Serial Number of each Mosaic as a key to look up information unique to each system, so what would be great is some method of that, to be able to set the Client ID based on the Serial Number.

    That would insure each mosaic has it's own client ID. Which may be affecting it.

    It's also hard to test the system because it's kind of far away.

Related