# from machine import Pin
# from time import sleep
# from dht import DHT22
# dht=DHT22(Pin(15))
# while True:
# dht.measure()
# temp=dht.temperature()
# hum=dht.humidity()
# print(f"Temperature: {temp}C Humidity: {hum}% ")
import machine
import time
import urequests
import network
import ujson
import dht
# ThingSpeak settings
THINGSPEAK_API_KEY = "4GRWNW3IQIM7BEUA"
THINGSPEAK_URL = "http://api.thingspeak.com/update"
# Wi-Fi settings
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""
# DHT22 (AM2302) sensor on GPIO 4
dht_sensor = dht.DHT22(machine.Pin(4))
# Function to read room temperature and humidity
def read_room_temperature_humidity():
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
return temperature, humidity
# Function to send data to ThingSpeak
def send_data_to_thingspeak(temperature, humidity):
data = {
"api_key": THINGSPEAK_API_KEY,
"field1": temperature,
"field2": humidity,
}
response = urequests.post(THINGSPEAK_URL, data=ujson.dumps(data), headers={"Content-Type": "application/json"})
response.close()
# Initialize the Wi-Fi connection
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
# Wait for the Wi-Fi connection to establish
while not wifi.isconnected():
time.sleep(1)
print("Connected to Wi-Fi")
if __name__== "__main__":
try:
while True:
temperature, humidity = read_room_temperature_humidity()
send_data_to_thingspeak(temperature, humidity)
print("Data sent to ThingSpeak")
time.sleep(15) # Send data every 15 seconds (adjust the interval as needed)
except KeyboardInterrupt:
pass