Skip to content

Instantly share code, notes, and snippets.

@alanedwardes
Last active May 25, 2021 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanedwardes/e2046beb85cf07132b6717f3aad1d235 to your computer and use it in GitHub Desktop.
Save alanedwardes/e2046beb85cf07132b6717f3aad1d235 to your computer and use it in GitHub Desktop.
Uses the Pimoroni enviroplus board to capture statistics every second, update the screen and send them to a Graphite installation
#!/usr/bin/env python3
import graphyte
import time
import colorsys
import sys
import ST7735
try:
# Transitional fix for breaking change in LTR559
from ltr559 import LTR559
ltr559 = LTR559()
except ImportError:
import ltr559
from bme280 import BME280
from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError
from enviroplus import gas
from enviroplus.noise import Noise
from subprocess import PIPE, Popen
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from fonts.ttf import RobotoMedium
import logging
noise = Noise()
graphyte.init('example.com', prefix='pi1')
logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',level=logging.INFO,datefmt='%Y-%m-%d %H:%M:%S')
# BME280 temperature/pressure/humidity sensor
bme280 = BME280()
# PMS5003 particulate sensor
pms5003 = PMS5003()
# Create ST7735 LCD display class
st7735 = ST7735.ST7735(port=0,cs=1,dc=9,backlight=12,rotation=270,spi_speed_hz=10000000)
# Initialize display
st7735.begin()
WIDTH = st7735.width
HEIGHT = st7735.height
# Set up canvas and font
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
fontLarge = ImageFont.truetype(RobotoMedium, 30)
fontSmall = ImageFont.truetype(RobotoMedium, 20)
# Get the temperature of the CPU for compensation
def get_cpu_temperature():
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE, universal_newlines=True)
output, _error = process.communicate()
return float(output[output.index('=') + 1:output.rindex("'")])
# Tuning factor for compensation. Decrease this number to adjust the
# temperature down, and increase to adjust up
factor = 1.0
cpu_temps = [get_cpu_temperature()] * 5
# The main loop
try:
while True:
cpu_temp = get_cpu_temperature()
# Smooth out with some averaging to decrease jitter
cpu_temps = cpu_temps[1:] + [cpu_temp]
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
raw_temp = bme280.get_temperature()
room_temperature = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
graphyte.send('bme280.temperature', room_temperature)
graphyte.send('bme280.sensor_temperature', bme280.get_temperature())
graphyte.send('bme280.cpu_temperature', get_cpu_temperature())
humidity = bme280.get_humidity()
pressure = bme280.get_pressure()
graphyte.send('bme280.pressure', pressure)
graphyte.send('bme280.humidity', humidity)
graphyte.send('ltr559.light', ltr559.get_lux())
graphyte.send('ltr559.proximity', ltr559.get_proximity())
particles = pms5003.read()
graphyte.send('pms5003.pm1', float(particles.pm_ug_per_m3(1.0)))
graphyte.send('pms5003.pm25', float(particles.pm_ug_per_m3(2.5)))
graphyte.send('pms5003.pm10', float(particles.pm_ug_per_m3(10)))
resistance = gas.read_all()
graphyte.send('gas.oxidising', resistance.oxidising)
graphyte.send('gas.reducing', resistance.reducing)
graphyte.send('gas.nh3', resistance.nh3)
noise_low, noise_mid, noise_high, noise_amp = noise.get_noise_profile()
graphyte.send('noise.low', noise_low)
graphyte.send('noise.mid', noise_mid)
graphyte.send('noise.high', noise_high)
graphyte.send('noise.amp', noise_amp)
draw.rectangle((0, 0, WIDTH, HEIGHT), (0, 0, 0))
draw.text((0, 0), "{:.1f}°".format(room_temperature), font=fontLarge, fill=(255, 255, 255))
draw.text((0, 30), "Humid. {:.1f}%".format(humidity), font=fontSmall, fill=(128, 128, 128))
draw.text((0, 50), "Press. {:.1f}hPa".format(pressure), font=fontSmall, fill=(128, 128, 128))
st7735.display(img)
time.sleep(2)
# Exit cleanly
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment