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
22 from fonts
.ttf
import RobotoMedium
as UserFont
26 format
='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
28 datefmt
='%Y-%m-%d %H:%M:%S')
30 logging
.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
36 # BME280 temperature/pressure/humidity sensor
39 # 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
)
63 font
= ImageFont
.truetype(UserFont
, font_size_large
)
64 smallfont
= ImageFont
.truetype(UserFont
, font_size_small
)
70 # The position of the top bar
73 # Create a values dict to store the data
74 variables
= ["temperature",
96 # Define your own warning limits
97 # The limits definition follows the order of the variables array
98 # Example limits explanation for temperature:
100 # [-273.15 .. 4] -> Dangerously Low
102 # (18 .. 28] -> Normal
104 # (35 .. MAX] -> Dangerously High
105 # DISCLAIMER: The limits provided here are just examples and come
106 # with NO WARRANTY. The authors of this example code claim
107 # NO RESPONSIBILITY if reliance on the following values or this
108 # code in general leads to ANY DAMAGES or DEATH.
109 limits
= [[4,18,28,35],
110 [250,650,1013.25,1015],
112 [-1,-1,30000,100000],
120 # RGB palette for values on the combined screen
121 palette
= [(0,0,255), # Dangerously Low
125 (255,0,0)] # Dangerously High
130 # Displays data and text on the 0.96" LCD
131 def display_text(variable
, data
, unit
):
132 # Maintain length of list
133 values
[variable
] = values
[variable
][1:] + [data
]
134 # Scale the values for the variable between 0 and 1
135 colours
= [(v
- min(values
[variable
]) + 1) / (max(values
[variable
])
136 - min(values
[variable
]) + 1) for v
in values
[variable
]]
137 # Format the variable name and value
138 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
139 logging
.info(message
)
140 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (255, 255, 255))
141 for i
in range(len(colours
)):
142 # Convert the values to colours from red to blue
143 colour
= (1.0 - colours
[i
]) * 0.6
144 r
, g
, b
= [int(x
* 255.0) for x
in colorsys
.hsv_to_rgb(colour
,
146 # Draw a 1-pixel wide rectangle of colour
147 draw
.rectangle((i
, top_pos
, i
+1, HEIGHT
), (r
, g
, b
))
148 # Draw a line graph in black
149 line_y
= HEIGHT
- (top_pos
+ (colours
[i
] * (HEIGHT
- top_pos
)))\
151 draw
.rectangle((i
, line_y
, i
+1, line_y
+1), (0, 0, 0))
152 # Write the text at the top in black
153 draw
.text((0, 0), message
, font
=font
, fill
=(0, 0, 0))
156 # Saves the data to be used in the graphs later and prints to the log
157 def save_data(idx
, data
):
158 variable
= variables
[idx
]
159 # Maintain length of list
160 values
[variable
] = values
[variable
][1:] + [data
]
162 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
163 logging
.info(message
)
166 # Displays all the text on the 0.96" LCD
167 def display_everything():
168 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (0, 0, 0))
170 row_count
= (len(variables
)/column_count
)
171 for i
in range(len(variables
)):
172 variable
= variables
[i
]
173 data_value
= values
[variable
][-1]
175 x
= x_offset
+ ((WIDTH
/column_count
) * (i
/ row_count
))
176 y
= y_offset
+ ((HEIGHT
/row_count
) * (i
% row_count
))
177 message
= "{}: {:.1f} {}".format(variable
[:4], data_value
, unit
)
180 for j
in range(len(lim
)):
181 if data_value
> lim
[j
]:
183 draw
.text((x
, y
), message
, font
=smallfont
, fill
=rgb
)
188 # Get the temperature of the CPU for compensation
189 def get_cpu_temperature():
190 process
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
, universal_newlines
=True)
191 output
, _error
= process
.communicate()
192 return float(output
[output
.index('=') + 1:output
.rindex("'")])
195 # Tuning factor for compensation. Decrease this number to adjust the
196 # temperature down, and increase to adjust up
199 cpu_temps
= [get_cpu_temperature()] * 5
201 delay
= 0.5 # Debounce the proximity tap
202 mode
= 10 # The starting mode
207 values
[v
] = [1] * WIDTH
212 proximity
= ltr559
.get_proximity()
214 # If the proximity crosses the threshold, toggle the mode
215 if proximity
> 1500 and time
.time() - last_page
> delay
:
217 mode
%= (len(variables
)+1)
218 last_page
= time
.time()
220 # One mode for each variable
222 # variable = "temperature"
224 cpu_temp
= get_cpu_temperature()
225 # Smooth out with some averaging to decrease jitter
226 cpu_temps
= cpu_temps
[1:] + [cpu_temp
]
227 avg_cpu_temp
= sum(cpu_temps
) / float(len(cpu_temps
))
228 raw_temp
= bme280
.get_temperature()
229 data
= raw_temp
- ((avg_cpu_temp
- raw_temp
) / factor
)
230 display_text(variables
[mode
], data
, unit
)
233 # variable = "pressure"
235 data
= bme280
.get_pressure()
236 display_text(variables
[mode
], data
, unit
)
239 # variable = "humidity"
241 data
= bme280
.get_humidity()
242 display_text(variables
[mode
], data
, unit
)
248 data
= ltr559
.get_lux()
251 display_text(variables
[mode
], data
, unit
)
254 # variable = "oxidised"
256 data
= gas
.read_all()
257 data
= data
.oxidising
/ 1000
258 display_text(variables
[mode
], data
, unit
)
261 # variable = "reduced"
263 data
= gas
.read_all()
264 data
= data
.reducing
/ 1000
265 display_text(variables
[mode
], data
, unit
)
270 data
= gas
.read_all()
271 data
= data
.nh3
/ 1000
272 display_text(variables
[mode
], data
, unit
)
278 data
= pms5003
.read()
279 except pmsReadTimeoutError
:
280 logging
.warn("Failed to read PMS5003")
282 data
= float(data
.pm_ug_per_m3(1.0))
283 display_text(variables
[mode
], data
, unit
)
289 data
= pms5003
.read()
290 except pmsReadTimeoutError
:
291 logging
.warn("Failed to read PMS5003")
293 data
= float(data
.pm_ug_per_m3(2.5))
294 display_text(variables
[mode
], data
, unit
)
300 data
= pms5003
.read()
301 except pmsReadTimeoutError
:
302 logging
.warn("Failed to read PMS5003")
304 data
= float(data
.pm_ug_per_m3(10))
305 display_text(variables
[mode
], data
, unit
)
307 # Everything on one screen
308 cpu_temp
= get_cpu_temperature()
309 # Smooth out with some averaging to decrease jitter
310 cpu_temps
= cpu_temps
[1:] + [cpu_temp
]
311 avg_cpu_temp
= sum(cpu_temps
) / float(len(cpu_temps
))
312 raw_temp
= bme280
.get_temperature()
313 raw_data
= raw_temp
- ((avg_cpu_temp
- raw_temp
) / factor
)
314 save_data(0, raw_data
)
316 raw_data
= bme280
.get_pressure()
317 save_data(1, raw_data
)
319 raw_data
= bme280
.get_humidity()
320 save_data(2, raw_data
)
322 raw_data
= ltr559
.get_lux()
325 save_data(3, raw_data
)
327 gas_data
= gas
.read_all()
328 save_data(4, gas_data
.oxidising
/ 1000)
329 save_data(5, gas_data
.reducing
/ 1000)
330 save_data(6, gas_data
.nh3
/ 1000)
334 pms_data
= pms5003
.read()
335 except pmsReadTimeoutError
:
336 logging
.warn("Failed to read PMS5003")
338 save_data(7, float(pms_data
.pm_ug_per_m3(1.0)))
339 save_data(8, float(pms_data
.pm_ug_per_m3(2.5)))
340 save_data(9, float(pms_data
.pm_ug_per_m3(10)))
346 except KeyboardInterrupt: