|
@@ -0,0 +1,111 @@
|
|
1
|
+#!/usr/bin/env python
|
|
2
|
+
|
|
3
|
+import sys
|
|
4
|
+import asyncio
|
|
5
|
+from bleak import BleakClient, BleakScanner
|
|
6
|
+from bleak.exc import BleakDBusError
|
|
7
|
+
|
|
8
|
+async def ble_conn(address):
|
|
9
|
+ attempts = 10
|
|
10
|
+ print("Opening device..", end = "", flush=True)
|
|
11
|
+ while attempts > 0:
|
|
12
|
+ try:
|
|
13
|
+ if (attempts % 10) == 0:
|
|
14
|
+ print(".", end = "", flush=True)
|
|
15
|
+
|
|
16
|
+ device = await BleakScanner.find_device_by_address(address)
|
|
17
|
+ client = BleakClient(device)
|
|
18
|
+ print("", flush=True)
|
|
19
|
+ return client
|
|
20
|
+ except BleakDBusError as e:
|
|
21
|
+ attempts -= 1
|
|
22
|
+ if attempts == 0:
|
|
23
|
+ print("", flush=True)
|
|
24
|
+ print(e)
|
|
25
|
+ else:
|
|
26
|
+ await asyncio.sleep(0.1)
|
|
27
|
+
|
|
28
|
+ print("Could not connect to device")
|
|
29
|
+ return None
|
|
30
|
+
|
|
31
|
+async def get_current_temp(client):
|
|
32
|
+ val = await client.read_gatt_char("10110001-5354-4f52-5a26-4249434b454c")
|
|
33
|
+ num = int.from_bytes(val, byteorder="little")
|
|
34
|
+ return num / 10.0
|
|
35
|
+
|
|
36
|
+async def get_target_temp(client):
|
|
37
|
+ val = await client.read_gatt_char("10110003-5354-4f52-5a26-4249434b454c")
|
|
38
|
+ num = int.from_bytes(val, byteorder="little")
|
|
39
|
+ return num / 10.0
|
|
40
|
+
|
|
41
|
+async def set_target_temp(client, temp):
|
|
42
|
+ val = int(temp * 10.0)
|
|
43
|
+ d = val.to_bytes(4, byteorder="little")
|
|
44
|
+ await client.write_gatt_char("10110003-5354-4f52-5a26-4249434b454c", d)
|
|
45
|
+
|
|
46
|
+async def get_unit_is_fahrenheit(client):
|
|
47
|
+ val = await client.read_gatt_char("1010000d-5354-4f52-5a26-4249434b454c")
|
|
48
|
+ num = int.from_bytes(val, byteorder="little")
|
|
49
|
+ return (num & 0x200) != 0
|
|
50
|
+
|
|
51
|
+async def get_state(client):
|
|
52
|
+ val = await client.read_gatt_char("1010000c-5354-4f52-5a26-4249434b454c")
|
|
53
|
+ num = int.from_bytes(val, byteorder="little")
|
|
54
|
+ heater = (num & 0x0020) != 0
|
|
55
|
+ pump = (num & 0x2000) != 0
|
|
56
|
+ return (heater, pump)
|
|
57
|
+
|
|
58
|
+async def set_state(client, state):
|
|
59
|
+ heater, pump = state
|
|
60
|
+ if heater:
|
|
61
|
+ await client.write_gatt_char("1011000f-5354-4f52-5a26-4249434b454c", 0)
|
|
62
|
+ else:
|
|
63
|
+ await client.write_gatt_char("10110010-5354-4f52-5a26-4249434b454c", 0)
|
|
64
|
+ if pump:
|
|
65
|
+ await client.write_gatt_char("10110013-5354-4f52-5a26-4249434b454c", 0)
|
|
66
|
+ else:
|
|
67
|
+ await client.write_gatt_char("10110014-5354-4f52-5a26-4249434b454c", 0)
|
|
68
|
+
|
|
69
|
+async def test_poll(client):
|
|
70
|
+ temp = await get_current_temp(client)
|
|
71
|
+ print("Current Temperature: {}".format(temp))
|
|
72
|
+
|
|
73
|
+ target = await get_target_temp(client)
|
|
74
|
+ print("Target Temperature: {}".format(target))
|
|
75
|
+
|
|
76
|
+ fahrenheit = await get_unit_is_fahrenheit(client)
|
|
77
|
+ if fahrenheit:
|
|
78
|
+ print("Unit is Fahrenheit")
|
|
79
|
+ else:
|
|
80
|
+ print("Unit is Celsius")
|
|
81
|
+
|
|
82
|
+ heater, pump = await get_state(client)
|
|
83
|
+ if heater:
|
|
84
|
+ print("Heater is On")
|
|
85
|
+ else:
|
|
86
|
+ print("Heater is Off")
|
|
87
|
+ if pump:
|
|
88
|
+ print("Pump is On")
|
|
89
|
+ else:
|
|
90
|
+ print("Pump is Off")
|
|
91
|
+
|
|
92
|
+async def test(address):
|
|
93
|
+ device = await ble_conn(address)
|
|
94
|
+
|
|
95
|
+ print("Connecting...")
|
|
96
|
+ async with device as client:
|
|
97
|
+ print("Writing...")
|
|
98
|
+ await set_target_temp(client, 190.0)
|
|
99
|
+
|
|
100
|
+ print("Reading...")
|
|
101
|
+ for i in range(0, 5):
|
|
102
|
+ await test_poll(client)
|
|
103
|
+ print()
|
|
104
|
+ await asyncio.sleep(2.0)
|
|
105
|
+
|
|
106
|
+if __name__ == "__main__":
|
|
107
|
+ if len(sys.argv) <= 1:
|
|
108
|
+ print("Please pass MAC address of device")
|
|
109
|
+ sys.exit(1)
|
|
110
|
+
|
|
111
|
+ asyncio.run(test(sys.argv[1]))
|