mirror of
https://github.com/pimoroni/grow-python
synced 2025-10-25 15:19:23 +00:00
Do some more basic housekeeping to get to a building working version 0.0.1
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from enviroplus import gas
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""adc.py - Print readings from the MICS6814 Gas sensor.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
gas.enable_adc()
|
||||
gas.set_adc_gain(4.096)
|
||||
|
||||
try:
|
||||
while True:
|
||||
readings = gas.read_all()
|
||||
logging.info(readings)
|
||||
time.sleep(1.0)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
@@ -1,190 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import colorsys
|
||||
import os
|
||||
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 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
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
|
||||
Press Ctrl+C to exit!
|
||||
""")
|
||||
|
||||
# BME280 temperature/pressure/humidity sensor
|
||||
bme280 = BME280()
|
||||
|
||||
# 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)
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
font_size = 20
|
||||
font = ImageFont.truetype(UserFont, font_size)
|
||||
|
||||
message = ""
|
||||
|
||||
# The position of the top bar
|
||||
top_pos = 25
|
||||
|
||||
|
||||
# Displays data and text on the 0.96" LCD
|
||||
def display_text(variable, data, unit):
|
||||
# Maintain length of list
|
||||
values[variable] = values[variable][1:] + [data]
|
||||
# Scale the values for the variable between 0 and 1
|
||||
vmin = min(values[variable])
|
||||
vmax = max(values[variable])
|
||||
colours = [(v - vmin + 1) / (vmax - vmin + 1) for v in values[variable]]
|
||||
# Format the variable name and value
|
||||
message = "{}: {:.1f} {}".format(variable[:4], data, unit)
|
||||
logging.info(message)
|
||||
draw.rectangle((0, 0, WIDTH, HEIGHT), (255, 255, 255))
|
||||
for i in range(len(colours)):
|
||||
# Convert the values to colours from red to blue
|
||||
colour = (1.0 - colours[i]) * 0.6
|
||||
r, g, b = [int(x * 255.0) for x in colorsys.hsv_to_rgb(colour, 1.0, 1.0)]
|
||||
# Draw a 1-pixel wide rectangle of colour
|
||||
draw.rectangle((i, top_pos, i + 1, HEIGHT), (r, g, b))
|
||||
# Draw a line graph in black
|
||||
line_y = HEIGHT - (top_pos + (colours[i] * (HEIGHT - top_pos))) + top_pos
|
||||
draw.rectangle((i, line_y, i + 1, line_y + 1), (0, 0, 0))
|
||||
# Write the text at the top in black
|
||||
draw.text((0, 0), message, font=font, fill=(0, 0, 0))
|
||||
st7735.display(img)
|
||||
|
||||
|
||||
# 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 = 2.25
|
||||
|
||||
cpu_temps = [get_cpu_temperature()] * 5
|
||||
|
||||
delay = 0.5 # Debounce the proximity tap
|
||||
mode = 0 # The starting mode
|
||||
last_page = 0
|
||||
light = 1
|
||||
|
||||
# Create a values dict to store the data
|
||||
variables = ["temperature",
|
||||
"pressure",
|
||||
"humidity",
|
||||
"light",
|
||||
"oxidised",
|
||||
"reduced",
|
||||
"nh3"]
|
||||
|
||||
values = {}
|
||||
|
||||
for v in variables:
|
||||
values[v] = [1] * WIDTH
|
||||
|
||||
# The main loop
|
||||
try:
|
||||
while True:
|
||||
proximity = ltr559.get_proximity()
|
||||
|
||||
# If the proximity crosses the threshold, toggle the mode
|
||||
if proximity > 1500 and time.time() - last_page > delay:
|
||||
mode += 1
|
||||
mode %= len(variables)
|
||||
last_page = time.time()
|
||||
|
||||
# One mode for each variable
|
||||
if mode == 0:
|
||||
# variable = "temperature"
|
||||
unit = "C"
|
||||
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()
|
||||
data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 1:
|
||||
# variable = "pressure"
|
||||
unit = "hPa"
|
||||
data = bme280.get_pressure()
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 2:
|
||||
# variable = "humidity"
|
||||
unit = "%"
|
||||
data = bme280.get_humidity()
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 3:
|
||||
# variable = "light"
|
||||
unit = "Lux"
|
||||
if proximity < 10:
|
||||
data = ltr559.get_lux()
|
||||
else:
|
||||
data = 1
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 4:
|
||||
# variable = "oxidised"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.oxidising / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 5:
|
||||
# variable = "reduced"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.reducing / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 6:
|
||||
# variable = "nh3"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.nh3 / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
# Exit cleanly
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
@@ -4,16 +4,9 @@ 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 grow import moisture
|
||||
from grow import pump
|
||||
from subprocess import PIPE, Popen
|
||||
from PIL import Image
|
||||
from PIL import ImageDraw
|
||||
@@ -26,18 +19,12 @@ logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
|
||||
logging.info("""all-in-one.py - Displays readings from all of Grow HAT Mini's moisture sensors
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
# BME280 temperature/pressure/humidity sensor
|
||||
bme280 = BME280()
|
||||
|
||||
# PMS5003 particulate sensor
|
||||
pms5003 = PMS5003()
|
||||
|
||||
# Create ST7735 LCD display class
|
||||
st7735 = ST7735.ST7735(
|
||||
port=0,
|
||||
@@ -111,16 +98,12 @@ last_page = 0
|
||||
light = 1
|
||||
|
||||
# Create a values dict to store the data
|
||||
variables = ["temperature",
|
||||
"pressure",
|
||||
"humidity",
|
||||
"light",
|
||||
"oxidised",
|
||||
"reduced",
|
||||
"nh3",
|
||||
"pm1",
|
||||
"pm25",
|
||||
"pm10"]
|
||||
variables = ["moisture1",
|
||||
"moisture2",
|
||||
"moisture3",
|
||||
"pump1",
|
||||
"pump2",
|
||||
"pump3"]
|
||||
|
||||
values = {}
|
||||
|
||||
@@ -130,101 +113,12 @@ for v in variables:
|
||||
# The main loop
|
||||
try:
|
||||
while True:
|
||||
proximity = ltr559.get_proximity()
|
||||
|
||||
# If the proximity crosses the threshold, toggle the mode
|
||||
if proximity > 1500 and time.time() - last_page > delay:
|
||||
mode += 1
|
||||
mode %= len(variables)
|
||||
last_page = time.time()
|
||||
|
||||
# One mode for each variable
|
||||
if mode == 0:
|
||||
# variable = "temperature"
|
||||
unit = "C"
|
||||
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()
|
||||
data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 1:
|
||||
# variable = "pressure"
|
||||
unit = "hPa"
|
||||
data = bme280.get_pressure()
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 2:
|
||||
# variable = "humidity"
|
||||
unit = "%"
|
||||
data = bme280.get_humidity()
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 3:
|
||||
# variable = "light"
|
||||
unit = "Lux"
|
||||
if proximity < 10:
|
||||
data = ltr559.get_lux()
|
||||
else:
|
||||
data = 1
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 4:
|
||||
# variable = "oxidised"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.oxidising / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 5:
|
||||
# variable = "reduced"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.reducing / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 6:
|
||||
# variable = "nh3"
|
||||
unit = "kO"
|
||||
data = gas.read_all()
|
||||
data = data.nh3 / 1000
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 7:
|
||||
# variable = "pm1"
|
||||
unit = "ug/m3"
|
||||
try:
|
||||
data = pms5003.read()
|
||||
except pmsReadTimeoutError:
|
||||
logging.warn("Failed to read PMS5003")
|
||||
else:
|
||||
data = float(data.pm_ug_per_m3(1.0))
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 8:
|
||||
# variable = "pm25"
|
||||
unit = "ug/m3"
|
||||
try:
|
||||
data = pms5003.read()
|
||||
except pmsReadTimeoutError:
|
||||
logging.warn("Failed to read PMS5003")
|
||||
else:
|
||||
data = float(data.pm_ug_per_m3(2.5))
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
if mode == 9:
|
||||
# variable = "pm10"
|
||||
unit = "ug/m3"
|
||||
try:
|
||||
data = pms5003.read()
|
||||
except pmsReadTimeoutError:
|
||||
logging.warn("Failed to read PMS5003")
|
||||
else:
|
||||
data = float(data.pm_ug_per_m3(10))
|
||||
display_text(variables[mode], data, unit)
|
||||
data = moisture.read_all()
|
||||
display_text(variables[mode], data, unit)
|
||||
|
||||
time.sleep(delay)
|
||||
|
||||
# Exit cleanly
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
|
||||
@@ -4,16 +4,10 @@ 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 grow import moisture
|
||||
from grow import pump
|
||||
from subprocess import PIPE, Popen
|
||||
from PIL import Image
|
||||
from PIL import ImageDraw
|
||||
@@ -32,13 +26,6 @@ Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
# BME280 temperature/pressure/humidity sensor
|
||||
bme280 = BME280()
|
||||
|
||||
# PMS5003 particulate sensor
|
||||
pms5003 = PMS5003()
|
||||
time.sleep(1.0)
|
||||
|
||||
# Create ST7735 LCD display class
|
||||
st7735 = ST7735.ST7735(
|
||||
port=0,
|
||||
@@ -71,27 +58,19 @@ message = ""
|
||||
top_pos = 25
|
||||
|
||||
# Create a values dict to store the data
|
||||
variables = ["temperature",
|
||||
"pressure",
|
||||
"humidity",
|
||||
"light",
|
||||
"oxidised",
|
||||
"reduced",
|
||||
"nh3",
|
||||
"pm1",
|
||||
"pm25",
|
||||
"pm10"]
|
||||
variables = ["moisture1",
|
||||
"moisture2",
|
||||
"moisture3",
|
||||
"pump1",
|
||||
"pump2",
|
||||
"pump3"]
|
||||
|
||||
units = ["C",
|
||||
"hPa",
|
||||
"%",
|
||||
"Lux",
|
||||
"kO",
|
||||
"kO",
|
||||
"kO",
|
||||
"ug/m3",
|
||||
"ug/m3",
|
||||
"ug/m3"]
|
||||
units = ["Hz",
|
||||
"Hz",
|
||||
"Hz",
|
||||
"",
|
||||
"",
|
||||
""]
|
||||
|
||||
# Define your own warning limits
|
||||
# The limits definition follows the order of the variables array
|
||||
@@ -111,18 +90,13 @@ limits = [[4, 18, 28, 35],
|
||||
[20, 30, 60, 70],
|
||||
[-1, -1, 30000, 100000],
|
||||
[-1, -1, 40, 50],
|
||||
[-1, -1, 450, 550],
|
||||
[-1, -1, 200, 300],
|
||||
[-1, -1, 50, 100],
|
||||
[-1, -1, 50, 100],
|
||||
[-1, -1, 50, 100]]
|
||||
[-1, -1, 450, 550]]
|
||||
|
||||
# RGB palette for values on the combined screen
|
||||
palette = [(0, 0, 255), # Dangerously Low
|
||||
(0, 255, 255), # Low
|
||||
(0, 255, 0), # Normal
|
||||
(255, 255, 0), # High
|
||||
(255, 0, 0)] # Dangerously High
|
||||
palette = [(0, 0, 255), # Dry
|
||||
(0, 255, 255), # Damp
|
||||
(0, 255, 0), # Moist
|
||||
(255, 255, 0)] # Wet
|
||||
|
||||
values = {}
|
||||
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from bme280 import BME280
|
||||
|
||||
try:
|
||||
from smbus2 import SMBus
|
||||
except ImportError:
|
||||
from smbus import SMBus
|
||||
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""compensated-temperature.py - Use the CPU temperature
|
||||
to compensate temperature readings from the BME280 sensor.
|
||||
Method adapted from Initial State's Enviro pHAT review:
|
||||
https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
bus = SMBus(1)
|
||||
bme280 = BME280(i2c_dev=bus)
|
||||
|
||||
|
||||
# Get the temperature of the CPU for compensation
|
||||
def get_cpu_temperature():
|
||||
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
|
||||
temp = f.read()
|
||||
temp = int(temp) / 1000.0
|
||||
return temp
|
||||
|
||||
|
||||
# Tuning factor for compensation. Decrease this number to adjust the
|
||||
# temperature down, and increase to adjust up
|
||||
factor = 2.25
|
||||
|
||||
cpu_temps = [get_cpu_temperature()] * 5
|
||||
|
||||
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()
|
||||
comp_temp = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
|
||||
logging.info("Compensated temperature: {:05.2f} *C".format(comp_temp))
|
||||
time.sleep(1.0)
|
||||
@@ -1,24 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from enviroplus import gas
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""gas.py - Print readings from the MICS6814 Gas sensor.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
try:
|
||||
while True:
|
||||
readings = gas.read_all()
|
||||
logging.info(readings)
|
||||
time.sleep(1.0)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
@@ -1,33 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
import logging
|
||||
try:
|
||||
# Transitional fix for breaking change in LTR559
|
||||
from ltr559 import LTR559
|
||||
ltr559 = LTR559()
|
||||
except ImportError:
|
||||
import ltr559
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""light.py - Print readings from the LTR559 Light & Proximity sensor.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
try:
|
||||
while True:
|
||||
lux = ltr559.get_lux()
|
||||
prox = ltr559.get_proximity()
|
||||
logging.info("""Light: {:05.02f} Lux
|
||||
Proximity: {:05.02f}
|
||||
""".format(lux, prox))
|
||||
time.sleep(1.0)
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
@@ -1,188 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import requests
|
||||
import ST7735
|
||||
import time
|
||||
from bme280 import BME280
|
||||
from pms5003 import PMS5003, ReadTimeoutError
|
||||
from subprocess import PIPE, Popen, check_output
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
from fonts.ttf import RobotoMedium as UserFont
|
||||
|
||||
try:
|
||||
from smbus2 import SMBus
|
||||
except ImportError:
|
||||
from smbus import SMBus
|
||||
|
||||
print("""luftdaten.py - Reads temperature, pressure, humidity,
|
||||
PM2.5, and PM10 from Enviro plus and sends data to Luftdaten,
|
||||
the citizen science air quality project.
|
||||
|
||||
Note: you'll need to register with Luftdaten at:
|
||||
https://meine.luftdaten.info/ and enter your Raspberry Pi
|
||||
serial number that's displayed on the Enviro plus LCD along
|
||||
with the other details before the data appears on the
|
||||
Luftdaten map.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
bus = SMBus(1)
|
||||
|
||||
# Create BME280 instance
|
||||
bme280 = BME280(i2c_dev=bus)
|
||||
|
||||
# Create LCD instance
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=1,
|
||||
dc=9,
|
||||
backlight=12,
|
||||
rotation=270,
|
||||
spi_speed_hz=10000000
|
||||
)
|
||||
|
||||
# Initialize display
|
||||
disp.begin()
|
||||
|
||||
# Create PMS5003 instance
|
||||
pms5003 = PMS5003()
|
||||
|
||||
|
||||
# Read values from BME280 and PMS5003 and return as dict
|
||||
def read_values():
|
||||
values = {}
|
||||
cpu_temp = get_cpu_temperature()
|
||||
raw_temp = bme280.get_temperature()
|
||||
comp_temp = raw_temp - ((cpu_temp - raw_temp) / comp_factor)
|
||||
values["temperature"] = "{:.2f}".format(comp_temp)
|
||||
values["pressure"] = "{:.2f}".format(bme280.get_pressure() * 100)
|
||||
values["humidity"] = "{:.2f}".format(bme280.get_humidity())
|
||||
try:
|
||||
pm_values = pms5003.read()
|
||||
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
|
||||
values["P1"] = str(pm_values.pm_ug_per_m3(10))
|
||||
except ReadTimeoutError:
|
||||
pms5003.reset()
|
||||
pm_values = pms5003.read()
|
||||
values["P2"] = str(pm_values.pm_ug_per_m3(2.5))
|
||||
values["P1"] = str(pm_values.pm_ug_per_m3(10))
|
||||
return values
|
||||
|
||||
|
||||
# Get CPU temperature to use 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("'")])
|
||||
|
||||
|
||||
# Get Raspberry Pi serial number to use as ID
|
||||
def get_serial_number():
|
||||
with open('/proc/cpuinfo', 'r') as f:
|
||||
for line in f:
|
||||
if line[0:6] == 'Serial':
|
||||
return line.split(":")[1].strip()
|
||||
|
||||
|
||||
# Check for Wi-Fi connection
|
||||
def check_wifi():
|
||||
if check_output(['hostname', '-I']):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# Display Raspberry Pi serial and Wi-Fi status on LCD
|
||||
def display_status():
|
||||
wifi_status = "connected" if check_wifi() else "disconnected"
|
||||
text_colour = (255, 255, 255)
|
||||
back_colour = (0, 170, 170) if check_wifi() else (85, 15, 15)
|
||||
id = get_serial_number()
|
||||
message = "{}\nWi-Fi: {}".format(id, wifi_status)
|
||||
img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
|
||||
draw = ImageDraw.Draw(img)
|
||||
size_x, size_y = draw.textsize(message, font)
|
||||
x = (WIDTH - size_x) / 2
|
||||
y = (HEIGHT / 2) - (size_y / 2)
|
||||
draw.rectangle((0, 0, 160, 80), back_colour)
|
||||
draw.text((x, y), message, font=font, fill=text_colour)
|
||||
disp.display(img)
|
||||
|
||||
|
||||
def send_to_luftdaten(values, id):
|
||||
pm_values = dict(i for i in values.items() if i[0].startswith("P"))
|
||||
temp_values = dict(i for i in values.items() if not i[0].startswith("P"))
|
||||
|
||||
pm_values_json = [{"value_type": key, "value": val} for key, val in pm_values.items()]
|
||||
temp_values_json = [{"value_type": key, "value": val} for key, val in temp_values.items()]
|
||||
|
||||
resp_1 = requests.post(
|
||||
"https://api.luftdaten.info/v1/push-sensor-data/",
|
||||
json={
|
||||
"software_version": "enviro-plus 0.0.1",
|
||||
"sensordatavalues": pm_values_json
|
||||
},
|
||||
headers={
|
||||
"X-PIN": "1",
|
||||
"X-Sensor": id,
|
||||
"Content-Type": "application/json",
|
||||
"cache-control": "no-cache"
|
||||
}
|
||||
)
|
||||
|
||||
resp_2 = requests.post(
|
||||
"https://api.luftdaten.info/v1/push-sensor-data/",
|
||||
json={
|
||||
"software_version": "enviro-plus 0.0.1",
|
||||
"sensordatavalues": temp_values_json
|
||||
},
|
||||
headers={
|
||||
"X-PIN": "11",
|
||||
"X-Sensor": id,
|
||||
"Content-Type": "application/json",
|
||||
"cache-control": "no-cache"
|
||||
}
|
||||
)
|
||||
|
||||
if resp_1.ok and resp_2.ok:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
# Compensation factor for temperature
|
||||
comp_factor = 2.25
|
||||
|
||||
# Raspberry Pi ID to send to Luftdaten
|
||||
id = "raspi-" + get_serial_number()
|
||||
|
||||
# Width and height to calculate text position
|
||||
WIDTH = disp.width
|
||||
HEIGHT = disp.height
|
||||
|
||||
# Text settings
|
||||
font_size = 16
|
||||
font = ImageFont.truetype(UserFont, font_size)
|
||||
|
||||
# Display Raspberry Pi serial and Wi-Fi status
|
||||
print("Raspberry Pi serial: {}".format(get_serial_number()))
|
||||
print("Wi-Fi: {}\n".format("connected" if check_wifi() else "disconnected"))
|
||||
|
||||
time_since_update = 0
|
||||
update_time = time.time()
|
||||
|
||||
# Main loop to read data, display, and send to Luftdaten
|
||||
while True:
|
||||
try:
|
||||
time_since_update = time.time() - update_time
|
||||
values = read_values()
|
||||
print(values)
|
||||
if time_since_update > 145:
|
||||
resp = send_to_luftdaten(values, id)
|
||||
update_time = time.time()
|
||||
print("Response: {}\n".format("ok" if resp else "failed"))
|
||||
display_status()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
@@ -1,44 +0,0 @@
|
||||
import ST7735
|
||||
from PIL import Image, ImageDraw
|
||||
from enviroplus.noise import Noise
|
||||
|
||||
print("""noise-amps-at-freqs.py - Measure amplitude from specific frequency bins
|
||||
|
||||
This example retrieves the median amplitude from 3 user-specified frequency ranges and plots them in Blue, Green and Red on the Enviro+ display.
|
||||
|
||||
As you play a continuous rising tone on your phone, you should notice peaks that correspond to the frequency entering each range.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
noise = Noise()
|
||||
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=ST7735.BG_SPI_CS_FRONT,
|
||||
dc=9,
|
||||
backlight=12,
|
||||
rotation=90)
|
||||
|
||||
disp.begin()
|
||||
|
||||
img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0))
|
||||
draw = ImageDraw.Draw(img)
|
||||
|
||||
|
||||
while True:
|
||||
amps = noise.get_amplitudes_at_frequency_ranges([
|
||||
(100, 200),
|
||||
(500, 600),
|
||||
(1000, 1200)
|
||||
])
|
||||
amps = [n * 32 for n in amps]
|
||||
img2 = img.copy()
|
||||
draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0))
|
||||
img.paste(img2, (1, 0))
|
||||
draw.line((0, 0, 0, amps[0]), fill=(0, 0, 255))
|
||||
draw.line((0, 0, 0, amps[1]), fill=(0, 255, 0))
|
||||
draw.line((0, 0, 0, amps[2]), fill=(255, 0, 0))
|
||||
|
||||
disp.display(img)
|
||||
@@ -1,40 +0,0 @@
|
||||
import ST7735
|
||||
from PIL import Image, ImageDraw
|
||||
from enviroplus.noise import Noise
|
||||
|
||||
print("""noise-profile.py - Get a simple noise profile.
|
||||
|
||||
This example grabs a basic 3-bin noise profile of low, medium and high frequency noise, plotting the noise characteristics as coloured bars.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
noise = Noise()
|
||||
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=ST7735.BG_SPI_CS_FRONT,
|
||||
dc=9,
|
||||
backlight=12,
|
||||
rotation=90)
|
||||
|
||||
disp.begin()
|
||||
|
||||
img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0))
|
||||
draw = ImageDraw.Draw(img)
|
||||
|
||||
|
||||
while True:
|
||||
low, mid, high, amp = noise.get_noise_profile()
|
||||
low *= 128
|
||||
mid *= 128
|
||||
high *= 128
|
||||
amp *= 64
|
||||
|
||||
img2 = img.copy()
|
||||
draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0))
|
||||
img.paste(img2, (1, 0))
|
||||
draw.line((0, 0, 0, amp), fill=(int(low), int(mid), int(high)))
|
||||
|
||||
disp.display(img)
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from pms5003 import PMS5003, ReadTimeoutError
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""particulates.py - Print readings from the PMS5003 particulate sensor.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
pms5003 = PMS5003()
|
||||
time.sleep(1.0)
|
||||
|
||||
try:
|
||||
while True:
|
||||
try:
|
||||
readings = pms5003.read()
|
||||
logging.info(readings)
|
||||
except ReadTimeoutError:
|
||||
pms5003 = PMS5003()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
@@ -1,425 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import time
|
||||
import numpy
|
||||
import colorsys
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
||||
from fonts.ttf import RobotoMedium as UserFont
|
||||
|
||||
import ST7735
|
||||
from bme280 import BME280
|
||||
from ltr559 import LTR559
|
||||
|
||||
import pytz
|
||||
from pytz import timezone
|
||||
from astral.geocoder import database, lookup
|
||||
from astral.sun import sun
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
try:
|
||||
from smbus2 import SMBus
|
||||
except ImportError:
|
||||
from smbus import SMBus
|
||||
|
||||
|
||||
def calculate_y_pos(x, centre):
|
||||
"""Calculates the y-coordinate on a parabolic curve, given x."""
|
||||
centre = 80
|
||||
y = 1 / centre * (x - centre) ** 2
|
||||
|
||||
return int(y)
|
||||
|
||||
|
||||
def circle_coordinates(x, y, radius):
|
||||
"""Calculates the bounds of a circle, given centre and radius."""
|
||||
|
||||
x1 = x - radius # Left
|
||||
x2 = x + radius # Right
|
||||
y1 = y - radius # Bottom
|
||||
y2 = y + radius # Top
|
||||
|
||||
return (x1, y1, x2, y2)
|
||||
|
||||
|
||||
def map_colour(x, centre, start_hue, end_hue, day):
|
||||
"""Given an x coordinate and a centre point, a start and end hue (in degrees),
|
||||
and a Boolean for day or night (day is True, night False), calculate a colour
|
||||
hue representing the 'colour' of that time of day."""
|
||||
|
||||
start_hue = start_hue / 360 # Rescale to between 0 and 1
|
||||
end_hue = end_hue / 360
|
||||
|
||||
sat = 1.0
|
||||
|
||||
# Dim the brightness as you move from the centre to the edges
|
||||
val = 1 - (abs(centre - x) / (2 * centre))
|
||||
|
||||
# Ramp up towards centre, then back down
|
||||
if x > centre:
|
||||
x = (2 * centre) - x
|
||||
|
||||
# Calculate the hue
|
||||
hue = start_hue + ((x / centre) * (end_hue - start_hue))
|
||||
|
||||
# At night, move towards purple/blue hues and reverse dimming
|
||||
if not day:
|
||||
hue = 1 - hue
|
||||
val = 1 - val
|
||||
|
||||
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, sat, val)]
|
||||
|
||||
return (r, g, b)
|
||||
|
||||
|
||||
def x_from_sun_moon_time(progress, period, x_range):
|
||||
"""Recalculate/rescale an amount of progress through a time period."""
|
||||
|
||||
x = int((progress / period) * x_range)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
def sun_moon_time(city_name, time_zone):
|
||||
"""Calculate the progress through the current sun/moon period (i.e day or
|
||||
night) from the last sunrise or sunset, given a datetime object 't'."""
|
||||
|
||||
city = lookup(city_name, database())
|
||||
|
||||
# Datetime objects for yesterday, today, tomorrow
|
||||
utc = pytz.utc
|
||||
utc_dt = datetime.now(tz=utc)
|
||||
local_dt = utc_dt.astimezone(pytz.timezone(time_zone))
|
||||
today = local_dt.date()
|
||||
yesterday = today - timedelta(1)
|
||||
tomorrow = today + timedelta(1)
|
||||
|
||||
# Sun objects for yesterday, today, tomorrow
|
||||
sun_yesterday = sun(city.observer, date=yesterday)
|
||||
sun_today = sun(city.observer, date=today)
|
||||
sun_tomorrow = sun(city.observer, date=tomorrow)
|
||||
|
||||
# Work out sunset yesterday, sunrise/sunset today, and sunrise tomorrow
|
||||
sunset_yesterday = sun_yesterday["sunset"]
|
||||
sunrise_today = sun_today["sunrise"]
|
||||
sunset_today = sun_today["sunset"]
|
||||
sunrise_tomorrow = sun_tomorrow["sunrise"]
|
||||
|
||||
# Work out lengths of day or night period and progress through period
|
||||
if sunrise_today < local_dt < sunset_today:
|
||||
day = True
|
||||
period = sunset_today - sunrise_today
|
||||
# mid = sunrise_today + (period / 2)
|
||||
progress = local_dt - sunrise_today
|
||||
|
||||
elif local_dt > sunset_today:
|
||||
day = False
|
||||
period = sunrise_tomorrow - sunset_today
|
||||
# mid = sunset_today + (period / 2)
|
||||
progress = local_dt - sunset_today
|
||||
|
||||
else:
|
||||
day = False
|
||||
period = sunrise_today - sunset_yesterday
|
||||
# mid = sunset_yesterday + (period / 2)
|
||||
progress = local_dt - sunset_yesterday
|
||||
|
||||
# Convert time deltas to seconds
|
||||
progress = progress.total_seconds()
|
||||
period = period.total_seconds()
|
||||
|
||||
return (progress, period, day, local_dt)
|
||||
|
||||
|
||||
def draw_background(progress, period, day):
|
||||
"""Given an amount of progress through the day or night, draw the
|
||||
background colour and overlay a blurred sun/moon."""
|
||||
|
||||
# x-coordinate for sun/moon
|
||||
x = x_from_sun_moon_time(progress, period, WIDTH)
|
||||
|
||||
# If it's day, then move right to left
|
||||
if day:
|
||||
x = WIDTH - x
|
||||
|
||||
# Calculate position on sun/moon's curve
|
||||
centre = WIDTH / 2
|
||||
y = calculate_y_pos(x, centre)
|
||||
|
||||
# Background colour
|
||||
background = map_colour(x, 80, mid_hue, day_hue, day)
|
||||
|
||||
# New image for background colour
|
||||
img = Image.new('RGBA', (WIDTH, HEIGHT), color=background)
|
||||
# draw = ImageDraw.Draw(img)
|
||||
|
||||
# New image for sun/moon overlay
|
||||
overlay = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
|
||||
overlay_draw = ImageDraw.Draw(overlay)
|
||||
|
||||
# Draw the sun/moon
|
||||
circle = circle_coordinates(x, y, sun_radius)
|
||||
overlay_draw.ellipse(circle, fill=(200, 200, 50, opacity))
|
||||
|
||||
# Overlay the sun/moon on the background as an alpha matte
|
||||
composite = Image.alpha_composite(img, overlay).filter(ImageFilter.GaussianBlur(radius=blur))
|
||||
|
||||
return composite
|
||||
|
||||
|
||||
def overlay_text(img, position, text, font, align_right=False, rectangle=False):
|
||||
draw = ImageDraw.Draw(img)
|
||||
w, h = font.getsize(text)
|
||||
if align_right:
|
||||
x, y = position
|
||||
x -= w
|
||||
position = (x, y)
|
||||
if rectangle:
|
||||
x += 1
|
||||
y += 1
|
||||
position = (x, y)
|
||||
border = 1
|
||||
rect = (x - border, y, x + w, y + h + border)
|
||||
rect_img = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
|
||||
rect_draw = ImageDraw.Draw(rect_img)
|
||||
rect_draw.rectangle(rect, (255, 255, 255))
|
||||
rect_draw.text(position, text, font=font, fill=(0, 0, 0, 0))
|
||||
img = Image.alpha_composite(img, rect_img)
|
||||
else:
|
||||
draw.text(position, text, font=font, fill=(255, 255, 255))
|
||||
return img
|
||||
|
||||
|
||||
def get_cpu_temperature():
|
||||
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
|
||||
temp = f.read()
|
||||
temp = int(temp) / 1000.0
|
||||
return temp
|
||||
|
||||
|
||||
def correct_humidity(humidity, temperature, corr_temperature):
|
||||
dewpoint = temperature - ((100 - humidity) / 5)
|
||||
corr_humidity = 100 - (5 * (corr_temperature - dewpoint))
|
||||
return min(100, corr_humidity)
|
||||
|
||||
|
||||
def analyse_pressure(pressure, t):
|
||||
global time_vals, pressure_vals, trend
|
||||
if len(pressure_vals) > num_vals:
|
||||
pressure_vals = pressure_vals[1:] + [pressure]
|
||||
time_vals = time_vals[1:] + [t]
|
||||
|
||||
# Calculate line of best fit
|
||||
line = numpy.polyfit(time_vals, pressure_vals, 1, full=True)
|
||||
|
||||
# Calculate slope, variance, and confidence
|
||||
slope = line[0][0]
|
||||
intercept = line[0][1]
|
||||
variance = numpy.var(pressure_vals)
|
||||
residuals = numpy.var([(slope * x + intercept - y) for x, y in zip(time_vals, pressure_vals)])
|
||||
r_squared = 1 - residuals / variance
|
||||
|
||||
# Calculate change in pressure per hour
|
||||
change_per_hour = slope * 60 * 60
|
||||
# variance_per_hour = variance * 60 * 60
|
||||
|
||||
mean_pressure = numpy.mean(pressure_vals)
|
||||
|
||||
# Calculate trend
|
||||
if r_squared > 0.5:
|
||||
if change_per_hour > 0.5:
|
||||
trend = ">"
|
||||
elif change_per_hour < -0.5:
|
||||
trend = "<"
|
||||
elif -0.5 <= change_per_hour <= 0.5:
|
||||
trend = "-"
|
||||
|
||||
if trend != "-":
|
||||
if abs(change_per_hour) > 3:
|
||||
trend *= 2
|
||||
else:
|
||||
pressure_vals.append(pressure)
|
||||
time_vals.append(t)
|
||||
mean_pressure = numpy.mean(pressure_vals)
|
||||
change_per_hour = 0
|
||||
trend = "-"
|
||||
|
||||
# time.sleep(interval)
|
||||
return (mean_pressure, change_per_hour, trend)
|
||||
|
||||
|
||||
def describe_pressure(pressure):
|
||||
"""Convert pressure into barometer-type description."""
|
||||
if pressure < 970:
|
||||
description = "storm"
|
||||
elif 970 <= pressure < 990:
|
||||
description = "rain"
|
||||
elif 990 <= pressure < 1010:
|
||||
description = "change"
|
||||
elif 1010 <= pressure < 1030:
|
||||
description = "fair"
|
||||
elif pressure >= 1030:
|
||||
description = "dry"
|
||||
else:
|
||||
description = ""
|
||||
return description
|
||||
|
||||
|
||||
def describe_humidity(humidity):
|
||||
"""Convert relative humidity into good/bad description."""
|
||||
if 40 < humidity < 60:
|
||||
description = "good"
|
||||
else:
|
||||
description = "bad"
|
||||
return description
|
||||
|
||||
|
||||
def describe_light(light):
|
||||
"""Convert light level in lux to descriptive value."""
|
||||
if light < 50:
|
||||
description = "dark"
|
||||
elif 50 <= light < 100:
|
||||
description = "dim"
|
||||
elif 100 <= light < 500:
|
||||
description = "light"
|
||||
elif light >= 500:
|
||||
description = "bright"
|
||||
return description
|
||||
|
||||
|
||||
# Initialise the LCD
|
||||
disp = ST7735.ST7735(
|
||||
port=0,
|
||||
cs=1,
|
||||
dc=9,
|
||||
backlight=12,
|
||||
rotation=270,
|
||||
spi_speed_hz=10000000
|
||||
)
|
||||
|
||||
disp.begin()
|
||||
|
||||
WIDTH = disp.width
|
||||
HEIGHT = disp.height
|
||||
|
||||
# The city and timezone that you want to display.
|
||||
city_name = "Sheffield"
|
||||
time_zone = "Europe/London"
|
||||
|
||||
# Values that alter the look of the background
|
||||
blur = 50
|
||||
opacity = 125
|
||||
|
||||
mid_hue = 0
|
||||
day_hue = 25
|
||||
|
||||
sun_radius = 50
|
||||
|
||||
# Fonts
|
||||
font_sm = ImageFont.truetype(UserFont, 12)
|
||||
font_lg = ImageFont.truetype(UserFont, 14)
|
||||
|
||||
# Margins
|
||||
margin = 3
|
||||
|
||||
|
||||
# Set up BME280 weather sensor
|
||||
bus = SMBus(1)
|
||||
bme280 = BME280(i2c_dev=bus)
|
||||
|
||||
min_temp = None
|
||||
max_temp = None
|
||||
|
||||
factor = 2.25
|
||||
cpu_temps = [get_cpu_temperature()] * 5
|
||||
|
||||
# Set up light sensor
|
||||
ltr559 = LTR559()
|
||||
|
||||
# Pressure variables
|
||||
pressure_vals = []
|
||||
time_vals = []
|
||||
num_vals = 1000
|
||||
interval = 1
|
||||
trend = "-"
|
||||
|
||||
# Keep track of time elapsed
|
||||
start_time = time.time()
|
||||
|
||||
while True:
|
||||
path = os.path.dirname(os.path.realpath(__file__))
|
||||
progress, period, day, local_dt = sun_moon_time(city_name, time_zone)
|
||||
background = draw_background(progress, period, day)
|
||||
|
||||
# Time.
|
||||
time_elapsed = time.time() - start_time
|
||||
date_string = local_dt.strftime("%d %b %y").lstrip('0')
|
||||
time_string = local_dt.strftime("%H:%M")
|
||||
img = overlay_text(background, (0 + margin, 0 + margin), time_string, font_lg)
|
||||
img = overlay_text(img, (WIDTH - margin, 0 + margin), date_string, font_lg, align_right=True)
|
||||
|
||||
# Temperature
|
||||
temperature = bme280.get_temperature()
|
||||
|
||||
# Corrected temperature
|
||||
cpu_temp = get_cpu_temperature()
|
||||
cpu_temps = cpu_temps[1:] + [cpu_temp]
|
||||
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
|
||||
corr_temperature = temperature - ((avg_cpu_temp - temperature) / factor)
|
||||
|
||||
if time_elapsed > 30:
|
||||
if min_temp is not None and max_temp is not None:
|
||||
if corr_temperature < min_temp:
|
||||
min_temp = corr_temperature
|
||||
elif corr_temperature > max_temp:
|
||||
max_temp = corr_temperature
|
||||
else:
|
||||
min_temp = corr_temperature
|
||||
max_temp = corr_temperature
|
||||
|
||||
temp_string = f"{corr_temperature:.0f}°C"
|
||||
img = overlay_text(img, (68, 18), temp_string, font_lg, align_right=True)
|
||||
spacing = font_lg.getsize(temp_string)[1] + 1
|
||||
if min_temp is not None and max_temp is not None:
|
||||
range_string = f"{min_temp:.0f}-{max_temp:.0f}"
|
||||
else:
|
||||
range_string = "------"
|
||||
img = overlay_text(img, (68, 18 + spacing), range_string, font_sm, align_right=True, rectangle=True)
|
||||
temp_icon = Image.open(f"{path}/icons/temperature.png")
|
||||
img.paste(temp_icon, (margin, 18), mask=temp_icon)
|
||||
|
||||
# Humidity
|
||||
humidity = bme280.get_humidity()
|
||||
corr_humidity = correct_humidity(humidity, temperature, corr_temperature)
|
||||
humidity_string = f"{corr_humidity:.0f}%"
|
||||
img = overlay_text(img, (68, 48), humidity_string, font_lg, align_right=True)
|
||||
spacing = font_lg.getsize(humidity_string)[1] + 1
|
||||
humidity_desc = describe_humidity(corr_humidity).upper()
|
||||
img = overlay_text(img, (68, 48 + spacing), humidity_desc, font_sm, align_right=True, rectangle=True)
|
||||
humidity_icon = Image.open(f"{path}/icons/humidity-{humidity_desc.lower()}.png")
|
||||
img.paste(humidity_icon, (margin, 48), mask=humidity_icon)
|
||||
|
||||
# Light
|
||||
light = ltr559.get_lux()
|
||||
light_string = f"{int(light):,}"
|
||||
img = overlay_text(img, (WIDTH - margin, 18), light_string, font_lg, align_right=True)
|
||||
spacing = font_lg.getsize(light_string.replace(",", ""))[1] + 1
|
||||
light_desc = describe_light(light).upper()
|
||||
img = overlay_text(img, (WIDTH - margin - 1, 18 + spacing), light_desc, font_sm, align_right=True, rectangle=True)
|
||||
light_icon = Image.open(f"{path}/icons/bulb-{light_desc.lower()}.png")
|
||||
img.paste(humidity_icon, (80, 18), mask=light_icon)
|
||||
|
||||
# Pressure
|
||||
pressure = bme280.get_pressure()
|
||||
t = time.time()
|
||||
mean_pressure, change_per_hour, trend = analyse_pressure(pressure, t)
|
||||
pressure_string = f"{int(mean_pressure):,} {trend}"
|
||||
img = overlay_text(img, (WIDTH - margin, 48), pressure_string, font_lg, align_right=True)
|
||||
pressure_desc = describe_pressure(mean_pressure).upper()
|
||||
spacing = font_lg.getsize(pressure_string.replace(",", ""))[1] + 1
|
||||
img = overlay_text(img, (WIDTH - margin - 1, 48 + spacing), pressure_desc, font_sm, align_right=True, rectangle=True)
|
||||
pressure_icon = Image.open(f"{path}/icons/weather-{pressure_desc.lower()}.png")
|
||||
img.paste(pressure_icon, (80, 48), mask=pressure_icon)
|
||||
|
||||
# Display image
|
||||
disp.display(img)
|
||||
@@ -1,35 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import time
|
||||
from bme280 import BME280
|
||||
|
||||
try:
|
||||
from smbus2 import SMBus
|
||||
except ImportError:
|
||||
from smbus import SMBus
|
||||
|
||||
import logging
|
||||
|
||||
logging.basicConfig(
|
||||
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
|
||||
level=logging.INFO,
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
logging.info("""weather.py - Print readings from the BME280 weather sensor.
|
||||
|
||||
Press Ctrl+C to exit!
|
||||
|
||||
""")
|
||||
|
||||
bus = SMBus(1)
|
||||
bme280 = BME280(i2c_dev=bus)
|
||||
|
||||
while True:
|
||||
temperature = bme280.get_temperature()
|
||||
pressure = bme280.get_pressure()
|
||||
humidity = bme280.get_humidity()
|
||||
logging.info("""Temperature: {:05.2f} *C
|
||||
Pressure: {:05.2f} hPa
|
||||
Relative humidity: {:05.2f} %
|
||||
""".format(temperature, pressure, humidity))
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user