-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsBug.py
170 lines (152 loc) · 6.88 KB
/
sBug.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
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>. bscan.py - Simple bluetooth LE
# scanner and data extractor
from bluepy.btle import Scanner, DefaultDelegate
import time
#import pymysql
import struct
#from mqtttest import writetoMQTT
hostname = 'localhost'
username = 'datasrc'
password = 'datasrc000'
database = 'bleSensor'
#Enter the MAC address of the sensor from the lescan
SENSOR_ADDRESS = ["ec:fe:7e:10:92:09"]
SENSOR_LOCATION = ["Garage"]
data = {'battery':-99,'RSSI':-99,'temperature':-99,'light':-99,'position':-99}
class DecodeErrorException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
# print ("Discovered device", dev.addr)
return
elif isNewData:
# print ("Received new data from", dev.addr)
return
def doQueryInsert (conn, addr, loc, temp, accero):
#blesensor table is date, time, addr, location, temp, accero
# cur = conn.cursor()
dostr = 'INSERT INTO data VALUES (CURRENT_DATE(), NOW(), %s, %s, %s, %s);'
#6 cur.execute (dostr, (addr, loc, temp, accero))
# conn.commit()
scanner = Scanner().withDelegate(ScanDelegate())
# myConnection = pymysql.connect (host=hostname, user=username, passwd=password, db=database)
ManuDataHex = []
ReadLoop = True
RetryCount = 0
try:
while (ReadLoop):
devices = scanner.scan(2.0)
ManuData = ""
for dev in devices:
entry = 0
AcceleroData = 0
AcceleroType = 0
TempData = 0
for saddr in SENSOR_ADDRESS:
entry += 1
if (dev.addr == saddr):
print (" ")
print ("Device %s (%s), RSSI=%d dB" % (dev.addr, dev.addrType, dev.rssi))
CurrentDevAddr = saddr
CurrentDevLoc = SENSOR_LOCATION[entry-1]
data['RSSI'] = dev.rssi
for (adtype, desc, value) in dev.getScanData():
# print (adtype)
print (" %s = %s" % (desc, value))
if (desc == "Manufacturer"):
ManuData = value
if (ManuData == ""):
print ("No data received, end decoding")
continue
#print (ManuData)
for i, j in zip (ManuData[::2], ManuData[1::2]):
ManuDataHex.append(int(i+j, 16))
#Start decoding the raw Manufacturer data
if ((ManuDataHex[0] == 0x85) and (ManuDataHex[1] == 0x00)):
print ("Header byte 0x0085 found")
else:
print ("Header byte 0x0085 not found, decoding stop")
continue
id=0
# while (id < len(ManuDataHex)):
# print ("ID: " + str(id))
# print ("Data: " + hex(ManuDataHex[id]))
# id += 1
#Skip Major/Minor Index 5 is 0x3c, indicate
#battery level and config #
if (ManuDataHex[4] == 0x3c):
BatteryLevel = ManuDataHex[5]
data['battery'] = BatteryLevel
ConfigCounter = ManuDataHex[6]
idx = 7
#print "TotalLen: " + str(len(ManuDataHex))
while (idx < len(ManuDataHex)):
# print ("Idx: " + str(idx))
# print ("Data: " + hex(ManuDataHex[idx]))
# LightData = 0
if (ManuDataHex[idx] == 0x41):
#Accerometer data
idx += 1
AcceleroType = ManuDataHex[idx]
AcceleroData = ManuDataHex[idx+1]
# print (hex(AcceleroData))
if (AcceleroData == 0x20):
data['position'] = 'Open'
else:
data['position'] = 'Closed'
idx += 2
elif (ManuDataHex[idx] == 0x42):
#Light data
idx += 1
LightData = ManuDataHex[idx+1]
LightData += ManuDataHex[idx+2] * 0x100
LightData = LightData * (4000/4095)
data['light'] = round(LightData)
idx += 3
elif (ManuDataHex[idx] == 0x43):
#Temperature data
idx += 1
TempData = ManuDataHex[idx]
TempData += ManuDataHex[idx+1] * 0x100
TempData = TempData * 0.0625
data['temperature'] = round(TempData,1)
TempData = (TempData * 1.8) + 32
idx += 2
else:
idx += 1
print ("Device Address: " + CurrentDevAddr)
print ("Device Location: " + CurrentDevLoc)
print ("Battery Level: " + str(BatteryLevel) + "%")
print ("Config Counter: " + str(ConfigCounter))
print ("Accelero Data: " + hex(AcceleroType) + " " + hex(AcceleroData))
print ("Light Data: " + str(round(LightData)))
print ("Temp Data: " + str(round(TempData,1)))
# writetoMQTT(data)
# doQueryInsert(myConnection, CurrentDevAddr, CurrentDevLoc, TempData, AcceleroData)
# ReadLoop = False
time.sleep(60)
else:
continue
print ("btLE not found")
RetryCount +=1
if RetryCount > 3: ReadLoop = False
except DecodeErrorException:
pass