# ex_3_2_01_dht22_basic.py (จำลอง: https://wokwi.com/projects/469815189066571777)
from machine import Pin
import dht
import time
sensor = dht.DHT22(Pin(25)) # DATA ของ DHT22 ต่อที่ GPIO25
while True:
try:
sensor.measure() # สั่งวัดค่า (ใช้เวลาราว 0.25 วินาที)
temp = sensor.temperature() # องศาเซลเซียส
hum = sensor.humidity() # เปอร์เซ็นต์ความชื้นสัมพัทธ์
print("อุณหภูมิ: %.1f °C, ความชื้น: %.1f %%" % (temp, hum))
except OSError:
print("[ERROR] อ่าน DHT22 ไม่สำเร็จ ตรวจสายและไฟเลี้ยง")
time.sleep(2) # DHT22 อ่านซ้ำได้ไม่เร็วกว่า 2 วินาที