import network
import time
from machine import Pin
import dht
import urequests
MQTT_CLIENT_ID = "micropython-weather-demo"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-weather"
sta_if = network.WLAN(network.STA_IF)
print("Connecting to Internet...")
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
pass
print("connected to wifi")
print("Network config:", sta_if.ifconfig())
sensor = dht.DHT22(Pin(16))
token = "ISL5myKklLk0aewVX2bqysAuMpQQEyZe"
while True:
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print("Temperature: {:.2f}°C".format(temperature))
print("Humidity: {:.2f}%".format(humidity))
temp_response = urequests.get(
"https://blr1.blynk.cloud/external/api/update?token={}&V0={}".format(token, temperature)
)
humid_response = urequests.get(
"https://blr1.blynk.cloud/external/api/update?token={}&V1={}".format(token, humidity)
)
print("Data sent to Blynk")
temp_response.close()
humid_response.close()
except Exception as e:
print("Error:", e)
time.sleep(2)