from machine import Pin, SoftI2C, ADC
from time import sleep
import ssd1306
import dht
i2c = SoftI2C(sda=Pin(22), scl=Pin(23))
display_largura:int = 128
display_altura:int = 64
oled = ssd1306.SSD1306_I2C(display_largura, display_altura, i2c)
sensor = dht.DHT22(Pin(19))
ldr = ADC(Pin(33))
ldr.atten(ADC.ATTN_0DB)
def DESENHO(x_inicial, y_inicial, largura, altura, cor):
for x in range(x_inicial, x_inicial + largura):
oled.pixel(x, y_inicial, cor)
oled.pixel(x, y_inicial + altura - 1, cor)
for y in range(y_inicial, y_inicial + altura):
oled.pixel(x_inicial, y, cor)
oled.pixel(x_inicial + largura - 1, y, cor)
def atualiza_display():
oled.fill(0)
try:
sensor.measure()
temperatura = sensor.temperature()
umidade = sensor.humidity()
except OSError as e:
temperatura = umidade = None
print('ERRO')
luminosidade = ldr.read() * (3.3 / 4095) * 100
DESENHO(0, 0, 40, 20, 1) # temp
DESENHO(44, 0, 40, 20, 1) # umid
DESENHO(88, 0, 40, 20, 1) # lumi
if temperatura is not None and umidade is not None:
oled.text("Temp.:", 2, 2)
oled.text(f"{temperatura:.1f}C", 2, 10)
oled.text("Umid.:", 46, 2)
oled.text(f"{umidade:.1f}%", 46, 10)
else:
oled.text("Erro DHT", 2, 2)
oled.text("Lumi.:", 90, 2)
oled.text(f"{luminosidade:.1f}", 90, 10)
oled.show()
while True:
atualiza_display()
sleep(2)