I carnt see publish/subscribe messages from the class I created.

19 views
Skip to first unread message

Spencer Du

unread,
Jul 10, 2019, 10:07:43 AM7/10/19
to MQTT
Hi I carnt seem to print the publish/subscribe messages. I want the publish/subscribe messages from the class I created. The class I created must be kept. Any suggestions much appreciated. 

import paho.mqtt.client as mqtt
import time
class MyMQTTClass(mqtt.Client):    
    def on_connect(mqttc, obj, flags, rc):
        print("rc: "+str(rc))
        print("Subscribing to topic","microscope/light_sheet_microscope/laser")
        mqttc.subscribe("microscope/light_sheet_microscope/laser")
    def on_message(mqttc, userdata, message):
        print("message received " ,str(message.payload.decode("utf-8")))
        print("message topic=",message.topic)
        print("message qos=",message.qos)
        print("message retain flag=",message.retain)
    def on_publish(mqttc, obj, mid):
        print("mid: "+str(mid))
    def on_subscribe(mqttc, obj, mid, granted_qos):
        print("Subscribed: "+str(mid)+" "+str(granted_qos))
    def on_log(client, userdata, level, buf):
        print("log: ",buf)
broker_address="broker.hivemq.com"
#broker_address="iot.eclipse.org"
print("creating new instance")
mqttc = mqtt.Client("Laser") #create new instance
mqttc.on_message=on_message #attach function to callback
mqttc.on_publish=on_publish
mqttc.on_connect=on_connect
mqttc.on_log=on_log
print("connecting to broker")
mqttc.connect(broker_address) #connect to broker
mqttc.loop_start() #start the loop
time.sleep(2)
print("Publishing message to topic","microscope/light_sheet_microscope/laser")
mqttc.publish("microscope/light_sheet_microscope/laser","Hello World Im a laser!")
time.sleep(2) # wait
mqttc.loop_stop() #stop the loop

Thanks

Simon Walters

unread,
Jul 10, 2019, 4:43:04 PM7/10/19
to mq...@googlegroups.com
Take your def on_connect on_message, on_publish out of your class
Do they work? 

As much as you'd want to (for unknown reason) I don't think you can have them inside an un-referenced class

Simon

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/mqtt/1cc168fa-8d22-417f-88b0-cbdec2616c19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages