import utime
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from dht import DHT22
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
button = Pin(10, Pin.IN, Pin.PULL_UP)
dht = DHT22(Pin(15))
current_screen = 1
oled.fill(0)
oled.text("Clean.Air.Act!", 5, 10)
oled.text("2023-2024", 5, 20)
oled.text(">> Push button!", 5, 55)
oled.show()
def show_screen_1():
oled.fill(0)
oled.text("Data sensor #1", 5, 20)
oled.show()
def show_screen_2():
oled.fill(0)
oled.text("Data sensor #2", 5, 20)
oled.show()
def show_screen_3(temp, hum, heat_index):
oled.fill(0)
oled.text("Temp: {:.1f} C".format(temp), 5, 10)
oled.text("Hum: {:.1f} %".format(hum), 5, 25)
oled.text("Heat Index:", 5, 40)
oled.text("{:.1f} C".format(heat_index), 5, 50)
oled.show()
while True:
if button.value() == 0:
utime.sleep(0.2)
current_screen += 1
if current_screen > 3:
current_screen = 1
if current_screen == 1:
show_screen_1()
elif current_screen == 2:
show_screen_2()
elif current_screen == 3:
try:
dht.measure()
temp = dht.temperature()
hum = dht.humidity()
heat_index = -8.78469475556 + (1.61139411 * temp) + (2.33854883889 * hum) \
+ (-0.14611605 * temp * hum) + (-0.012308094 * temp**2) \
+ (-0.0164248277778 * hum**2) + (0.002211732 * temp**2 * hum) \
+ (0.00072546 * temp * hum**2) + (-0.000003582 * temp**2 * hum**2)
show_screen_3(temp, hum, heat_index)
except Exception as e:
oled.fill(0)
oled.text("DHT22 Error", 5, 20)
oled.text(str(e), 5, 35)
oled.show()
utime.sleep(2)