from machine import Pin
import network
import urequests
import time
import dht
# WiFi credentials
ssid = "Wokwi-GUEST"
password = ""
# ThingSpeak API
api_key = "EOWCXQTORJ8338S4"
# Connect to WiFi
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
print("Connecting to WiFi...")
time.sleep(1)
print("Connected to WiFi")
# Initialize DHT11
sensor = dht.DHT22(Pin(15))
while True:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temperature:", temp, "C")
print("Humidity:", hum, "%")
url = "https://api.thingspeak.com/update?api_key=" + api_key + "&field1=" + str(temp)+ "&field2=" + str(hum)
#url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={temp}&field2={hum}"
response = urequests.get(url)
print("Data Sent to ThingSpeak")
response.close()
except Exception as e:
print("Error:", e)
time.sleep(15) # ThingSpeak minimum delay