import network
import urequests
import time
import dht
from machine import Pin
# --- CONFIGURAÇÕES ---
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
API_KEY = "86R7ICEK8FI5LY8L" # Substitua pela sua Write API Key
URL = "http://api.thingspeak.com/update"
# Configuração de Hardware
sensor = dht.DHT22(Pin(15))
led_vm = Pin(13, Pin.OUT)
led_vd = Pin(12, Pin.OUT)
led_az = Pin(11, Pin.OUT)
# Função para conectar ao Wi-Fi
def conectar_wifi():
print("Conectando ao Wi-Fi...", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASSWORD)
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.5)
print(" Conectado!")
# Lógica dos LEDs
def atualizar_leds(temp):
led_vm.off(); led_vd.off(); led_az.off()
if temp > 24:
led_vm.on()
elif temp < 18:
led_az.on()
else:
led_vd.on()
# Execução Principal
conectar_wifi()
while True:
try:
sensor.measure()
t = sensor.temperature()
u = sensor.humidity()
print(f"\nTemp: {t}°C | Umidade: {u}%")
atualizar_leds(t)
# Envio para o ThingSpeak
print("Enviando dados para nuvem...")
request_url = f"{URL}?api_key={API_KEY}&field1={t}&field2={u}"
res = urequests.get(request_url)
print(f"Resposta: {res.status_code}") # 200 significa sucesso
res.close()
except Exception as e:
print("Erro ao processar/enviar dados:", e)
# O ThingSpeak gratuito exige um intervalo mínimo de 15 segundos
print("Aguardando 20 segundos para o próximo envio...")
time.sleep(20)