|
@@ -1,75 +1,105 @@
|
1
|
1
|
#!/usr/bin/env python
|
2
|
2
|
|
3
|
|
-import simplepyble
|
4
|
|
-from scan import ble_scan
|
|
3
|
+import uasyncio as asyncio
|
|
4
|
+import bluetooth
|
5
|
5
|
import time
|
6
|
6
|
|
7
|
|
-serviceUuidVolcano3 = "10100000-5354-4f52-5a26-4249434b454c"
|
8
|
|
-serviceUuidVolcano4 = "10110000-5354-4f52-5a26-4249434b454c"
|
|
7
|
+from scan import ble_scan
|
|
8
|
+
|
|
9
|
+serviceUuidVolcano3 = bluetooth.UUID("10100000-5354-4f52-5a26-4249434b454c")
|
|
10
|
+serviceUuidVolcano4 = bluetooth.UUID("10110000-5354-4f52-5a26-4249434b454c")
|
9
|
11
|
|
10
|
|
-def ble_conn(address, adapter):
|
11
|
|
- dev = ble_scan(address, adapter)
|
|
12
|
+async def ble_conn(address):
|
|
13
|
+ dev = await ble_scan(address)
|
12
|
14
|
|
13
|
|
- if dev != None:
|
14
|
|
- address = dev.address()
|
|
15
|
+ if dev:
|
|
16
|
+ address = dev.addr_hex()
|
15
|
17
|
print("Connecting to '{}'...".format(address))
|
16
|
|
- dev.connect()
|
|
18
|
+ connection = await dev.connect()
|
|
19
|
+ return connection
|
17
|
20
|
|
18
|
|
- return dev
|
|
21
|
+ return None
|
19
|
22
|
|
20
|
|
-def get_current_temp(device):
|
21
|
|
- val = device.read(serviceUuidVolcano4, "10110001-5354-4f52-5a26-4249434b454c")
|
22
|
|
- num = int.from_bytes(val, byteorder="little")
|
|
23
|
+async def get_current_temp(device):
|
|
24
|
+ service = await device.service(serviceUuidVolcano4)
|
|
25
|
+ uuid = bluetooth.UUID("10110001-5354-4f52-5a26-4249434b454c")
|
|
26
|
+ characteristic = await service.characteristic(uuid)
|
|
27
|
+ val = await characteristic.read()
|
|
28
|
+ num = int.from_bytes(val, "little")
|
23
|
29
|
return num / 10.0
|
24
|
30
|
|
25
|
|
-def get_target_temp(device):
|
26
|
|
- val = device.read(serviceUuidVolcano4, "10110003-5354-4f52-5a26-4249434b454c")
|
27
|
|
- num = int.from_bytes(val, byteorder="little")
|
|
31
|
+async def get_target_temp(device):
|
|
32
|
+ service = await device.service(serviceUuidVolcano4)
|
|
33
|
+ uuid = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
|
|
34
|
+ characteristic = await service.characteristic(uuid)
|
|
35
|
+ val = await characteristic.read()
|
|
36
|
+ num = int.from_bytes(val, "little")
|
28
|
37
|
return num / 10.0
|
29
|
38
|
|
30
|
|
-def set_target_temp(device, temp):
|
|
39
|
+async def set_target_temp(device, temp):
|
31
|
40
|
val = int(temp * 10.0)
|
32
|
|
- d = val.to_bytes(4, byteorder="little")
|
33
|
|
- device.write_request(serviceUuidVolcano4, "10110003-5354-4f52-5a26-4249434b454c", d)
|
34
|
|
-
|
35
|
|
-def get_unit_is_fahrenheit(device):
|
36
|
|
- val = device.read(serviceUuidVolcano3, "1010000d-5354-4f52-5a26-4249434b454c")
|
37
|
|
- num = int.from_bytes(val, byteorder="little")
|
|
41
|
+ d = val.to_bytes(4, "little")
|
|
42
|
+ service = await device.service(serviceUuidVolcano4)
|
|
43
|
+ uuid = bluetooth.UUID("10110003-5354-4f52-5a26-4249434b454c")
|
|
44
|
+ characteristic = await service.characteristic(uuid)
|
|
45
|
+ await characteristic.write(d)
|
|
46
|
+
|
|
47
|
+async def get_unit_is_fahrenheit(device):
|
|
48
|
+ service = await device.service(serviceUuidVolcano3)
|
|
49
|
+ uuid = bluetooth.UUID("1010000d-5354-4f52-5a26-4249434b454c")
|
|
50
|
+ characteristic = await service.characteristic(uuid)
|
|
51
|
+ val = await characteristic.read()
|
|
52
|
+ num = int.from_bytes(val, "little")
|
38
|
53
|
return (num & 0x200) != 0
|
39
|
54
|
|
40
|
|
-def get_state(device):
|
41
|
|
- val = device.read(serviceUuidVolcano3, "1010000c-5354-4f52-5a26-4249434b454c")
|
42
|
|
- num = int.from_bytes(val, byteorder="little")
|
|
55
|
+async def get_state(device):
|
|
56
|
+ service = await device.service(serviceUuidVolcano3)
|
|
57
|
+ uuid = bluetooth.UUID("1010000c-5354-4f52-5a26-4249434b454c")
|
|
58
|
+ characteristic = await service.characteristic(uuid)
|
|
59
|
+ val = await characteristic.read()
|
|
60
|
+ num = int.from_bytes(val, "little")
|
43
|
61
|
heater = (num & 0x0020) != 0
|
44
|
62
|
pump = (num & 0x2000) != 0
|
45
|
63
|
return (heater, pump)
|
46
|
64
|
|
47
|
|
-def set_state(device, state):
|
|
65
|
+async def set_state(device, state):
|
48
|
66
|
heater, pump = state
|
49
|
67
|
if heater:
|
50
|
|
- device.write_request(serviceUuidVolcano4, "1011000f-5354-4f52-5a26-4249434b454c", 0)
|
|
68
|
+ service = await device.service(serviceUuidVolcano4)
|
|
69
|
+ uuid = bluetooth.UUID("1011000f-5354-4f52-5a26-4249434b454c")
|
|
70
|
+ characteristic = await service.characteristic(uuid)
|
|
71
|
+ await characteristic.write(int(0).to_bytes(1, "little"))
|
51
|
72
|
else:
|
52
|
|
- device.write_request(serviceUuidVolcano4, "10110010-5354-4f52-5a26-4249434b454c", 0)
|
|
73
|
+ service = await device.service(serviceUuidVolcano4)
|
|
74
|
+ uuid = bluetooth.UUID("10110010-5354-4f52-5a26-4249434b454c")
|
|
75
|
+ characteristic = await service.characteristic(uuid)
|
|
76
|
+ await characteristic.write(int(0).to_bytes(1, "little"))
|
53
|
77
|
if pump:
|
54
|
|
- device.write_request(serviceUuidVolcano4, "10110013-5354-4f52-5a26-4249434b454c", 0)
|
|
78
|
+ service = await device.service(serviceUuidVolcano4)
|
|
79
|
+ uuid = bluetooth.UUID("10110013-5354-4f52-5a26-4249434b454c")
|
|
80
|
+ characteristic = await service.characteristic(uuid)
|
|
81
|
+ await characteristic.write(int(0).to_bytes(1, "little"))
|
55
|
82
|
else:
|
56
|
|
- device.write_request(serviceUuidVolcano4, "10110014-5354-4f52-5a26-4249434b454c", 0)
|
|
83
|
+ service = await device.service(serviceUuidVolcano4)
|
|
84
|
+ uuid = bluetooth.UUID("10110014-5354-4f52-5a26-4249434b454c")
|
|
85
|
+ characteristic = await service.characteristic(uuid)
|
|
86
|
+ await characteristic.write(int(0).to_bytes(1, "little"))
|
57
|
87
|
|
58
|
88
|
if __name__ == "__main__":
|
59
|
|
- def test_poll(device):
|
60
|
|
- temp = get_current_temp(device)
|
|
89
|
+ async def test_poll(device):
|
|
90
|
+ temp = await get_current_temp(device)
|
61
|
91
|
print("Current Temperature: {}".format(temp))
|
62
|
92
|
|
63
|
|
- target = get_target_temp(device)
|
|
93
|
+ target = await get_target_temp(device)
|
64
|
94
|
print("Target Temperature: {}".format(target))
|
65
|
95
|
|
66
|
|
- fahrenheit = get_unit_is_fahrenheit(device)
|
|
96
|
+ fahrenheit = await get_unit_is_fahrenheit(device)
|
67
|
97
|
if fahrenheit:
|
68
|
98
|
print("Unit is Fahrenheit")
|
69
|
99
|
else:
|
70
|
100
|
print("Unit is Celsius")
|
71
|
101
|
|
72
|
|
- heater, pump = get_state(device)
|
|
102
|
+ heater, pump = await get_state(device)
|
73
|
103
|
if heater:
|
74
|
104
|
print("Heater is On")
|
75
|
105
|
else:
|
|
@@ -79,36 +109,19 @@ if __name__ == "__main__":
|
79
|
109
|
else:
|
80
|
110
|
print("Pump is Off")
|
81
|
111
|
|
82
|
|
- def test(address, adapter):
|
83
|
|
- device = ble_conn(address, adapter)
|
|
112
|
+ async def test(address):
|
|
113
|
+ device = await ble_conn(address)
|
84
|
114
|
if device == None:
|
85
|
115
|
return
|
86
|
116
|
|
87
|
|
- try:
|
|
117
|
+ async with device:
|
88
|
118
|
print("Writing...")
|
89
|
|
- set_target_temp(device, 190.0)
|
|
119
|
+ await set_target_temp(device, 190.0)
|
90
|
120
|
|
91
|
121
|
print("Reading...")
|
92
|
122
|
for i in range(0, 5):
|
93
|
|
- test_poll(device)
|
|
123
|
+ await test_poll(device)
|
94
|
124
|
print()
|
95
|
125
|
time.sleep(2.0)
|
96
|
|
- except:
|
97
|
|
- print("Disconnecting")
|
98
|
|
- client.disconnect()
|
99
|
|
-
|
100
|
|
- raise
|
101
|
|
-
|
102
|
|
- print("Disconnecting")
|
103
|
|
- client.disconnect()
|
104
|
|
-
|
105
|
|
- import sys
|
106
|
|
-
|
107
|
|
- adapter = None
|
108
|
|
- mac = None
|
109
|
|
- if len(sys.argv) > 1:
|
110
|
|
- adapter = int(sys.argv[1])
|
111
|
|
- if len(sys.argv) > 2:
|
112
|
|
- mac = sys.argv[2]
|
113
|
126
|
|
114
|
|
- test(mac, adapter)
|
|
127
|
+ asyncio.run(test(None))
|