from machine import Pin
import time
import dht
import network
import urequests
# ====== WIFI (WORKWI) ======
SSID = "Wokwi-GUEST"
PASSWORD = ""
# ====== THINGSPEAK ======
API_KEY = "THZOBT2C1VSPMFRQ"
URL = "http://api.thingspeak.com/update"
# ====== LEDs ======
led_vm = Pin(13, Pin.OUT) # vermelho
led_vd = Pin(12, Pin.OUT) # verde
led_az = Pin(11, Pin.OUT) # azul
# ====== SENSOR ======
sensor = dht.DHT22(Pin(15))
# ====== FAIXA IDEAL ======
TEMP_MIN = 20
TEMP_MAX = 24
# ====== CONECTAR WIFI ======
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
print("Conectando ao Wi-Fi (Workwi)...")
while not wifi.isconnected():
time.sleep(1)
print("Conectado!")
print("IP:", wifi.ifconfig()[0])
# ====== LOOP ======
while True:
try:
sensor.measure()
temp = sensor.temperature()
umid = sensor.humidity()
print("Temp:", temp, "°C | Umidade:", umid, "%")
# LEDs
led_vm.off()
led_vd.off()
led_az.off()
if temp > TEMP_MAX:
led_vm.on()
elif temp < TEMP_MIN:
led_az.on()
else:
led_vd.on()
# ====== ENVIO ======
url = f"{URL}?api_key={API_KEY}&field1={temp}&field2={umid}"
response = urequests.get(url)
print("ThingSpeak:", response.text)
response.close()
except Exception as e:
print("Erro:", e)
# mínimo 15s
time.sleep(15)
Loading
pi-pico-w
pi-pico-w