-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhumi_temp_read.py
More file actions
55 lines (45 loc) · 1.34 KB
/
humi_temp_read.py
File metadata and controls
55 lines (45 loc) · 1.34 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
import sys
import pexpect
DEVICE = "C7:DA:EB:01:F7:38" #REPLACE WITH YOUR MAC ID (FF:FF:FF:FF:FF:FF)
if len(sys.argv) == 2:
DEVICE = str(sys.argv[1])
# Run gatttool interactively.
child = pexpect.spawn("gatttool -I")
# Connect to the device.
print("Connecting to:"),
print(DEVICE)
NOF_REMAINING_RETRY = 3
while True:
try:
child.sendline("connect {0}".format(DEVICE))
child.expect("Connection successful", timeout=5)
except pexpect.TIMEOUT:
NOF_REMAINING_RETRY = NOF_REMAINING_RETRY-1
if (NOF_REMAINING_RETRY>0):
print ("timeout, retry...")
continue
else:
print ("timeout, giving up.")
break
else:
print("Connected!")
child.sendline("char-read-hnd 0x0023")
child.expect("Characteristic value/descriptor: ", timeout=10)
child.expect("\r\n", timeout=10)
print(child.before)
hex_full = child.before;
hex_temp_s = int(hex_full[0:2], 16);
hex_temp = int(hex_full[3:5], 16);
hex_temp_dec = int(hex_full[6:8], 16);
hex_hum = int(hex_full[12:14], 16);
hex_hum_dec = int(hex_full[15:18], 16);
sign = "+";
def hex_to_sign(hex):
if hex_temp_s == 0:
sign = "+"
elif hex_temp_s == 1:
sign = "-"
return sign
hex_to_sign(hex_temp_s)
print(sign,hex_temp,".",hex_temp_dec," | ",hex_hum,".",hex_hum_dec,"%")
break