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.
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")
    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)
print("creating new instance")
mqttc = mqtt.Client("Laser") #create new instance
mqttc.on_message=on_message #attach function to callback
print("connecting to broker")
mqttc.connect(broker_address) #connect to broker
mqttc.loop_start() #start the loop
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
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.

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)