from machine import Pin, UART, ADC, PWM
import dht, time
import network
import BlynkLib
##define BLYNK_TEMPLATE_ID "TMPL2SlNfkQpw"
#define BLYNK_TEMPLATE_NAME "1st Semstre Project"
BLYNK_TEMPLATE_ID = "MPL2SlNfkQpw"
BLYNK_TEMPLATE_NAME = "1st Semstre Project"
BLYNK_AUTH_TOKEN = "F6NuhUn1i7p5_YbHPi1Zp4rOYWFENpGb"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
print("Connecting WiFi...")
while not wifi.isconnected():
time.sleep(0.1)
print("WiFi Connected!")
blynk = BlynkLib.Blynk(
BLYNK_AUTH_TOKEN,
tmpl_id=BLYNK_TEMPLATE_ID,
insecure=True
)
@blynk.on("connected")
def blynk_connected():
print("Blynk Connected!")
blynk.sync_virtual(0)
# DHT22 on GPIO14
dht_sensor = dht.DHT22(Pin(14))
# UART
uart = UART(1, baudrate=9600, tx=Pin(17), rx=Pin(16))
while True:
try:
# DHT22
dht_sensor.measure()
t = dht_sensor.temperature() # °C
h = dht_sensor.humidity() # %
# CSV message
msg = "{:.1f},{:.1f},{}\n".format(t, h)
uart.write(msg)
print("Sent:", msg.strip())
except Exception as e:
print("DHT read error:", e)
time.sleep(2)