Replies: 8 comments 3 replies
-
The |
Beta Was this translation helpful? Give feedback.
-
I've probably not understood what you are asking as the whole point of mqtt is for client programs to publish messages (information or requests?) to another client program that can do something with the information or respond by publishing a response message. I have a very simple snippet to hand so with a quick tweak I show it here in case its what you are after. The snippet cheats a bit because it starts off by connecting to a broker, but then publishes a mqtt message to which the program has subscribed to. Normally one would expect this published request message to come from another program. But as you can see the program subscribes to the topic of 'test/request'. When it receives such a message (and it will because the program also publishes a message with this topic) then the message callback function check for a message on the subject of 'test/request' and if so calls the function response(). This function then prints a supposes sensor value (set as a global variable for this example) and also publish an mqtt message on the topic of 'test/RESPONSE' with the payload containing the value of the supposed sensor. Maybe this very noddy and cut down mqtt example helps?
|
Beta Was this translation helpful? Give feedback.
-
Thanks for this snippet. This is what I was looking for. It shows the structure I need. |
Beta Was this translation helpful? Give feedback.
-
Peter I was aware of your latest spiffing event based interface of a month or so back but I have been amiss in amending my personal mqtt_as snippets. To ensure I use the event interface going forward I have reworked the noddy demo snippet I posted to include in my snippet files and include it here in case it helps the OP to see a comparison to the callback based snippet I showed.
|
Beta Was this translation helpful? Give feedback.
-
The sticking point for me was "if topic == 'test/request" because this shows that I have to use different topics instead of one topic with all options and routines. |
Beta Was this translation helpful? Give feedback.
-
@klinwer, good to hear you are now unstuck :-) . Its probably obvious, but a couple of things maybe worth reminding for one starting out to use mqtt. First is to remember that only mqtt messages that are subscribed to will be received by your program so you don't necessarily need the if topic ==. In the example snippet only one message topic was subscribed to so in that case the 'if topic' is superfluous. Of course for multiple subscriptions it does allow different actions to be taken depending on the message topic. Also my example did not receive a message payload with the test/request topic (as none was published), but the message payload could also signal a particular action. The payload could also be formatted, perhaps as a dictionary of values etc. For example it could contain a json formatted message where different actions could be taken depending on the content of a particular item in the structure. Likewise the test/RESPONSE message with the payload of the sensor value could have been sending a message that contained a whole bunch of different sensor readings etc. and the received program may take some action based on just one of the payload items. (e.g. take action to switch on heater if temperature is too low, but just display both the temperature the humidity value as received in the payload.) And as regards the @peterhinch mqtt_as, as I understand it, both event and callback options will work fine, but the preference is to go with the event based option for new programs. I am cogitating if I should change any of my existing programs as I presume the event based option is more efficient and works better with asynchronous programs and I will probably learn something in the exercise. (updates to Peters programs have been keeping me on my toes over the last few months :-) ) You caught me drinking a cuppa so forgive the probably unwarranted over voluble response. |
Beta Was this translation helpful? Give feedback.
-
Your hints are really helpfull. I will test the event based option also. The other solution is already working fine on an ESP32c3. I also have to incooperate the new topic structure on server side. |
Beta Was this translation helpful? Give feedback.
-
The update to the event based option is working. 👍 Thanks a lot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello to everyone.
I try to implement a respond message like a sensor value after a request message. It should be a kind of handshake.
Are there any specific examples which I could use as a starting point.
Beta Was this translation helpful? Give feedback.
All reactions