Hi Shoe,
Nearly there, i used the code as described but get this error
lines[0] = ["{:.2f}".format(air_quality_score)]
IndexError: list assignment index out of range
(program exited with code: 1)
Press return to continue
import bme680
import random
import blinkt
import time
import colorsys
from sys import exit
import unicornhathd
blinkt.set_clear_on_exit()
print("""Estimate indoor air quality
Runs the sensor for a burn-in period, then uses a
combination of relative humidity and gas resistance
to estimate indoor air quality as a percentage.
Press Ctrl+C to exit
""")
sensor = bme680.BME680()
lines = []
def show_text(lines):
colours = [tuple([int(n * 255) for n in colorsys.hsv_to_rgb(x/float(len(lines)), 1.0, 1.0)]) for x in range(len(lines))]
FONT = ("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 12)
width, height = unicornhathd.get_shape()
text_x = width
text_y = 2
font_file, font_size = FONT
font = ImageFont.truetype(font_file, font_size)
text_width, text_height = width, 0
for line in lines:
w, h = font.getsize(line)
text_width += w + width
text_height = max(text_height,h)
text_width += width + text_x + 1
image = Image.new("RGB", (text_width,max(16, text_height)), (0,0,0))
draw = ImageDraw.Draw(image)
offset_left = 0
for index, line in enumerate(lines):
draw.text((text_x + offset_left, text_y), line, colours[index], font=font)
offset_left += font.getsize(line)[0] + width
for scroll in range(text_width - width):
for x in range(width):
for y in range(height):
pixel = image.getpixel((x+scroll, y))
r, g, b = [int(n) for n in pixel]
unicornhathd.set_pixel(width-1-x, y, r, g, b)
unicornhathd.show()
time.sleep(0.01)
# These oversampling settings can be tweaked to
# change the balance between accuracy and noise in
# the data.
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
sensor.set_gas_status(bme680.ENABLE_GAS_MEAS)
sensor.set_gas_heater_temperature(320)
sensor.set_gas_heater_duration(150)
sensor.select_gas_heater_profile(0)
# start_time and curr_time ensure that the
# burn_in_time (in seconds) is kept track of.
start_time = time.time()
curr_time = time.time()
burn_in_time = 3 #should be 300 which is 300/60=5 minutes
burn_in_data = []
try:
# Collect gas resistance burn-in values, then use the average
# of the last 50 values to set the upper limit for calculating
# gas_baseline.
print("Collecting gas resistance burn-in data for 5 mins\n")
while curr_time - start_time < burn_in_time:
curr_time = time.time()
if sensor.get_sensor_data() and sensor.data.heat_stable:
gas = sensor.data.gas_resistance
burn_in_data.append(gas)
print("Gas: {0} Ohms".format(gas))
blinkt.clear()
blinkt.set_pixel(3, 255, 0, 1.0)
blinkt.set_pixel(4, 255, 0, 1.0)
blinkt.show()
time.sleep(1)
gas_baseline = sum(burn_in_data[-50:]) / 50.0
# Set the humidity baseline to 40%, an optimal indoor humidity.
hum_baseline = 40.0
# This sets the balance between humidity and gas reading in the
# calculation of air_quality_score (25:75, humidity:gas)
hum_weighting = 0.25
print("Gas baseline: {0} Ohms, humidity baseline: {1:.2f} %RH\n".format(gas_baseline, hum_baseline))
while True:
if sensor.get_sensor_data() and sensor.data.heat_stable:
gas = sensor.data.gas_resistance
gas_offset = gas_baseline - gas
hum = sensor.data.humidity
hum_offset = hum - hum_baseline
# Calculate hum_score as the distance from the hum_baseline.
if hum_offset > 0:
hum_score = (100 - hum_baseline - hum_offset) / (100 - hum_baseline) * (hum_weighting * 100)
else:
hum_score = (hum_baseline + hum_offset) / hum_baseline * (hum_weighting * 100)
# Calculate gas_score as the distance from the gas_baseline.
if gas_offset > 0:
gas_score = (gas / gas_baseline) * (100 - (hum_weighting * 100))
else:
gas_score = 100 - (hum_weighting * 100)
# Calculate air_quality_score.
air_quality_score = hum_score + gas_score
print("Gas: {0:.2f} Ohms,humidity: {1:.2f} %RH,air quality: {2:.2f}".format(gas, hum, air_quality_score))
lines[0] = ["{:.2f}".format(air_quality_score)]
show_text(lines)
if air_quality_score >= 85:
blinkt.clear()
blinkt.set_all(0, 255, 0, 0.1)
blinkt.show()
elif air_quality_score > 70 and air_quality_score < 85:
blinkt.clear()
blinkt.set_all(255, 255, 0, 0.1)
blinkt.show()
else:
blinkt.clear()
blinkt.set_all(255, 0, 0, 0.1)
blinkt.show()
time.sleep(0.02)
except KeyboardInterrupt:
pass