Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 I carnt see publish/subscribe messages from the class I created.
#1
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
ichabod801 wrote Jul-10-2019, 03:08 PM:
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How do I create a actor if I subscribe to a specific topic? sdf1444 0 145 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  logging messages ahead of print messages vindo 6 434 Jun-18-2019, 02:45 PM
Last Post: vindo
  Is it safe to use outside a class created inside the function? oneroslavsky 2 869 Dec-27-2017, 01:10 AM
Last Post: mpd

Forum Jump:


Users browsing this thread: 1 Guest(s)