from machine import Pin
import time
import dht
import network
import urequests
# ===== WIFI =====
SSID = "Wokwi-GUEST"
PASSWORD = ""
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
print("Conectando ao WiFi...", end="")
while not wifi.isconnected():
print(".", end="")
time.sleep(0.5)
print("\nConectado!")
# ===== THINGSPEAK =====
API_KEY = "47NDQO6XHUSSV6M6"
URL = "http://api.thingspeak.com/update"
# ===== LEDs =====
led_vm = Pin(13, Pin.OUT)
led_az = Pin(12, Pin.OUT)
led_vr = Pin(11, Pin.OUT)
# ===== SENSOR =====
sensor = dht.DHT22(Pin(15))
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temperatura:", temp)
print("Umidade:", hum)
# Desliga LEDs
led_vm.off()
led_az.off()
led_vr.off()
# Controle por temperatura
if temp < 20:
led_az.on()
elif temp < 30:
led_vr.on()
else:
led_vm.on()
# ===== ENVIO PARA THINGSPEAK =====
resposta = urequests.get(
f"{URL}?api_key={API_KEY}&field1={temp}&field2={hum}"
)
resposta.close()
print("Dados enviados!")
except Exception as e:
print("Erro:", e)
time.sleep(15) # ThingSpeak exige mínimo ~15s