from framebuf import FrameBuffer, MONO_VLSB
from machine import Pin, I2C
from dht import DHT22
from ssd1306 import SSD1306_I2C
from time import sleep
I2C_OLED = I2C(scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = SSD1306_I2C(oled_width, oled_height, I2C_OLED)
buf = bytearray(oled_width * oled_height // 8)
fb = FrameBuffer(buf, oled_width, oled_height, MONO_VLSB)
dht = DHT22(Pin(13))
power_button = Pin(19, Pin.IN)
on = True
leds = {'on': Pin(18, Pin.OUT), 'off': Pin(17, Pin.OUT)}
def draw_borders():
fb.line(0, 0, oled_width-1, 0, 1)
fb.line(0, 0, 0, 20, 1)
fb.line(0, 20, oled_width-1, 20, 1)
fb.line(oled_width-1, 0, oled_width-1, 20, 1)
fb.line(0, 20, 0, 45, 1)
fb.line(oled_width-1, 20, oled_width-1, 45, 1)
fb.line(0, 45, 0, oled_height-1, 1)
fb.line(oled_width-1, 45, oled_width-1, oled_height-1, 1)
fb.line(0, oled_height-1, oled_width-1, oled_height-1, 1)
fb.fill(0)
oled.blit(fb, 0, 0)
oled.show()
temperature = 0
humidity = 0
while True:
fb.fill(0)
if power_button.value() and not is_pressed_before:
on = not on
if on:
dht.measure()
temperature = dht.temperature()
humidity = dht.humidity()
draw_borders()
fb.text(' Sensores', 0, 5)
fb.text(f'Temperatura:{round(temperature)}C', 2, 25)
fb.text(f'Umidade:{humidity}%', 2, 50)
is_pressed_before = power_button.value()
if not leds['on'].value():
leds['on'].on()
leds['off'].off()
else:
if not leds['off'].value():
leds['off'].on()
leds['on'].off()
oled.blit(fb, 0, 0)
oled.show()
sleep(1)