mqttClient.publish() not working outside callback loop

123 views
Skip to first unread message

shantanu...@gmail.com

unread,
Sep 21, 2016, 12:42:24 AM9/21/16
to MQTT
Hi,
I am running a simple PIR motion sensor with MQTT using mosquitto broker.
The problem is everything seems to be working fine. Except one thing.

When I enter the callback loop, after receiving a Particular message from the broker, I enter a second Function. But when i do mqtt publish, it does not publish a message.

More simplified, this is my process:
Enter callback.
         Receive message from broker
                     IF (message == "xyz")
                             user_defined_function();

 This user defined function runs the PIR sensor operation and i need it to publish as soon as motion is detected. It does glow a led when motion is detected. But it is not publishing.

What can be the problem?

Karl Palsson

unread,
Sep 21, 2016, 5:40:59 AM9/21/16
to mq...@googlegroups.com
Particularly with qos >= 1, it won't publish until the callback exits and your mosquitto loop can run again. The qos flows for the message you received can't complete until your callback is complete, and your publish is after that.
signature.asc

Roger Light

unread,
Sep 21, 2016, 5:44:59 AM9/21/16
to MQTT
Karl,

You are correct but I presume in this case the original poster isn't
using libmosquitto because the subject mentions mqttClient.publish().

Cheers,

Roger
> --
> To learn more about MQTT please visit http://mqtt.org
> ---
> You received this message because you are subscribed to the Google Groups "MQTT" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
> To post to this group, send email to mq...@googlegroups.com.
> Visit this group at https://groups.google.com/group/mqtt.
> For more options, visit https://groups.google.com/d/optout.

shantanu...@gmail.com

unread,
Sep 21, 2016, 6:02:39 AM9/21/16
to MQTT
Thats right Roger. I am running MQTT client on a micro-controller. This micro-controller is hooked with a motion sensor. First the micro-controller receives a ACTIVATE request and ONLY THEN starts the Sensor. The sensor starts detection. I want it to publish "DETECTION" message while in that loop. But this Sensor function is called from within the callback loop. 

Also, the sensor shuts down once a "DEACTIVATE" request is received.

So that's my problem. It is not publishing when motion is detected.
Reply all
Reply to author
Forward
0 new messages