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?
Cartrax/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
76 lines (56 sloc)
3.35 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 | |
""" callback function for connection """ | |
def on_connect(client, userdata, flags, rc): #client method to connect | |
if rc==0: | |
client.connected_flag = True #set flag | |
# Subscribing to all the topics | |
client.subscribe("cartrax/location/data") | |
client.subscribe("cartrax/moisture_sensor/data") | |
client.subscribe("cartrax/humidity_sensor/data") | |
client.subscribe("cartrax/temperature_sensor/data") | |
client.subscribe("cartrax/security/data") | |
client.subscribe("cartrax/speedometer/data") | |
else: | |
print("Bad connection Returned code=",rc) #if we can't connect | |
""" 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: | |
if topic == "cartrax/temperature_sensor/data": | |
data = json.loads(msg.payload.decode("utf8")) #decode message | |
Temperature = [0] | |
moistureSensor = [0] # addded | |
Status = [1] | |
Day = [2] | |
Time_time = [3] | |
print(f"Received from {topic} the following data --> {str(data)}\n") #print the entire message just for fun | |
if topic == "cartrax/moisture_sensor/data": | |
data = json.loads(msg.payload.decode("utf8")) #decode message | |
print(f"Received from {topic} the following data --> {str(data)}\n") | |
if topic == "cartrax/humidity_sensor/data": | |
dataHumidity = json.loads(msg.payload.decode("utf8")) # decode message | |
Humidity = dataHumidity[0] | |
Status = dataHumidity[1] | |
Day = dataHumidity[2] | |
Time_time = dataHumidity[3] | |
print(f"Received from {topic} the following data --> Humidity: {Humidity}, Status: {Status}, Day: {Day}, Time: {Time_time}\n") | |
except Exception as e: | |
print("Error:", e) | |
print("Cannot decode data on topic {0}".format(topic)) # cannot decode; print the topic for the non-decodable message | |
if topic == "cartrax/speedometer/data": | |
dataSpeed = json.loads(msg.payload.decode("utf8")) #decode message | |
Speed = dataSpeed[0] | |
Status = dataSpeed[1] | |
Day = dataSpeed[2] | |
Time_time = dataSpeed[3] | |
print(f"Received from {topic} the following data --> {str(dataSpeed)}\n") #prints entire message | |
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.connect("broker.hivemq.com", 1883) #connect to the broker on an appropriate port | |
client.loop_forever() #keep looping forever (allows realtime subscription) |