-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLYWSD03MMC.py
executable file
·87 lines (67 loc) · 2.34 KB
/
LYWSD03MMC.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/python3 -u
from bluepy import btle
import os
import re
from dataclasses import dataclass
from collections import deque
import threading
import time
import math
debug = False
@dataclass
class Measurement:
temperature: float
humidity: int
voltage: float
battery: int = 0
myMeasurement = Measurement(0, 0, 0, 0)
class MyDelegate(btle.DefaultDelegate):
def __init__(self, params):
btle.DefaultDelegate.__init__(self)
# ... initialise here
def handleNotification(self, cHandle, data):
try:
if debug: print('Got data:', data)
myMeasurement.temperature = int.from_bytes(
data[0:2], byteorder='little', signed=True) / 100.
myMeasurement.humidity = int.from_bytes(data[2:3],
byteorder='little')
voltage = int.from_bytes(data[3:5], byteorder='little') / 1000.
# 3.1 or above --> 100% 2.1 --> 0 %
myMeasurement.battery = min(int(round((voltage - 2.1), 2) * 100),
100)
myMeasurement.voltage = voltage
except Exception as e:
print(e)
# Initialisation -------
def connect(address):
p = btle.Peripheral(address)
p.withDelegate(MyDelegate("abc"))
return p
def LYWSD03MMC(address):
connected = False
try:
if not connected:
if debug: print("Connecting to BLE: " + address)
p = connect(address)
connected = True
if p.waitForNotifications(4000):
if debug: print("End of BLE notification")
p.disconnect()
return myMeasurement.temperature, myMeasurement.humidity, myMeasurement.battery
except Exception as e:
if debug: print("Connection lost: ", e)
if connected is True: # First connection abort after connected
connected = False
return 0, 0, 0
def main(m):
print('Reading MAC.:', m)
t, h, b = LYWSD03MMC(m)
print('temperature.: '+str(t)+'°')
print('humidity....: '+str(h)+'%')
print('battery.....: '+str(b)+'%')
if __name__ == "__main__":
main('58:2D:34:51:B7:FB')
# gatttool -I -b 58:2D:34:51:B7:FB
# char-read-uuid ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6
# characteristics