News:

  • June 27, 2026, 07:19:04 PM

Login with username, password and session length

Author Topic: Best way to space MQTT publish instructions  (Read 17914 times)

diannacone

  • Newbie
  • *
  • Posts: 4
Best way to space MQTT publish instructions
« on: April 29, 2025, 03:29:19 PM »
I have several switches connected to a BRX.  The switches each trigger a "publish" to a topic and I notice that if flip the switches one by one I get 100% publish success to the MQTT broker. if I flick multiple switches at the same time I get a failure rate about 1 in 10 to the broker. I wrote a python script that publishes to the same topics all at once in an attempt to flood the broker and i can't seem to recreate any failure so its pretty obvious to me its the plc.
I assume I need to space the publish messages out over several scans?  Is there a best practice for this or slick way to do this without a bunch of timers or like stage programming?

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to space MQTT publish instructions
« Reply #1 on: April 29, 2025, 09:46:29 PM »
You could either publish a MQTT message containing the status of all the switches, on or off, or you would have to create a buffer/queue of each message, and then whenever there is data in the queue, run your publish instructions, taking a message out of the queue each time.

diannacone

  • Newbie
  • *
  • Posts: 4
Re: Best way to space MQTT publish instructions
« Reply #2 on: May 01, 2025, 05:17:16 PM »
So something like create a FIFO and store a D value that corresponds the message needed to publish. Then a loop a subroutine where your publish instructions are inside of that on rungs where D value = publish instruction? I guess I need a timer in the subroutine too?

I'm just trying not to run into the same thing where the instruction isn't sent because it can't complete it in one scan. Honestly, I am a little confused why this is happening with Qos =1 in the MQTT client settings.  ohhh..Wait do you think it would have anything to do with the success bit being the same for multiple MQTTPUBs ....maybe I try to remedy that.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: Best way to space MQTT publish instructions
« Reply #3 on: May 03, 2025, 12:31:21 PM »
Not knowing exactly what your MQTT messages are, yes, a FIFO with D values could be used to lookup a message to publish (you could also create a FIFO of strings to put the messages directly into the queue). I don't think you need a loop instruction, just call a Task whenever your queue is not empty.

FIFOUNLOAD to D0
IF D0 = 1 STRPRINT SS0 "Message 1"
IF D0 = 2 STRPRINT SS0 "Message 2", etc. (You can also use STRPRINT's Lookup function to do it all in one rung)
MQTTPUB with a Payload of SS0.

Depending on your MQTT broker, you may need a timer of a few seconds to prevent exceeding your allocated rate.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6164
  • Yes Pinky, Do-more will control the world!
Re: Best way to space MQTT publish instructions
« Reply #4 on: May 03, 2025, 06:55:39 PM »
ohhh..Wait do you think it would have anything to do with the success bit being the same for multiple MQTTPUBs ....maybe I try to remedy that.

The instruction cannot be re-run with new data until it has signaled completion by asserting the success/error bit or stage, so if you are not accounting for that with unique bits, or preferably, sequencing with stage logic, you are gonna lose states. If the data you are reporting is changing faster than the instruction can run, then you will need to cache pending values in a FIFO.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO