Skip to content

Instantly share code, notes, and snippets.

@drkpxl
Created September 13, 2020 22:21
Show Gist options
  • Select an option

  • Save drkpxl/2aabe294c1ef333670f5bf71cfb5c097 to your computer and use it in GitHub Desktop.

Select an option

Save drkpxl/2aabe294c1ef333670f5bf71cfb5c097 to your computer and use it in GitHub Desktop.
import psutil
from gpiozero import CPUTemperature
import time
import datetime
from Adafruit_IO import Client
from bme280 import BME280
from enviroplus.noise import Noise
import colorsys
import sys
import ST7735
try:
# Transitional fix for breaking change in LTR559
from ltr559 import LTR559
ltr559 = LTR559()
except ImportError:
import ltr559
try:
from smbus2 import SMBus
except ImportError:
from smbus import SMBus
from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError, SerialTimeoutError
from enviroplus import gas
from subprocess import PIPE, Popen
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from fonts.ttf import RobotoMedium as UserFont
from datetime import timedelta
# Initial Setup of sensors / API
bus = SMBus(1)
bme280 = BME280(i2c_dev=bus)
aio = Client('xxx', 'aio_xxx')
pms5003 = PMS5003()
noise = Noise()
# Create LCD class instance.
disp = ST7735.ST7735(
port=0,
cs=1,
dc=9,
backlight=12,
rotation=270,
spi_speed_hz=10000000
)
# Create array for gas readings
gas_array = []
# Initialize display.
disp.begin()
# Width and height to calculate text position.
WIDTH = disp.width
HEIGHT = disp.height
# New canvas to draw on.
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
draw = ImageDraw.Draw(img)
# Text settings.
font_size = 20
small_font_size = 12
font = ImageFont.truetype(UserFont, font_size)
small_font = ImageFont.truetype(UserFont, small_font_size)
text_colour = (255, 255, 255)
back_colour = (0, 0, 0)
#size_x, size_y = draw.textsize(message, font)
warning = "Warning!"
# Calculate text position
#x = (WIDTH - size_x) / 2
#y = (HEIGHT / 2) - (size_y / 2)
x = 0
y = 0
def warm_func():
currentTime = datetime.datetime.now()
draw.rectangle((0, 0, 160, 80), (30, 160, 30))
draw.text((10, 20), "Warming Up", font=font, fill=text_colour)
draw.text((0, 66), currentTime.strftime("%a, %b %d %I:%M:%S %p"), font=small_font, fill=text_colour)
disp.display(img)
def cpu_report_func():
# Get CPU Info
cpu = CPUTemperature()
aio.send('cpu-temp', round(cpu.temperature * 1.8 + 32))
aio.send('cpu', psutil.cpu_percent())
def noise_func():
# Get Noise
global noise_amount, noise_display
noise_amount = noise.get_amplitude_at_frequency_range(20, 8000)
noise_display = str(int(round(noise_amount * 100))) + " db"
aio.send('noise', int(round(noise_amount * 100)))
def temp_func():
# Read Temp, convert to F and adjust
global tempf_display
tempf = round((bme280.get_temperature() * 1.8 + 32) * .92)
aio.send('temp', tempf)
tempf_display = "" + str(tempf) + " F"
def gas_func():
global gas_reading, gas_average, gas_warning_amount
# Get Gas
gas_reading = gas.read_all()
gas_array.append(gas_reading.reducing)
# If the array is larger than 8 items dump the first one
if (len(gas_array) > 8):
gas_array.pop(0)
#print("Popped!")
aio.send('gas', round(gas_reading.reducing))
gas_average = (sum(gas_array) / len(gas_array))
gas_warning_amount = str(round(gas_reading.reducing))
def pollution_func():
global pm25, pm10_display, pm25_display, pm1_display
# Read Particulate Matter
readings = pms5003.read()
pm25 = readings.pm_ug_per_m3(2.5)
pm10 = readings.pm_ug_per_m3(10)
pm1 = readings.pm_ug_per_m3(1)
# Send to Adafruit
aio.send('pollution.pm25', pm25)
aio.send('pollution.pm1', pm1)
aio.send('pollution.pm10', pm10)
# Draw on Screen
pm10_display = "PM10: " + str(pm10) + " ug/m3"
pm25_display = "PM25: " + str(pm25) + " ug/m3"
pm1_display = "PM10: " + str(pm1) + " ug/m3"
def systemup_func():
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
uptime_string = str(timedelta(seconds = uptime_seconds))
aio.send('system-uptime', uptime_string)
try:
# Warm Up
warm_func()
time.sleep(120)
while True:
currentTime = datetime.datetime.now()
# Run Sensor Functions
cpu_report_func()
noise_func()
gas_func()
temp_func()
pollution_func()
systemup_func()
# Display output of sensors on display
draw.rectangle((0, 0, 160, 80), back_colour)
disp.set_backlight(1)
if (pm25 > 50):
draw.rectangle((0, 0, 160, 80), (255, 0, 0))
draw.text((10, 20), warning, font=font, fill=text_colour)
draw.text((10, 40), pm25_display, font=font, fill=text_colour)
if (gas_reading.reducing > (gas_average * 1.05) and len(gas_array) == 8):
print("High Pollution Warning")
draw.rectangle((0, 0, 160, 80), (255, 0, 0))
draw.text((10, 20), warning, font=font, fill=text_colour)
draw.text((10, 40), gas_warning_amount, font=font, fill=text_colour)
draw.text((0, 0), pm10_display, font=font, fill=text_colour)
draw.text((0, 20), pm25_display, font=font, fill=text_colour)
draw.text((0, 40), pm1_display, font=font, fill=text_colour)
draw.text((0, 60), tempf_display, font=font, fill=text_colour)
draw.text((80, 60), noise_display, font=font, fill=text_colour)
disp.display(img)
time.sleep(60)
# Turn off backlight on control-c
except KeyboardInterrupt:
disp.set_backlight(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment