Python Forum
I carnt see publish/subscribe messages from the class I created. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I carnt see publish/subscribe messages from the class I created. (/thread-19686.html)



I carnt see publish/subscribe messages from the class I created. - sdf1444 - Jul-10-2019

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