eclipse-paho / eclipse-paho/paho.mqtt.python
How to call publish function independently
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
Hi,
I have created a class that should handle my mqtt publish dynamically (I used the class example:) )
```
import paho.mqtt.client as mqtt
class MQTTClient(mqtt.Client):
def on_connect(self, mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_connect_fail(self, mqttc, obj):
print("Connect failed")
def on_message(self, mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_publish(self, mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(self, mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(self, mqttc, obj, level, string):
print(string)
def run(self):
self.connect("localhost", 1883, 60)
self.subscribe("state", 0)
rc = 0
while rc == 0:
rc = self.loop()
return rc
def publish(self, channel="state", payload="payload"):
#self.connect("localhost", 1883, 60)
self.publish(channel, payload=payload)
```
I have added this publish function in hope that i can call mqttc.publish() from whereever I want. As this is not working at all I am beginning to think that I haven't understood way mqtt works...
Is there a way I can open a connection (in a Thread - it should not block my script where I call publish()) and just pass channel and payload an this gets published to the localhost which I can connect?
Contributor guide
Assessment
This issue has not been assessed yet.