-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path_P512_BLEMijia.py
277 lines (258 loc) · 8.73 KB
/
_P512_BLEMijia.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
#############################################################################
####################### BLE Mijia plugin for RPIEasy ########################
#############################################################################
#
# Xiaomi Mijia Bluetooth Temperature Humidity Sensor plugin.
# Can be used when BLE compatible Bluetooth dongle, and BluePy is installed.
#
# Copyright (C) 2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
from bluepy import btle
import threading
import time
import binascii
from random import uniform
import lib.lib_blehelper as BLEHelper
BATTERY_HANDLE = 0x0018
TEMP_HUM_WRITE_HANDLE = 0x0010
TEMP_HUM_READ_HANDLE = 0x000E
TEMP_HUM_WRITE_VALUE = bytearray([0x01, 0x10])
class Plugin(plugin.PluginProto):
PLUGIN_ID = 512
PLUGIN_NAME = "Environment - BLE Xiaomi Mijia Temperature&Humidity (TESTING)"
PLUGIN_VALUENAME1 = "Temperature"
PLUGIN_VALUENAME2 = "Humidity"
PLUGIN_VALUENAME3 = "Battery"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_BLE
self.vtype = rpieGlobals.SENSOR_TYPE_TEMP_HUM
self.valuecount = 2
self.senddataoption = True
self.recdataoption = False
self.timeroption = True
self.timeroptional = True
self.connected = False
self.formulaoption = True
self.BLEPeripheral = False
self.cproc = False
self.waitnotifications = False
self.conninprogress = False
self.readinprogress = False
self.battery = 0
self.lastbatteryreq = 0
# self.lastread = 0
self.preread = 3000
self._lastdataservetime = 0
self._nextdataservetime = 0
self.TARR = []
self.HARR = []
self.failures = 0
self.blestatus = None
def webform_load(self): # create html page for settings
bledevs = BLEHelper.find_hci_devices()
options = []
optionvalues = []
if bledevs:
for bd in bledevs:
options.append(bd)
try:
optionvalues.append(int(bd[3:]))
except:
optionvalues.append(bd[3:])
webserver.addFormSelector("Local Device","plugin_512_dev",len(options),options,optionvalues,None,int(self.taskdevicepluginconfig[2]))
webserver.addFormTextBox("Device Address","plugin_512_addr",str(self.taskdevicepluginconfig[0]),20)
webserver.addFormNote("Enable blueetooth then <a href='blescanner'>scan MJ_HT_ address</a> first.")
webserver.addFormCheckBox("Add Battery value for non-Domoticz system","plugin_512_bat",self.taskdevicepluginconfig[1])
return True
def webform_save(self,params): # process settings post reply
self.taskdevicepluginconfig[0] = str(webserver.arg("plugin_512_addr",params)).strip().lower()
self.taskdevicepluginconfig[1] = (webserver.arg("plugin_512_bat",params)=="on")
try:
self.taskdevicepluginconfig[2] = int(webserver.arg("plugin_512_dev",params))
except:
self.taskdevicepluginconfig[2] = 0
self.plugin_init()
return True
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.readinprogress = False
self.conninprogress = False
self.waitnotifications = False
self.connected = False
self.TARR = []
self.HARR = []
self.uservar[0] = 0
self.uservar[1] = 0
if self.enabled:
self.timer1s = True
self.ports = str(self.taskdevicepluginconfig[0])
self.battery = -1
self._nextdataservetime = rpieTime.millis()-self.preread
self._lastdataservetime = 0
# self.lastread = 0
self.failures = 0
if self.taskdevicepluginconfig[1]:
self.valuecount = 3
self.vtype = rpieGlobals.SENSOR_TYPE_TRIPLE
else:
self.valuecount = 2
self.vtype = rpieGlobals.SENSOR_TYPE_TEMP_HUM
try:
devnum = int(self.taskdevicepluginconfig[2])
self.blestatus = BLEHelper.BLEStatus[devnum]
except:
pass
else:
self.ports = ""
self.timer1s = False
def timer_once_per_second(self):
if self.enabled:
if self._nextdataservetime-rpieTime.millis()<=self.preread:
if self.conninprogress==False and self.connected==False:
self.waitnotifications = False
if len(self.taskdevicepluginconfig[0])>10:
self.cproc = threading.Thread(target=self.connectproc)
self.cproc.daemon = True
self.cproc.start()
return self.timer1s
def plugin_read(self):
result = False
if self.enabled:
if len(self.TARR)>0 and len(self.HARR)>0:
self.get_battery_value()
if self.battery==-1:
self.get_battery_value()
self.set_value(1,self.TARR[-1],False)
if self.taskdevicepluginconfig[1]:
self.set_value(2,self.HARR[-1],False)
self.set_value(3,self.battery,False,susebattery=self.battery)
else:
self.set_value(2,self.HARR[-1],False,susebattery=self.battery)
self.plugin_senddata(pusebattery=self.battery)
if self.interval>10:
self.disconnect()
self.TARR = []
self.HARR = []
self._lastdataservetime = rpieTime.millis()
self._nextdataservetime = self._lastdataservetime + (self.interval*1000) - self.preread
self.failures = 0
else:
self.isconnected()
def connectproc(self):
try:
if self.blestatus.isscaninprogress():
self.blestatus.requeststopscan(self.taskindex)
return False
except Exception as e:
return False
self.conninprogress = True
while self.blestatus.norequesters()==False or self.blestatus.nodataflows()==False:
time.sleep(0.5)
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG_MORE,"BLE line not free for P512! "+str(self.blestatus.dataflow))
self.blestatus.registerdataprogress(self.taskindex)
prevstate = self.connected
try:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE connection initiated to "+str(self.taskdevicepluginconfig[0]))
time.sleep(uniform(0.4,1.8))
self.BLEPeripheral = btle.Peripheral(str(self.taskdevicepluginconfig[0]),iface=self.taskdevicepluginconfig[2])
self.connected = True
self.failures = 0
self.BLEPeripheral.setDelegate( TempHumDelegate(self.callbackfunc) )
except:
self.connected = False
self.conninprogress = False
self.isconnected()
if self.connected==False:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BLE connection failed "+str(self.taskdevicepluginconfig[0]))
self.blestatus.unregisterdataprogress(self.taskindex)
self.failures = self.failures +1
if self.failures>5:
self._nextdataservetime = rpieTime.millis()+(self.interval*5000)
self._lastdataservetime = self._nextdataservetime
return False
else:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"BLE connected to "+str(self.taskdevicepluginconfig[0]))
self.waitnotifications = True
self.get_battery_value()
rpieTime.addsystemtimer(3,self.isconnected,[-1])
def request_temp_hum_value(self,d=None):
res = False
try:
if self.BLEPeripheral is not None:
self.BLEPeripheral.writeCharacteristic(TEMP_HUM_WRITE_HANDLE, TEMP_HUM_WRITE_VALUE)
res = True
except Exception as e:
res = False
# print(e)
return res
def isconnected(self,d=None):
if self.connected:
self.connected = self.request_temp_hum_value()
if self.connected==False:
self.blestatus.unregisterdataprogress(self.taskindex)
return self.connected
def get_battery_value(self):
if ((time.time()-self.lastbatteryreq)>600) or (self.battery<=0):
battery = 0
try:
battery = self.BLEPeripheral.readCharacteristic(BATTERY_HANDLE)
self.lastbatteryreq = time.time()
except Exception as e:
self.connected = False
try:
if battery:
self.battery = int(binascii.b2a_hex(battery),16)
except Exception as e:
pass
return self.battery
def callbackfunc(self,temp=None,hum=None):
# print("cb",temp,hum)
self.blestatus.unregisterdataprogress(self.taskindex)
if self.enabled:
self.TARR.append(temp)
self.HARR.append(hum)
# self.lastread = rpieTime.millis()
if rpieTime.millis()-self._lastdataservetime>2000:
self.plugin_read()
def disconnect(self):
# print("disconn")
self.connected = False
self.waitnotifications = False
if self.enabled:
try:
self.blestatus.unregisterdataprogress(self.taskindex)
if self.BLEPeripheral is not None:
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass
def plugin_exit(self):
self.disconnect()
# def __exit__(self,type,value,traceback):
# self.__del__()
class TempHumDelegate(btle.DefaultDelegate):
def __init__(self,callback):
self.callback = callback
btle.DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
if (cHandle == TEMP_HUM_READ_HANDLE) and data is not None:
temp = None
hum = None
try:
data = data.decode("utf-8").strip(' \t\r\n\0')
tarr = data.split(" ")
td = tarr[0].split("=")
th = tarr[1].split("=")
temp = float(td[1])
hum = float(th[1])
except Exception as e:
print(e)
# print(temp,hum) # DEBUG
self.callback(temp,hum)