-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensortag.py
78 lines (61 loc) · 2.09 KB
/
Sensortag.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
from __future__ import print_function
import datetime
import sys
import time
from bluepy.btle import BTLEException
from bluepy.sensortag import SensorTag
# configurations to be set accordingly
def enable_sensors(tag):
"""Enable sensors so that readings can be made."""
tag.IRtemperature.enable()
tag.accelerometer.enable()
tag.humidity.enable()
tag.magnetometer.enable()
tag.barometer.enable()
tag.gyroscope.enable()
tag.keypress.enable()
tag.lightmeter.enable()
# tag.battery.enable()
# Some sensors (e.g., temperature, accelerometer) need some time for initialization.
# Not waiting here after enabling a sensor, the first read value might be empty or incorrect.
time.sleep(1.0)
def disable_sensors(tag):
"""Disable sensors to improve battery life."""
tag.IRtemperature.disable()
tag.accelerometer.disable()
tag.humidity.disable()
tag.magnetometer.disable()
tag.barometer.disable()
tag.gyroscope.disable()
tag.keypress.disable()
tag.lightmeter.disable()
# tag.battery.disable()
def get_readings(tag):
"""Get sensor readings and collate them in a dictionary."""
try:
enable_sensors(tag)
readings = {}
# IR sensor
readings["ir_temp"], readings["ir"] = tag.IRtemperature.read()
# humidity sensor
readings["humidity_temp"], readings["humidity"] = tag.humidity.read()
# barometer
readings["baro_temp"], readings["pressure"] = tag.barometer.read()
# luxmeter
readings["light"] = tag.lightmeter.read()
# battery
# readings["battery"] = tag.battery.read()
disable_sensors(tag)
# round to 2 decimal places for all readings
readings = {key: round(value, 2) for key, value in readings.items()}
return readings
except BTLEException as e:
print("Unable to take sensor readings.")
print(e)
return {}
def reconnect(tag):
try:
tag.connect(tag.deviceAddr, tag.addrType)
except Exception as e:
print("Unable to reconnect to SensorTag.")
raise e