import dht
import network
import urequests
import time
from machine import Pin
# Wi-Fi credentials
SSID = "Wokwi-GUEST"
PASSWORD = ""
# ThingSpeak
API_KEY = "YOUR_API_KEY" # Replace with your Write API Key
URL = "http://api.thingspeak.com/update"
# DHT22 sensor
sensor = dht.DHT22(Pin(4))
# Wi-Fi connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting to Wi-Fi...")
while not wlan.isconnected():
time.sleep(1)
print("✅ Connected:", wlan.ifconfig())
while True:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temp:", temp, "°C Humidity:", hum, "%")
try:
response = urequests.get(f"{URL}?api_key={7L9BGOG5W35QPP9V}&field1={temp}&field2={hum}")
print("ThingSpeak response:", response.text)
response.close()
except Exception as e:
print("❌ Upload failed:", e)
time.sleep(20) # respect ThingSpeak rate limit