-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
executable file
·172 lines (127 loc) · 3.41 KB
/
sensors.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python3
import smbus, gi, server, time, wiringpi
from contextlib import suppress
gi.require_version('GLib', '2.0')
from gi.repository import GObject, GLib
class sht3x:
def __init__(self, bus, addr=0x44):
self.bus = bus
self.addr = addr
# Clear status reg
bus.write_i2c_block_data(addr, 0x30, [0x41])
def read_temp(self, celsius=True):
self.bus.write_i2c_block_data(self.addr, 0x24, [0x00])
time.sleep(.15)
data = self.bus.read_i2c_block_data(self.addr, 0x24, 6)
humidity = int(round(100. * (data[3] * 256 + data[4]) / 65535.))
temp = data[0] * 256 + data[1]
if (celsius):
temp = -45 + (175 * temp / 65535.)
else:
temp = -49 + (315 * temp / 65535.)
return temp, humidity
class bh1750fvi:
def __init__(self, bus, addr=0x23):
self.bus = bus
self.addr = addr
# Power on
bus.write_byte(addr, 1)
def read_light(self):
# Read one time high res mode 2 (0x21)
val = self.bus.read_i2c_block_data(self.addr, 0x21, 2)
return int(round((val[1] + val[0] * 256) / 1.2))
class tsl2561:
def __init__(self, bus, addr=0x39):
self.bus = bus
self.addr = addr
# Power on and set default gain/timing
bus.write_i2c_block_data(self.addr, 0x80, [3])
self.set_param(2, 16)
def set_param(self, time, gain):
#gain 0: 0x00
#gain 16: 0x10
#time = 0x0 -> 13 ms
#time = 0x1 -> 101 ms
#time = 0x2 -> 402 ms
param = time + gain
self.bus.write_i2c_block_data(self.addr, 0x81, [param])
self.gain = gain
self.time = time
def read_light(self):
bb = self.bus.read_word_data(self.addr, 0xac)
ir = self.bus.read_word_data(self.addr, 0xae)
scale = 1.
if self.gain:
scale /= self.gain
if not self.time:
scale /= .013
elif self.time == 1:
scale /= .101
else:
scale /= .403
return (bb + ir) * scale
class monitor:
def __init__(self, server, light):
self.light = light
self.server = server
self.mode = -2
def set_mode(self, mode):
if self.mode == mode:
return
if mode == 2:
for cam in self.server.cams:
cam.day_mode()
elif mode == 1:
for cam in self.server.cams:
cam.shimmer_mode()
else:
for cam in self.server.cams:
if self.mode > 0:
cam.night_mode()
prop = cam.cam.get_by_name('rpicamsrc')
if prop:
prop.set_property('shutter-speed', 1000000 if mode < 0 else 250000)
self.mode = mode
def update(self):
lux = self.light.read_light()
if lux < 1:
mode = -1
elif lux < 10:
mode = 0
elif lux < 300:
mode = 1
else:
mode = 2
self.set_mode(mode)
def update_text(cam, light, temp, monitor):
try:
[ t, rh ] = temp.read_temp()
l = light.read_light()
s = wiringpi.digitalRead(13)
txt = 't = %.1f - LV = %.0f%% - Lux %i - Stroomklok: %s' % (t, rh, l, "aan" if s else "uit")
mode = '1'
except OSError:
txt = ''
mode = '0'
cam.setprop('annotation-text', txt)
cam.setprop('annotation-mode', mode)
cam.setprop('annotation-text-colour', '1077264') #107010
monitor.update()
return True
if __name__ == '__main__':
main = server.Controller()
main.add_camera('cam1', 'uvch264src')
cam3 = main.add_camera('cam3', 'rpicamsrc')
#with suppress(OSError):
if True:
bus = smbus.SMBus(1)
wiringpi.wiringPiSetup()
# Probe for BH1750-FVI
light = bh1750fvi(bus)
# Probe for sht3x
temp = sht3x(bus)
# Use tsl2561 for choosing day/night mode..
light2 = tsl2561(bus)
monitor = monitor(main, light2)
GLib.timeout_add_seconds(1, update_text, cam3, light, temp, monitor)
main.run()