mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-22 21:45:48 +00:00
Update with functionality
OpenHaystack integration to be added for location data
This commit is contained in:
parent
30b791011e
commit
1272b78f69
37
main.py
37
main.py
|
@ -1,6 +1,37 @@
|
|||
import requests
|
||||
import json
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# Pull data from MQTT broker
|
||||
def OpenHayStackLocation():
|
||||
# TODO: replace with OpenHaystack script
|
||||
return 0, 0
|
||||
|
||||
# Any necessary local processing
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("Connected with result code "+str(rc))
|
||||
client.subscribe("/data")
|
||||
|
||||
# Push data to firebase backend
|
||||
def on_message(client, userdata, msg):
|
||||
if msg.topic == "/data":
|
||||
data = json.loads(msg.payload)
|
||||
firebase_url = "https://leg-barkr.nw.r.appspot.com/readings/save"
|
||||
firebase_headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Device-ID": data["devID"]
|
||||
}
|
||||
loc_lat, loc_lon = OpenHayStackLocation()
|
||||
firebase_data = {
|
||||
"air_temp": data["air_temp"],
|
||||
"cumulative_steps_today": data["day_steps"],
|
||||
"humidity": data["hum_perc"],
|
||||
"latitude": loc_lat,
|
||||
"longitude": loc_lon,
|
||||
"skin_temp": data['pet_temp']
|
||||
}
|
||||
r = requests.post(url = firebase_url, json = firebase_data, params = None, headers = firebase_headers) # add any other necessary values in request
|
||||
print(r.status_code, r.reason, r.text)
|
||||
|
||||
client = mqtt.Client("DataPuller")
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
client.connect("add8.duckdns.org", 8883, 60)
|
||||
client.loop_forever()
|
||||
|
|
6
requirements.txt
Normal file
6
requirements.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
certifi==2021.10.8
|
||||
charset-normalizer==2.0.12
|
||||
idna==3.3
|
||||
paho-mqtt==1.6.1
|
||||
requests==2.27.1
|
||||
urllib3==1.26.8
|
Loading…
Reference in a new issue