-
Notifications
You must be signed in to change notification settings - Fork 1
/
blemini_run.py
129 lines (104 loc) · 3.33 KB
/
blemini_run.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
#!/usr/bin/env python3
#
# Copyright 2013-2021 Thomas Ackermann
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Read digital inputs from a RedBearLabs BLEmini.
# BLEmini is a BLE (Bluetooth low energy) device so by
# automating gatttool with pexpect we are able to read and write values.
#
# Usage: blemini_test.py BLUETOOTH_ADR
#
# To find the address of your BLEmini run 'sudo hcitool lescan'
# To power up your bluetooth dongle run 'sudo hciconfig hci0 up'
#
#
# BLEMini Biscuit v2.0 handles
# (discovered by 'primary' resp. 'characteristic' cmd in gatttool):
#
# 0x12: read list of bytes transferred to BLEMini via serial connection
# 0x16: write byte to BLEMini to be received via serial connection
# 0x19: set baudrate for serial port (value is remanent; possible values are:
# 00=9600, 01=19200, 02=38400, 03=57600(default), 04=115200)
#
import os
import sys
import time
import random
import pexpect
from datetime import datetime
adr = sys.argv[1]
logdir = "/tmp/pihome"
try:
os.mkdir(logdir)
except:
pass
val = 0
exc = 0
act = 0
stamp = ""
handle = ""
def log_values():
print(adr, " DATA 0x%02X, EXC %d, ACT %d" % (val, exc, act))
print(adr, " STAMP '%s'" % stamp)
data = open(logdir+"/"+adr, "w")
data.write(" DATA 0x%02X\n" % val)
data.write("EXCPT %d\n" % exc)
data.write("ACTEX %d\n" % act)
data.write("STAMP '%s'\n" % stamp)
data.close()
while True:
try:
print(adr, " Trying to connect ...")
tool = pexpect.spawn('gatttool -b ' + adr + ' --interactive', encoding='utf-8')
tool.expect('\[LE\]>')
tool.sendline('connect')
tool.expect('success')
# find handle
cons = pexpect.run('hcitool con', encoding='utf-8')
cons = cons.split("\r\n")
for con in cons:
if adr in con:
tok = con.split()
handle = tok[4]
# set baudrate to 9600
tool.sendline('char-write-cmd 0x19 00')
tool.expect('\[LE\]>')
while True:
rnd = random.randint(0, 10)
# send byte to BLEMini
tool.sendline('char-write-cmd 0x16 ' + ("%02X" % rnd))
tool.expect('\[LE\]>')
# read byte/s from BLEMini
tool.sendline('char-read-hnd 0x12')
tool.expect('descriptor: .*? \r')
v = tool.after.split()
val = int(float.fromhex(v[1]))
stamp = datetime.now().ctime()
act = 0
log_values()
#time.sleep(3600)
time.sleep(300)
except KeyboardInterrupt:
tool.sendline('quit')
tool.close()
sys.exit()
except:
if handle != "":
pexpect.run('sudo hcitool ledc ' + handle, encoding='utf-8')
tool.sendline('quit')
tool.close(force=True)
exc = exc + 1
act = 1