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?
GO-4-UR-GOAL-MQTT-Integrative-Project/publishing.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
138 lines (104 sloc)
4.89 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 | |
import json,os | |
from datetime import date, datetime | |
import time | |
def on_log(client,userdata,level,buf): | |
"""log the subscriber considering the buffer""" | |
print(buf) | |
def on_connect(client,userdata,flags,rc): | |
"""This function checks if the client has connected to the broker, | |
if this is to be the case, "Connected!" will be printed to the | |
screen.Then, the client would be able to subscribe to all topics. | |
On the other hand, if the client is not connected, "Client disco | |
nnected" will be printed to the screen. | |
""" | |
if rc == 0: | |
client.connected_flag = True # set the flag | |
print("Connected!") # we have connected to the broker | |
client.subscribe("GUG/Topics/#") # subscribed to all topics | |
print("rc") | |
else: | |
print("Bad Connection") | |
print("client disconnected ok") | |
def on_publish(client,userdata,mid): | |
"""" callback function for message published """ | |
print("In on_pub callback = ",mid) | |
def on_disconnect(client,userdata,rc): | |
"""" callback function for client disconnected """ | |
print("Client Disconnected") | |
def on_message(client,userdata,msg,mid): | |
"""client method for recieving messages from the topic""" | |
topic =msg.topic("") # to be used when we can't decode | |
try: | |
data = json.loads.payload.decode("utf8") # decoding the message/ encryption | |
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)), mid) # print device, latitude & longtitude from the message, add time data | |
print(str(data)) | |
except: | |
print("Cannot decode data on topic(0)".format(topic)) | |
client = mqtt.Client() | |
# Enable TLS/SSL encryption in insecure mode | |
client.tls_set(cert_reqs=mqtt.ssl.CERT_NONE) | |
client.on_log = on_log | |
client.on_connect = on_connect # define connection to the client | |
client.on_disconnect = on_disconnect # define disconnection to the client | |
client_connected_flag = True | |
client.on_publish = on_publish | |
client.connect("broker.hivemq.com",8883 ) # connected to the broker on an appropriate port | |
client.loop_start() | |
while not client_connected_flag: | |
print("in wait loop") | |
time.sleep(3) | |
time.sleep(3) | |
print("Publishing") | |
class Topic: | |
"""The Topic Class consists of attributes which defines the number of each topic e.g 5000 Steps. | |
It consits of different methods of each topic, in my case i chose 5 topics ; workout,steps, | |
heart rate, blood pressure and calories.""" | |
def __init__(self,workoutScore_num,stepsTaken_num,caloriesBurnt_num,heartRate_num,bloodPressure_num): | |
self.workoutScore_num = workoutScore_num | |
self.stepsTaken_num =stepsTaken_num | |
self.caloriesBurnt_num = caloriesBurnt_num | |
self.heartRate_num = heartRate_num | |
self.bloodPressure_num = bloodPressure_num | |
def workoutTopic(self): | |
"""This function publishes the message of the workout topic to the client.""" | |
workout=client.publish("GUG/Topics/Workout",f"Your overall Workout Score {self.workoutScore_num}!",0) | |
print("publish return",workout) | |
time.sleep(3) | |
def stepsTopic(self): | |
"""This function publishes the message of the steps topic to the client.""" | |
stepsTaken=client.publish("GUG/Topics/StepsTaken",f"This was your total number of steps {self.stepsTaken_num}",0) | |
print("publish return",stepsTaken) | |
time.sleep(3) | |
def caloriesTopic(self): | |
"""This function publishes the message of the calories topic to the client.""" | |
caloriesBurnt=client.publish("GUG/Topics/caloriesBurnt",f"You burnt {self.caloriesBurnt_num} calories from our workout!",0) | |
print("publish return",caloriesBurnt) | |
time.sleep(3) | |
def heartrateTopic(self): | |
"""This function publishes the message of the heart rate topic to the client.""" | |
heartRate=client.publish("GUG/Topics/heartRate",f"Your heart rate was {self.heartRate_num} bpm",0) | |
print("publish return",heartRate) | |
time.sleep(3) | |
def bloodTopic(self): | |
"""This function publishes the message of the blood pressure topic to the client.""" | |
bloodPressure=client.publish("GUG/Topics/bloodPressure",f" Your Blood Pressure was {self.bloodPressure_num} mmHg!",0) | |
print("publish return",bloodPressure) | |
time.sleep(3) | |
# 90 = Workout score, 5000 = Steps, 394 = Calories burnt, 80 = Heart Rate , 34 = Blood Pressure | |
client1 = Topic(90,5000,394,80,34) | |
# To get the overall workout results | |
client1.workoutTopic() | |
# To get how many steps taken | |
client1.stepsTopic() | |
# To get how many calories burnt | |
client1.caloriesTopic() | |
# To get the users' heart rate | |
client1.heartrateTopic() | |
# To get the users' blood pressure | |
client1.bloodTopic() | |
client.loop_stop() | |
client.disconnect() | |