c0423e6d6a54f3cfaf6dcbf880edc03eda0f72d1
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
)
62 font
= ImageFont
.truetype(UserFont
, font_size
)
66 # The position of the top bar
70 # Displays data and text on the 0.96" LCD
71 def display_text(variable
, data
, unit
):
72 # Maintain length of list
73 values
[variable
] = values
[variable
][1:] + [data
]
74 # Scale the values for the variable between 0 and 1
75 colours
= [(v
- min(values
[variable
]) + 1) / (max(values
[variable
])
76 - min(values
[variable
]) + 1) for v
in values
[variable
]]
77 # Format the variable name and value
78 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
80 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (255, 255, 255))
81 for i
in range(len(colours
)):
82 # Convert the values to colours from red to blue
83 colour
= (1.0 - colours
[i
]) * 0.6
84 r
, g
, b
= [int(x
* 255.0) for x
in colorsys
.hsv_to_rgb(colour
,
86 # Draw a 1-pixel wide rectangle of colour
87 draw
.rectangle((i
, top_pos
, i
+1, HEIGHT
), (r
, g
, b
))
88 # Draw a line graph in black
89 line_y
= HEIGHT
- (top_pos
+ (colours
[i
] * (HEIGHT
- top_pos
)))\
91 draw
.rectangle((i
, line_y
, i
+1, line_y
+1), (0, 0, 0))
92 # Write the text at the top in black
93 draw
.text((0, 0), message
, font
=font
, fill
=(0, 0, 0))
97 # Get the temperature of the CPU for compensation
98 def get_cpu_temperature():
99 process
= Popen(['vcgencmd', 'measure_temp'], stdout
=PIPE
, universal_newlines
=True)
100 output
, _error
= process
.communicate()
101 return float(output
[output
.index('=') + 1:output
.rindex("'")])
104 # Tuning factor for compensation. Decrease this number to adjust the
105 # temperature down, and increase to adjust up
108 cpu_temps
= [get_cpu_temperature()] * 5
110 delay
= 0.5 # Debounce the proximity tap
111 mode
= 0 # The starting mode
115 # Create a values dict to store the data
116 variables
= ["temperature",
130 values
[v
] = [1] * WIDTH
135 proximity
= ltr559
.get_proximity()
137 # If the proximity crosses the threshold, toggle the mode
138 if proximity
> 1500 and time
.time() - last_page
> delay
:
140 mode
%= len(variables
)
141 last_page
= time
.time()
143 # One mode for each variable
145 # variable = "temperature"
147 cpu_temp
= get_cpu_temperature()
148 # Smooth out with some averaging to decrease jitter
149 cpu_temps
= cpu_temps
[1:] + [cpu_temp
]
150 avg_cpu_temp
= sum(cpu_temps
) / float(len(cpu_temps
))
151 raw_temp
= bme280
.get_temperature()
152 data
= raw_temp
- ((avg_cpu_temp
- raw_temp
) / factor
)
153 display_text(variables
[mode
], data
, unit
)
156 # variable = "pressure"
158 data
= bme280
.get_pressure()
159 display_text(variables
[mode
], data
, unit
)
162 # variable = "humidity"
164 data
= bme280
.get_humidity()
165 display_text(variables
[mode
], data
, unit
)
171 data
= ltr559
.get_lux()
174 display_text(variables
[mode
], data
, unit
)
177 # variable = "oxidised"
179 data
= gas
.read_all()
180 data
= data
.oxidising
/ 1000
181 display_text(variables
[mode
], data
, unit
)
184 # variable = "reduced"
186 data
= gas
.read_all()
187 data
= data
.reducing
/ 1000
188 display_text(variables
[mode
], data
, unit
)
193 data
= gas
.read_all()
194 data
= data
.nh3
/ 1000
195 display_text(variables
[mode
], data
, unit
)
201 data
= pms5003
.read()
202 except pmsReadTimeoutError
:
203 logging
.warn("Failed to read PMS5003")
205 data
= float(data
.pm_ug_per_m3(1.0))
206 display_text(variables
[mode
], data
, unit
)
212 data
= pms5003
.read()
213 except pmsReadTimeoutError
:
214 logging
.warn("Failed to read PMS5003")
216 data
= float(data
.pm_ug_per_m3(2.5))
217 display_text(variables
[mode
], data
, unit
)
223 data
= pms5003
.read()
224 except pmsReadTimeoutError
:
225 logging
.warn("Failed to read PMS5003")
227 data
= float(data
.pm_ug_per_m3(10))
228 display_text(variables
[mode
], data
, unit
)
231 except KeyboardInterrupt: