from machine import Pin, I2C, SPI
from time import sleep
from dht import DHT22
from pico_i2c_lcd import I2cLcd
from rtc_api import ler_hora
#from microsd import registar_excessos, armazenar_leituras, inicializar_sd
# Inicialização do microSD
#inicializar_sd()
# VAR Configs
temp_max = 40
intervalo_medicao = 5
# RTC setup
i2c_rtc = I2C(1, sda=Pin(26), scl=Pin(27), freq=400000)
# DHT22 sensor setup
dht = DHT22(Pin(18))
# LCD setup
i2c_lcd = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
I2C_ADDR = i2c_lcd.scan()[0]
lcd = I2cLcd(i2c_lcd, I2C_ADDR, 2, 16)
# Relay setup
relay = Pin(16, Pin.OUT)
relay.off()
while True:
try:
# Leitura do sensor DHT
dht.measure()
temp = dht.temperature()
humidity = dht.humidity()
# Exibição no LCD
lcd.clear()
lcd.write_lcd(f"Temp: {temp:.1f}C")
lcd.write_lcd(f"\nUmid: {humidity:.1f}%")
data = ler_data(i2c_rtc)
current_time = ler_hora(i2c_rtc)
print(data, current_time)
# Controle do relé
if temp > temp_max: # Temperatura limite
relay.on() # Liga o relé
alert = f"Em {current_time} Excedeu {temp_max}°C"
print(alert)
#registar_excessos(alert)
else:
relay.off() # Desliga o relé
sleep(1)
dados = f"Temperatura: {temp}°C, Umidade: {humidity}%"
print(dados)
#armazenar_leituras(dados)
sleep(intervalo_medicao)
except Exception as e:
print("Erro:", e)