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 83 Aug-01-2019, 09:29 PM
Last Post: sdf1444
  multi-line messages in raised exceptions? Skaperen 3 120 Aug-01-2019, 02:17 AM
Last Post: Skaperen
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 97 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  logging messages ahead of print messages vindo 6 328 Jun-18-2019, 02:45 PM
Last Post: vindo
  Call a varaible from class in the parent class Clement_2000 1 314 May-09-2019, 11:14 PM
Last Post: michalmonday
  folder PyQt5 created during creation exe atlass218 0 156 Apr-26-2019, 06:25 PM
Last Post: atlass218
  Reading email messages on Outlook johnjohn 9 10,145 Apr-03-2019, 10:41 AM
Last Post: Ken2702
  Get variable from class inside another class hcne 3 265 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  Help with importing a module from a package I created. FWendeburg 2 338 Mar-12-2019, 04:53 PM
Last Post: FWendeburg
  When I read csv file i am getting b'Skipping line messages GuJu 4 472 Mar-07-2019, 11:47 AM
Last Post: GuJu

Forum Jump:


Users browsing this thread: 1 Guest(s)