Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paho.mqtt.client as mqtt #import the client1
- import time
- ############
- def on_message(client, userdata, message):
- message_string = str(message.payload.decode("utf-8"))
- print("message received " , message_string)
- print("message topic=",message.topic)
- print("message qos=",message.qos)
- print("message retain flag=",message.retain)
- ########################################
- broker_address="192.168.4.1"
- pub_topic = "Base_station"
- sub_topic = "Train1"
- #broker_address="iot.eclipse.org"
- print("Creating new instance")
- client = mqtt.Client("P1") #create new instance
- client.on_message = on_message #attach function to callback
- print("Connecting to broker")
- client.connect(broker_address) #connect to broker
- client.loop_start() #start the loop
- print("Subscribing to topic",sub_topic)
- client.subscribe(sub_topic)
- print("Publishing message to topic",pub_topic)
- client.publish(pub_topic,"This message comes from Base Station")
- time.sleep(4) # wait
- #client.loop_stop() #stop the loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement