Button B turns the LEDS on for two mins (they fade) Always get a red LED 1 flash when a data point is logged.
133 lines
4.2 KiB
Python
133 lines
4.2 KiB
Python
# A weather station program display the date, time, temperature and humidity on the OLED screen with Air Quality and eCO2 on ZIP LEDs (Red/Green)
|
|
from PicoAirQuality import KitronikBME688, KitronikDataLogger, KitronikButton, KitronikOLED, KitronikRTC, KitronikZIPLEDs
|
|
import time
|
|
|
|
|
|
|
|
logFileName = "atmosphere_log.txt"
|
|
|
|
buttons = KitronikButton()
|
|
|
|
def ButtonA_IRQHandler(pin):
|
|
global buttonA
|
|
buttonA = 30
|
|
print("button A pressed", buttonA)
|
|
|
|
# LEDS ON FOR 2 MINS 10% brightness
|
|
def ButtonB_IRQHandler(pin):
|
|
global buttonB
|
|
buttonB = 2*60
|
|
print("button B pressed", buttonB)
|
|
|
|
buttons.buttonA.irq(trigger=machine.Pin.IRQ_RISING, handler=ButtonA_IRQHandler)
|
|
buttons.buttonB.irq(trigger=machine.Pin.IRQ_RISING, handler=ButtonB_IRQHandler)
|
|
|
|
bme688 = KitronikBME688()
|
|
oled = KitronikOLED()
|
|
rtc = KitronikRTC()
|
|
zipleds = KitronikZIPLEDs(3)
|
|
|
|
#rtc.setDate(07, 4, 2022)
|
|
#rtc.setTime(21, 58, 0)
|
|
|
|
bme688.setupGasSensor()
|
|
bme688.calcBaselines()
|
|
sec = 0
|
|
#f = open(logFileName, 'a')
|
|
#f.write("# Data Logging: Date, Time, Temperature, Pressure, Humidity, AirQuality, CO2\n")
|
|
|
|
while True:
|
|
#global buttonA, buttonB
|
|
if sec == 0:
|
|
f = open(logFileName, 'a+')
|
|
f.write("# Data Logging: Date, Time, Temperature, Pressure, Humidity, AirQuality, CO2\n")
|
|
f.write('# Restarted ' + str(rtc.readDateString()) + ' ' + str(rtc.readTimeString()) + '\n' )
|
|
time.sleep_ms(1000)
|
|
bme688.measureData()
|
|
oled.clear()
|
|
|
|
try:
|
|
_ = buttonA
|
|
except NameError:
|
|
buttonA = 30
|
|
|
|
if buttonA > 0:
|
|
oled.poweron()
|
|
#oled.drawRect(4, 5, 120, 35)
|
|
oled.displayText(rtc.readDateString(), 1, 25)
|
|
oled.displayText(rtc.readTimeString(), 2, 33)
|
|
#oled.drawLine(0, 48, 127, 48)
|
|
#oled.drawLine(0, 49, 127, 49)
|
|
oled.displayText(str(bme688.readTemperature()) + " oC", 3, 0)
|
|
oled.displayText(str(bme688.readHumidity()) + " %H", 3, 80)
|
|
eCO2 = bme688.readeCO2()
|
|
oled.displayText(str(eCO2) + " CO2 ppm", 4, 0)
|
|
IAQ = bme688.getAirQualityScore()
|
|
oled.displayText(str(IAQ) + " AQ ", 5, 0)
|
|
mbar = bme688.readPressure()/100.0
|
|
oled.displayText(str(mbar) + " mB ", 5, 50)
|
|
oled.show()
|
|
buttonA = buttonA - 1
|
|
else:
|
|
oled.poweroff() # would really like to turn it off
|
|
#print("oled.clear");
|
|
|
|
if (IAQ < 100):
|
|
zipleds.setLED(0, zipleds.GREEN)
|
|
else:
|
|
zipleds.setLED(0, zipleds.RED)
|
|
if (eCO2 < 800):
|
|
zipleds.setLED(2, zipleds.GREEN)
|
|
else:
|
|
zipleds.setLED(2, zipleds.RED)
|
|
|
|
|
|
try:
|
|
_ = buttonB
|
|
except NameError:
|
|
buttonB = 30
|
|
|
|
if buttonB > 0:
|
|
brightness = buttonB/4
|
|
if brightness > 100:
|
|
brighness = 100
|
|
zipleds.setBrightness(brightness)
|
|
buttonB = buttonB - 1
|
|
else:
|
|
zipleds.setBrightness(0)
|
|
|
|
|
|
# ffs all I want is an action every minute I hate python scope shit
|
|
#now = rtc.readTimeString();
|
|
# log every minute, thats 1440 records a day
|
|
sec = sec + 1
|
|
secmod = sec % 59
|
|
if (secmod) == 0:
|
|
#log.setupDataFields("Date", "Time", "Temperature", "Pressure", "Humidity", "IAQ", "eCO2")
|
|
log_string = (rtc.readDateString() + ',' + \
|
|
rtc.readTimeString() + ',' + \
|
|
str(bme688.readTemperature()) + ',' + \
|
|
str(bme688.readPressure()) + ',' + \
|
|
str(bme688.readHumidity())+ ',' + \
|
|
str(bme688.getAirQualityScore()) + ',' + \
|
|
str(bme688.readeCO2())) + '\n'
|
|
#print (log_string)
|
|
#print ("logging data\n")
|
|
f.write(log_string)
|
|
f.flush()
|
|
zipleds.setLED(1, zipleds.RED)
|
|
zipleds.setLED(0, zipleds.BLACK)
|
|
zipleds.setLED(2, zipleds.BLACK)
|
|
zipleds.setBrightness(20) # always show a logging flash
|
|
#print("flash");
|
|
elif (secmod < 10):
|
|
zipleds.setLED(1, zipleds.BLUE)
|
|
elif (secmod > 10):
|
|
s2 = secmod * 2
|
|
zip_col = (secmod, s2, s2)
|
|
zipleds.setLED(1, zip_col)
|
|
|
|
if ( (sec+1) % 2 ) == 0:
|
|
zipleds.setLED(1, zipleds.BLACK)
|
|
|
|
zipleds.show() |