Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Week5_Tutorial/mqtt_flights_subscribing.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
65 lines (53 sloc)
1.65 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import paho.mqtt.client as paho | |
import time | |
broker="192.168.1.157" | |
from queue import Queue | |
Q = Queue() | |
port=1883 | |
topic1 ="Flights/Air France/#" | |
flights={} | |
#define callback | |
def on_log(client, userdata, level, buf): | |
print("log: ",buf) | |
def on_message(client, userdata, message): | |
#time.sleep(1) | |
msg=str(message.payload.decode("utf-8")) | |
m="Flight"+message.topic+""+msg | |
if message.topic in flights: | |
if flights[message.topic]==msg | |
print("Not changes", message.topic) | |
else: | |
flights[message.topic]=msg | |
Q.put(m) | |
else | |
flights[message.topic]=msg | |
def on_subscribe(client,userdata,mid,granted_qos); #create function for callback | |
print("subscribed with qos", granted_qos," ", mid, "\n") | |
pass | |
def on_disconnect(client, userdata, rc) | |
def on_connect(client,userdata,flags,rc) | |
m="Connected flags" + str(flags) + "result code" +str(rc) | |
client= paho.Client("Python1", False) #create client object #assign function to callback client1.connect(broker,port) #establish connection client1.publish("house/bulb1","on") | |
client.on_log=on_log | |
###### | |
client.on_subscribe=on_subscribe #assign function to callback | |
client.on_message=on_message | |
client.on_connect=on_connect | |
client.on_disconnect=on_disconnect | |
print("connecting to broker ",broker) | |
client.connect(broker,port) | |
print("Subscribing to topic", topic1) | |
time.sleep(1) | |
time.sleep(2) | |
r=client.subscribe(topic1,0) #subscribe single topic | |
tstart=time.time() | |
while True: | |
client.loop(001) | |
if time.time()-tstart>=3 | |
while not Q empty(): | |
m=Q.get() | |
print(m) | |
print(*********) | |
tstart=time.time() | |
#time.sleep(1) | |
client.disconnect() #disconnect |