forked from RohmSemiconductor/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathble_logger_sens_scan.py
executable file
·83 lines (74 loc) · 3.11 KB
/
ble_logger_sens_scan.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
#!/usr/bin/env python3
# coding: utf-8
################################################################################
# BLE Logger
#
# Bluetooth LE拡張ボードを搭載した Spresenseが送信するビーコンを受信し、
# ビーコンに含まれる、温度センサ値と気圧センサ値を取得し、表示します。
#
# Copyright (c) 2019 Wataru KUNINO
################################################################################
#【インストール方法】
# bluepy (Bluetooth LE interface for Python)をインストールしてください
# sudo pip3 install bluepy
#
#【実行方法】
# 実行するときは sudoを付与してください
# sudo ./ble_logger_sens_scan.py &
#
#【参考文献】
# 本プログラムを作成するにあたり下記を参考にしました
# https://ianharvey.github.io/bluepy-doc/scanner.html
interval = 1.01 # 動作間隔
from bluepy import btle
from sys import argv
import getpass
from time import sleep
def payval(num, bytes=1, sign=False):
global val
a = 0
for i in range(0, bytes):
a += (256 ** i) * int(val[(num - 2 + i) * 2 : (num - 1 + i) * 2],16)
if sign:
if a >= 2 ** (bytes * 8 - 1):
a -= 2 ** (bytes * 8)
return a
scanner = btle.Scanner()
while True:
# BLE受信処理
try:
devices = scanner.scan(interval)
except Exception as e:
print("ERROR",e)
if getpass.getuser() != 'root':
print('使用方法: sudo', argv[0])
exit()
sleep(interval)
continue
# 受信データについてBLEデバイス毎の処理
for dev in devices:
# print("\nDevice %s (%s), RSSI=%d dB, Connectable=%s, updateCount=%d" % (dev.addr, dev.addrType, dev.rssi, dev.connectable, dev.updateCount))
print("\nDevice %s (%s), RSSI=%d dB, Connectable=%s" % (dev.addr, dev.addrType, dev.rssi, dev.connectable))
isRohmMedal = False
sensors = dict()
for (adtype, desc, val) in dev.getScanData():
print(" %3d %s = %s" % (adtype, desc, val))
if adtype == 8 and val[0:10] == 'LapisDev':
isRohmMedal = True
if isRohmMedal and desc == 'Manufacturer':
# センサ値を辞書型変数sensorsへ代入
sensors['ID'] = hex(payval(2,2))
sensors['Temperature'] = -45 + 175 * payval(4,2) / 65536
sensors['Pressure'] = payval(6,3) / 2048
sensors['SEQ'] = payval(9)
sensors['RSSI'] = dev.rssi
# 画面へ表示
print(' ID =',sensors['ID'])
if sensors['ID'] == '0x179' and sensors['SEQ'] == 15:
print(' No Sensors')
isRohmMedal = False
break
print(' SEQ =',sensors['SEQ'])
print(' Temperature =',round(sensors['Temperature'],2),'℃')
print(' Pressure =',round(sensors['Pressure'],3),'hPa')
print(' RSSI =',sensors['RSSI'],'dB')