9 # Transitional fix for breaking change in LTR559
10 from ltr559
import LTR559
15 from bme280
import BME280
16 from pms5003
import PMS5003
, ReadTimeoutError
as pmsReadTimeoutError
17 from enviroplus
import gas
18 from subprocess
import PIPE
, Popen
20 from PIL
import ImageDraw
21 from PIL
import ImageFont
25 format
='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
27 datefmt
='%Y-%m-%d %H:%M:%S')
29 logging
.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
35 # BME280 temperature/pressure/humidity sensor
38 # PMS5003 particulate sensor
42 # Create ST7735 LCD display class
43 st7735
= ST7735
.ST7735(
56 HEIGHT
= st7735
.height
58 # Set up canvas and font
59 img
= Image
.new('RGB', (WIDTH
, HEIGHT
), color
=(0, 0, 0))
60 draw
= ImageDraw
.Draw(img
)
61 path
= os
.path
.dirname(os
.path
.realpath(__file__
))
62 font
= ImageFont
.truetype(path
+ "/fonts/Asap/Asap-Bold.ttf", 20)
63 smallfont
= ImageFont
.truetype(path
+ "/fonts/Asap/Asap-Bold.ttf", 10)
69 # The position of the top bar
72 # Create a values dict to store the data
73 variables
= ["temperature",
95 # Define your own warning limits
96 # The limits definition follows the order of the variables array
97 # Example limits explanation for temperature:
99 # [-273.15 .. 4] -> Dangerously Low
101 # (18 .. 28] -> Normal
103 # (35 .. MAX] -> Dangerously High
104 # DISCLAIMER: The limits provided here are just examples and come
105 # with NO WARRANTY. The authors of this example code claim
106 # NO RESPONSIBILITY if reliance on the following values or this
107 # code in general leads to ANY DAMAGES or DEATH.
108 limits
= [[4,18,28,35],
109 [250,650,1013.25,1015],
111 [-1,-1,30000,100000],
119 # RGB palette for values on the combined screen
120 palette
= [(0,0,255), # Dangerously Low
124 (255,0,0)] # Dangerously High
129 # Displays data and text on the 0.96" LCD
130 def display_text(variable
, data
, unit
):
131 # Maintain length of list
132 values
[variable
] = values
[variable
][1:] + [data
]
133 # Scale the values for the variable between 0 and 1
134 colours
= [(v
- min(values
[variable
]) + 1) / (max(values
[variable
])
135 - min(values
[variable
]) + 1) for v
in values
[variable
]]
136 # Format the variable name and value
137 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
138 logging
.info(message
)
139 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (255, 255, 255))
140 for i
in range(len(colours
)):
141 # Convert the values to colours from red to blue
142 colour
= (1.0 - colours
[i
]) * 0.6
143 r
, g
, b
= [int(x
* 255.0) for x
in colorsys
.hsv_to_rgb(colour
,
145 # Draw a 1-pixel wide rectangle of colour
146 draw
.rectangle((i
, top_pos
, i
+1, HEIGHT
), (r
, g
, b
))
147 # Draw a line graph in black
148 line_y
= HEIGHT
- (top_pos
+ (colours
[i
] * (HEIGHT
- top_pos
)))\
150 draw
.rectangle((i
, line_y
, i
+1, line_y
+1), (0, 0, 0))
151 # Write the text at the top in black
152 draw
.text((0, 0), message
, font
=font
, fill
=(0, 0, 0))
155 # Saves the data to be used in the graphs later and prints to the log
156 def save_data(idx
, data
):
157 variable
= variables
[idx
]
158 # Maintain length of list
159 values
[variable
] = values
[variable
][1:] + [data
]
161 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
162 logging
.info(message
)
165 # Displays all the text on the 0.96" LCD
166 def display_everything():
167 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (0, 0, 0))
169 row_count
= (len(variables
)/column_count
)
170 for i
in xrange(len(variables
)):
171 variable
= variables
[i
]
172 data_value
= values
[variable
][-1]
174 x
= x_offset
+ ((WIDTH
/column_count
) * (i
/ row_count
))
175 y
= y_offset
+ ((HEIGHT
/row_count
) * (i
% row_count
))
176 message
= "{}: {:.1f} {}".format(variable
[:4], data_value
, unit
)
179 for j
in xrange(len(lim
)):
180 if data_value
> lim
[j
]:
182 draw
.text((x
, y
), message
, font
=smallfont
, fill
=rgb
)
187 # Get the temperature of the CPU for compensation
188 def get_cpu_temperature():
189 process
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
, universal_newlines
=True)
190 output
, _error
= process
.communicate()
191 return float(output
[output
.index('=') + 1:output
.rindex("'")])
194 # Tuning factor for compensation. Decrease this number to adjust the
195 # temperature down, and increase to adjust up
198 cpu_temps
= [get_cpu_temperature()] * 5
200 delay
= 0.5 # Debounce the proximity tap
201 mode
= 10 # The starting mode
206 values
[v
] = [1] * WIDTH
211 proximity
= ltr559
.get_proximity()
213 # If the proximity crosses the threshold, toggle the mode
214 if proximity
> 1500 and time
.time() - last_page
> delay
:
216 mode
%= (len(variables
)+1)
217 last_page
= time
.time()
219 # One mode for each variable
221 # variable = "temperature"
223 cpu_temp
= get_cpu_temperature()
224 # Smooth out with some averaging to decrease jitter
225 cpu_temps
= cpu_temps
[1:] + [cpu_temp
]
226 avg_cpu_temp
= sum(cpu_temps
) / float(len(cpu_temps
))
227 raw_temp
= bme280
.get_temperature()
228 data
= raw_temp
- ((avg_cpu_temp
- raw_temp
) / factor
)
229 display_text(variables
[mode
], data
, unit
)
232 # variable = "pressure"
234 data
= bme280
.get_pressure()
235 display_text(variables
[mode
], data
, unit
)
238 # variable = "humidity"
240 data
= bme280
.get_humidity()
241 display_text(variables
[mode
], data
, unit
)
247 data
= ltr559
.get_lux()
250 display_text(variables
[mode
], data
, unit
)
253 # variable = "oxidised"
255 data
= gas
.read_all()
256 data
= data
.oxidising
/ 1000
257 display_text(variables
[mode
], data
, unit
)
260 # variable = "reduced"
262 data
= gas
.read_all()
263 data
= data
.reducing
/ 1000
264 display_text(variables
[mode
], data
, unit
)
269 data
= gas
.read_all()
270 data
= data
.nh3
/ 1000
271 display_text(variables
[mode
], data
, unit
)
277 data
= pms5003
.read()
278 except pmsReadTimeoutError
:
279 logging
.warn("Failed to read PMS5003")
281 data
= float(data
.pm_ug_per_m3(1.0))
282 display_text(variables
[mode
], data
, unit
)
288 data
= pms5003
.read()
289 except pmsReadTimeoutError
:
290 logging
.warn("Failed to read PMS5003")
292 data
= float(data
.pm_ug_per_m3(2.5))
293 display_text(variables
[mode
], data
, unit
)
299 data
= pms5003
.read()
300 except pmsReadTimeoutError
:
301 logging
.warn("Failed to read PMS5003")
303 data
= float(data
.pm_ug_per_m3(10))
304 display_text(variables
[mode
], data
, unit
)
306 # Everything on one screen
307 cpu_temp
= get_cpu_temperature()
308 # Smooth out with some averaging to decrease jitter
309 cpu_temps
= cpu_temps
[1:] + [cpu_temp
]
310 avg_cpu_temp
= sum(cpu_temps
) / float(len(cpu_temps
))
311 raw_temp
= bme280
.get_temperature()
312 raw_data
= raw_temp
- ((avg_cpu_temp
- raw_temp
) / factor
)
313 save_data(0, raw_data
)
315 raw_data
= bme280
.get_pressure()
316 save_data(1, raw_data
)
318 raw_data
= bme280
.get_humidity()
319 save_data(2, raw_data
)
321 raw_data
= ltr559
.get_lux()
324 save_data(3, raw_data
)
326 gas_data
= gas
.read_all()
327 save_data(4, gas_data
.oxidising
/ 1000)
328 save_data(5, gas_data
.reducing
/ 1000)
329 save_data(6, gas_data
.nh3
/ 1000)
333 pms_data
= pms5003
.read()
334 except pmsReadTimeoutError
:
335 logging
.warn("Failed to read PMS5003")
337 save_data(7, float(pms_data
.pm_ug_per_m3(1.0)))
338 save_data(8, float(pms_data
.pm_ug_per_m3(2.5)))
339 save_data(9, float(pms_data
.pm_ug_per_m3(10)))
345 except KeyboardInterrupt: