SHARE
TWEET

Untitled

a guest Feb 27th, 2020 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paho.mqtt.client as mqtt #import the client1
  2. import time
  3. ############
  4. def on_message(client, userdata, message):
  5.     message_string = str(message.payload.decode("utf-8"))
  6.     print("message received " , message_string)
  7.     print("message topic=",message.topic)
  8.     print("message qos=",message.qos)
  9.     print("message retain flag=",message.retain)
  10. ########################################
  11. broker_address="192.168.4.1"
  12. pub_topic = "Base_station"
  13. sub_topic = "Train1"
  14.  
  15. #broker_address="iot.eclipse.org"
  16. print("Creating new instance")
  17. client = mqtt.Client("P1") #create new instance
  18. client.on_message = on_message #attach function to callback
  19. print("Connecting to broker")
  20. client.connect(broker_address) #connect to broker
  21. client.loop_start() #start the loop
  22.  
  23. print("Subscribing to topic",sub_topic)
  24. client.subscribe(sub_topic)
  25. print("Publishing message to topic",pub_topic)
  26. client.publish(pub_topic,"This message comes from Base Station")
  27. time.sleep(4) # wait
  28. #client.loop_stop() #stop the loop
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top