-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.py
126 lines (92 loc) · 3.6 KB
/
sensor.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
#!/usr/bin/env python3
import dbus
import dbus.exceptions
import dbus.mainloop.glib
import dbus.service
from bluez import *
class SensorService(Service):
TEST_SVC_UUID = '0a972ce6-5179-11e8-9c2d-fa7ae01bbebc'
def __init__(self, bus, index):
Service.__init__(self, bus, index, self.TEST_SVC_UUID, True)
self.add_characteristic(HumidityCharacteristic(bus, 0, self))
self.add_characteristic(TemperatureCharacteristic(bus, 1, self))
class TemperatureCharacteristic(Characteristic):
TEMP_CHRC_UUID = '0a97304c-5179-11e8-9c2d-fa7ae01bbebc'
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index,
self.TEMP_CHRC_UUID,
['read', 'notify'],
service)
self.value = []
self.notifying = False
self.timer = None
print('init characteristic')
def ReadValue(self, options):
self.read_sensor()
self.PropertiesChanged(GATT_CHRC_IFACE,{ 'Value': self.value }, [])
return self.value
def StartNotify(self):
if self.notifying:
print('Already notifying, nothing to do')
return
self.notifying = True
self.notify_temp()
def StopNotify(self):
if not self.notifying:
print('Not notifying, nothing to do')
return
self.notifying = False
self.timer.stop()
def read_sensor(self):
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
array = bytearray(struct.pack("f", temperature))
self.value = dbus.ByteArray(array)
print('TemperatureCharacteristic Read: ' + repr(self.value))
def notify_temp(self):
if not self.notifying:
return
if self.timer is None:
self.timer = RepeatedTimer(0.5, self.notify_temp)
self.read_sensor()
self.PropertiesChanged(GATT_CHRC_IFACE,{ 'Value': self.value }, [])
class HumidityCharacteristic(Characteristic):
TEMP_CHRC_UUID = 'b168025e-526c-11e8-9c2d-fa7ae01bbebc'
def __init__(self, bus, index, service):
Characteristic.__init__(
self, bus, index,
self.TEMP_CHRC_UUID,
['read', 'notify'],
service)
self.value = []
self.notifying = False
self.timer = None
print('init HumidityCharacteristic')
def ReadValue(self, options):
self.read_sensor()
self.PropertiesChanged(GATT_CHRC_IFACE,{ 'Value': self.value }, [])
return self.value
def StartNotify(self):
if self.notifying:
print('Already notifying, nothing to do')
return
self.notifying = True
self.notify_humidity()
def StopNotify(self):
if not self.notifying:
print('Not notifying, nothing to do')
return
self.notifying = False
self.timer.stop()
def read_sensor(self):
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
array = bytearray(struct.pack("f", humidity))
self.value = dbus.ByteArray(array)
print('Humidityharacteristic Read: ' + repr(self.value))
def notify_humidity(self):
if not self.notifying:
return
if self.timer is None:
self.timer = RepeatedTimer(0.5, self.notify_humidity)
self.read_sensor()
self.PropertiesChanged(GATT_CHRC_IFACE,{ 'Value': self.value }, [])