How to loop_forever() while publish to specific topics upon some events?

3,262 views
Skip to first unread message

idyll_yi

unread,
Nov 5, 2018, 9:43:22 PM11/5/18
to MQTT
I initialized a mqtt.Client, subscribe a few topics then loop_forever():

client = paho.Client()
client.on_message = on_message
client.connect(host, 1883)
client.subscribe("SYS/#", qos=1)
client.loop_forever()

My question is, how to response to external event and publish accordingly? 

Pseudocode:
client = paho.Client()
client.on_message = on_message
client.connect(host, 1883)
client.subscribe("SYS/#", qos=1)

# pseudocode start
def wait_for_data():
   
...
msg
= wait_for_data()
client
.publish("SYS/reported/", msg, qos=1)
# pseudocode end

client
.loop_forever()


However wait_for_data will block the whole process and cannot repeatedly listen to events.

Any advice here? Thank you!

Julien Christophe

unread,
Nov 6, 2018, 5:35:44 AM11/6/18
to mq...@googlegroups.com
Hello,

(This mailing list is for the mqtt protocol, for specifics implementations (like paho) you should go to there dedicated support.)

The loop_forever() method blocks the program, it is usefull when you need the program to use infinitly.
If your program is synchron, you can call repeatedly the loop() method, who loops for the amount of time defined (client.loop(0.1) blocks for 100ms)
You can also call the loop_start() (and the loop_stop()) method to start the loop in another thread, then you can control when your programm will exit.
If you still want to use the loop_forever() method, you should start a new thread for your publish function.

Here is a draft of the code i use in python :

import paho.mqtt.client as mqtt
import time

from threading import Thread

class Client(Thread):
    def on_message(self, client, userdata, message):
        print("Received message '" + str(message.payload) + "' on topic '" + message.topic + "' with QoS " + str(message.qos))
        if message.payload == "quit":
            self.stop()

    def on_connect(self, client, userdata, flags, rc):
        print("connected")
        self.subscribe("commands")

    def subscribe(self, topic):
        self.client.subscribe(self.topic_prefix + topic)

    def publish(self, topic, payload):
        self.client.publish(self.topic_prefix + topic, payload=payload)

    def __init__(self, name):
        Thread.__init__(self, name=name)
        self.running = False
        self.client = mqtt.Client(...)
        self.client.on_connect = self.on_connect
        self.client.on_message = self.on_message

        self.client.connect(...)
        self.client.loop_start()

        self.running = True

    def run(self):
        while self.running:
            self.publish("topic", "message")
            time.sleep(2)

    def stop(self):
        if self.running:
            self.running = False

            self.join()

            print("Ending and cleaning up")
            self.client.disconnect()


Here is the doc for the loop_*() methods in paho python : https://www.eclipse.org/paho/clients/python/docs/#network-loop

I hope it will help you,

Julien

--
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.

idyll_yi

unread,
Nov 6, 2018, 9:47:49 PM11/6/18
to MQTT
Thanks for your advice! (sorry for misuse this mailing list. found this list on paho.mqtt.python's readme)

I spotted some confusion here. My intention is to: 
1. loop_forever() to subscribe several topics (listen to msg comes from mqtt broker). Meanwhile, 
2. the loop somehow listen to a local socket, once got some msg sent by other application (on the same local device) , publish it to mqtt broker.

The structure looks like:

mqtt.png



在 2018年11月6日星期二 UTC+8下午6:35:44,Julien Christophe写道:

Slow Bro

unread,
Nov 7, 2018, 6:38:45 AM11/7/18
to mq...@googlegroups.com
“Thanks for your advice! (sorry for misuse this mailing list. found this list on paho.mqtt.python's readme)”

I made the same mistake. The Paho docs really ought to be updated to clarify that there are two lists and what their purposes are. I am not sure who to talk to about that or what needs to be done and I have a full week/can’t look into it yet. 
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages