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?
Week2_Tutorial/owntracks_mqtt_question2
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
68 lines (51 sloc)
2.73 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 mqtt #necessary imports | |
import json, os | |
from datetime import date, datetime | |
import time | |
print("Hello, Visual Studio") | |
""" callback function for connection """ | |
def on_connect(client, userdata, flags, rc): #client method to connect | |
if rc==0: | |
client.connected_flag = True #set flag | |
print("connected OK Returned code=",rc) #let us know we connected to the broker | |
client.subscribe("owntracks/KS/#") #we are connected, so subscribe to the topic. wildcard means any device | |
print("rc") | |
else: | |
print("Bad connection Returned code=",rc) #if we can't connect | |
def on_disconnect(client, userdata, rc): | |
def on_publish(client, userdata, mid): | |
def on_subscribe(client, userdata, mid, qos): | |
""" callback function for messages received """ | |
def on_message( client, userdata, msg ): #client method to get messages from topic | |
topic = msg.topic #for use when we can't decode | |
try: | |
data = json.loads(msg.payload.decode("utf8")) #decode message | |
day = date.today() #time functions | |
clock = datetime.now() | |
time = datetime.time(clock) | |
print ("TID = {0} is currently at {1}, {2},{3},{4}".format(data['tid'], data['lat'], data['lon'], str(day), str(time))) | |
#print device, latitude and longitude from the message; add time data | |
print(str(data), 0) #print the entire message just for fun | |
except: | |
print ("Cannot decode data on topic {0}".format(topic)) #cannot decode; print the topic for the non-decodable message | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.username_pw_set("KS", "mqttBROKER") #associate authentication details with the client | |
#client.tls_set("mqtt.coventry.ac.uk.crt") #certificate for client. needs to be in the same directory as this script | |
client.connect("broker.hivemq.com", 1883) #connect to the broker on an appropriate port | |
topic="Home/bulb/temperature1" | |
try: | |
client.connect("broker.hivemq.com", 1883) | |
except: | |
print("can't connect") | |
client.loop_start() | |
while not client.connected_flag #wait in a loop | |
time.sleep(1) | |
ret=client.subscribe(topic,0) | |
print("subscribed returned ", ret) | |
time.sleep(4) | |
msg="closed" | |
time.sleep(4) | |
client.loop_stop() | |
client.disconnect() |