10 from bme280
import BME280
11 from pms5003
import PMS5003
12 from enviroplus
import gas
14 from PIL
import ImageDraw
15 from PIL
import ImageFont
17 print("""all-in-one.py - Displays readings from all of Enviro plus' sensors
23 # BME280 temperature/pressure/humidity sensor
26 # PMS5003 particulate sensor
29 # Create ST7735 LCD display class
30 st7735
= ST7735
.ST7735(
43 HEIGHT
= st7735
.height
45 # Set up canvas and font
46 img
= Image
.new('RGB', (WIDTH
, HEIGHT
), color
=(0, 0, 0))
47 draw
= ImageDraw
.Draw(img
)
48 path
= os
.path
.dirname(os
.path
.realpath(__file__
))
49 font
= ImageFont
.truetype(path
+ "/fonts/Asap/Asap-Bold.ttf", 20)
53 # The position of the top bar
56 # Displays data and text on the 0.96" LCD
57 def display_text(variable
, data
, unit
):
58 # Maintain length of list
59 values
[variable
] = values
[variable
][1:] + [data
]
60 # Scale the values for the variable between 0 and 1
61 colours
= [(v
- min(values
[variable
]) + 1) / (max(values
[variable
]) - min(values
[variable
]) + 1) for v
in values
[variable
]]
62 # Format the variable name and value
63 message
= "{}: {:.1f} {}".format(variable
[:4], data
, unit
)
65 draw
.rectangle((0, 0, WIDTH
, HEIGHT
), (255, 255, 255))
66 for i
in range(len(colours
)):
67 # Convert the values to colours from red to blue
68 colour
= (1.0 - colours
[i
]) * 0.6
69 r
, g
, b
= [int(x
* 255.0) for x
in colorsys
.hsv_to_rgb(colour
, 1.0, 1.0)]
70 # Draw a 1-pixel wide rectangle of colour
71 draw
.rectangle((i
, top_pos
, i
+1, HEIGHT
), (r
, g
, b
))
72 # Draw a line graph in black
73 line_y
= HEIGHT
- (top_pos
+ (colours
[i
] * (HEIGHT
- top_pos
))) + top_pos
74 draw
.rectangle((i
, line_y
, i
+1, line_y
+1), (0, 0, 0))
75 # Write the text at the top in black
76 draw
.text((0, 0), message
, font
=font
, fill
=(0, 0, 0))
79 delay
= 0.5 # Debounce the proximity tap
80 mode
= 0 # The starting mode
84 # Create a values dict to store the data
85 variables
= ["temperature",
99 values
[v
] = [1] * WIDTH
104 proximity
= ltr559
.get_proximity()
106 # If the proximity crosses the threshold, toggle the mode
107 if proximity
> 1500 and time
.time() - last_page
> delay
:
109 mode
%= len(variables
)
110 last_page
= time
.time()
112 # One mode for each variable
114 variable
= "temperature"
116 data
= bme280
.get_temperature()
117 display_text(variable
, data
, unit
)
120 variable
= "pressure"
122 data
= bme280
.get_pressure()
123 display_text(variable
, data
, unit
)
126 variable
= "humidity"
128 data
= bme280
.get_humidity()
129 display_text(variable
, data
, unit
)
135 data
= ltr559
.get_lux()
138 display_text(variable
, data
, unit
)
141 variable
= "oxidised"
143 data
= gas
.read_all()
144 data
= data
.oxidising
/ 1000
145 display_text(variable
, data
, unit
)
150 data
= gas
.read_all()
151 data
= data
.reducing
/ 1000
152 display_text(variable
, data
, unit
)
157 data
= gas
.read_all()
158 data
= data
.nh3
/ 1000
159 display_text(variable
, data
, unit
)
164 data
= pms5003
.read()
165 data
= data
.pm_ug_per_m3(1.0)
166 display_text(variable
, data
, unit
)
171 data
= pms5003
.read()
172 data
= data
.pm_ug_per_m3(2.5)
173 display_text(variable
, data
, unit
)
178 data
= pms5003
.read()
179 data
= data
.pm_ug_per_m3(10)
180 display_text(variable
, data
, unit
)
183 except KeyboardInterrupt: