import network # For Wi-Fi connectivity
import time # For delays and timing
import urequests # For making HTTP requests
import dht # For interfacing with DHT sensors
from machine import Pin # For controlling GPIO pins
ssid = 'Wokwi-GUEST' # SSID of the Wi-Fi network
password = '' # Password (empty for open networks like Wokwi-GUEST)
THINGSPEAK_API_KEY = 'P95S82N65IHD2859' # Your ThingSpeak Write API key
THINGSPEAK_URL = 'https://api.thingspeak.com/update' # ThingSpeak endpoint
wlan = network.WLAN(network.STA_IF) # Create a WLAN object in station mode,
# the device connetcs to a Wi-Fi network as a client.
# Other option: AP_IF, Acces Point mode -
# the device creates its own Wi-Fi network (hotspot).
wlan.active(True) # Activate the Wi-Fi interface
wlan.connect(ssid, password) # Connect to the specified Wi-Fi network
print("Connecting to Wi-Fi...", end="")
while not wlan.isconnected():
print(".", end="") # Print dots while waiting
time.sleep(0.5) # Wait 0.5 seconds before retrying
# Once connected, print confirmation and IP address
print("\nConnected!")
print("IP address:", wlan.ifconfig()[0])
# Initialize the DTH22 sensor on GPIO pin 15
sensor = dht.DHT22(Pin(15))
# Function to send temperature data to ThingSpeak
def send_to_thingspeak(temp):
if temp is None:
print("Nothing to send")
return
try:
# Send HTTP POST request to ThingSpeak with temperature data
response = urequests.post(
THINGSPEAK_URL,
data = 'api_key={}&field1={}'.format(THINGSPEAK_API_KEY, temp),
headers={'Content-Type': 'application/x-www-form-urlencoded'}
)
print("ThingSpeak response:", response.text) # Print server response
response.close() # Close the connection
except Exception as e:
print("Failed sending data:", e) # Handle any errors
# Main loop: read sensor and send data every 15 seconds
while True:
try:
sensor.measure() # Trigger sensor measurement
temperature = sensor.temperature() # Read temperature in Celcius
print("Temperature:", temperature, "°C") # Display temperature
send_to_thingspeak(temperature)
except Exception as e:
print("Error reading sensor or sending data:", e) # Handle errors
time.sleep(15) # Wait 15 seconds before next reading