import network
import urequests
import time
import dht
from machine import Pin
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
THINGSPEAK_API_KEY = "PBR0AFTKFOQSCALE"
THINGSPEAK_URL = "https://thingspeak.com/update"
dht_sensor = dht.DHT22(Pin(15))
print("connecting to wifi...")
wlan = network.WLAN (network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID,WIFI_PASS)
while not wlan.isconnected():
time.sleep(0.5)
print (".",end="")
print ("\n connected to WIFI")
while True:
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print ("Temperature:", temp,"°C \n Humidity:", hum,"%")
response = urequests.get(f"{THINGSPEAK_URL}?api_key={THINGSPEAK_API_KEY}&field1={temp}&field2={hum}")
print ("Thingspeak response:", response.text)
response.close()
except Exception as e:
print("Error:",e)
time.sleep(5)