-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·60 lines (46 loc) · 1.19 KB
/
main.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
from pybleno import *
import sys
import signal
from hm10_characteristic import *
print('raspberrypi - echo');
bleno = Bleno()
hm10 = HM10_Characteristic('ffe1')
def onStateChange(state):
print('on -> stateChange: ' + state);
if (state == 'poweredOn'):
bleno.startAdvertising('raspberrypi', ['ffe0'])
else:
bleno.stopAdvertising();
bleno.on('stateChange', onStateChange)
def onAdvertisingStart(error):
print('on -> advertisingStart: ' + ('error ' + error if error else 'success'));
if not error:
bleno.setServices([
BlenoPrimaryService({
'uuid': 'ffe0',
'characteristics': [
hm10
]
})
])
bleno.on('advertisingStart', onAdvertisingStart)
bleno.start()
i = 0
timestamp = 0
lastTimestamp = 0
while True:
try:
i += 1
timestamp = hm10.data_in['timestamp']
if timestamp != lastTimestamp:
print(hm10.data_in)
lastTimestamp = timestamp
hm10.data_out = [i%256]
time.sleep(0.1)
except:
break
bleno.stopAdvertising()
bleno.disconnect()
hm10.sendThread.exit()
print ('terminated.')
sys.exit(1)