import dht
import machine
import time
# Replace with your ThingSpeak API key
api_key = "DD55NPNRNJOBBCOZ"
# Setup DHT22 sensor on pin 15 (Wokwi default)
sensor = dht.DHT22(machine.Pin(15))
def read_sensor():
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("Temperature:", temp, "°C")
print("Humidity:", hum, "%")
return temp, hum
except Exception as e:
print("Sensor read error:", e)
return None, None
def simulate_thingspeak_upload(temp, hum):
url = f"http://api.thingspeak.com/update?api_key={api_key}&field1={temp}&field2={hum}"
print("---- ThingSpeak Upload Simulation ----")
print("GET", url)
print("--------------------------------------")
def main():
while True:
temp, hum = read_sensor()
if temp is not None and hum is not None:
simulate_thingspeak_upload(temp, hum)
time.sleep(15) # ThingSpeak allows 15 sec delay between updates
main()