-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbbq_app.py
More file actions
66 lines (51 loc) · 2.16 KB
/
bbq_app.py
File metadata and controls
66 lines (51 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import pygatt
import csv
import datetime
# Connect to the device
connect_key = bytearray.fromhex("2101020304000000000000") # Update this with correct connection key you obtained in part 2
enable_notifications = bytearray.fromhex("0b0100000000") # This value is correct, no need to update
bbq_mac = "ff:ff:ff:ff:ff:ff" # Update this with your BBQ device's MAC address
adapter = pygatt.GATTToolBackend()
def fahrenheit(celcius):
return int(round(celcius * (9/5.0) + 32))
# Process and save the realtime data
def handle_notification(handle, value):
"""
handle -- integer, characteristic read handle the data was received on
value -- bytearray, the data returned in the notification
"""
temps = {"timestamp": str(datetime.datetime.now())}
for i in range(0,8,2):
celcius = int(int.from_bytes(value[i:i+2], "little") / 10)
f_degrees = fahrenheit(celcius)
temps[f"Probe-{int(i/2)+1}"] = f_degrees
with open("temperature_log.csv", "a") as csv_file:
writer = csv.writer(csv_file)
writer.writerow([temps[field] for field in temps])
try:
adapter.start()
try:
device = adapter.connect(bbq_mac,timeout=20)
except:
print("Couldn't connect to the device, retrying...")
device = adapter.connect(bbq_mac,timeout=20)
# Send the connection key to the 0x29
print("Pairing with the device...")
device.char_write_handle(0x0029, connect_key)
# Enable notifications by writing to 0x34
device.char_write_handle(0x0034, enable_notifications)
print("Connected with the device.")
with open('temperature_log.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["Timestamp", "Probe 1", "Probe 2", "Probe 3", "Probe 4"])
# Subscribe and listen for notifications of the realtime data
try:
device.subscribe("0000fff4-0000-1000-8000-00805f9b34fb", callback=handle_notification)
except Exception as e:
try:
device.subscribe("0000fff4-0000-1000-8000-00805f9b34fb", callback=handle_notification)
except:
pass
input("Enter any key to quit....")
finally:
adapter.stop()