import network
import time
from machine import Pin, I2C, PWM, ADC
import ssd1306
import dht
# ตัวแปรสำหรับเก็บสถานะระบบ
wifi_connected = False
current_angle = 0
last_pot_val = 0
# 1. ตั้งค่า Pin สำหรับหลอด LED สองสี
led_blue = Pin(18, Pin.OUT)
led_red = Pin(19, Pin.OUT)
# สถานะเริ่มต้น: สีแดงติด (ยังไม่เชื่อมต่อ) สีน้ำเงินดับ
led_red.value(1)
led_blue.value(0)
# 2. ตั้งค่า I2C สำหรับจอ OLED (SDA=GP16, SCL=GP17)
i2c = I2C(0, sda=Pin(16), scl=Pin(17))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# 3. ตั้งค่าเซ็นเซอร์ DHT22 (GP20)
sensor = dht.DHT22(Pin(20))
# 4. ตั้งค่า Servo Motor (GP14)
servo = PWM(Pin(14))
servo.freq(50)
def set_servo_angle(angle, source="ระบบ"):
global current_angle
current_angle = angle
min_duty = 1638
max_duty = 8192
duty = int(min_duty + (max_duty - min_duty) * (angle / 180))
servo.duty_u16(duty)
print(">> Servo หมุนไปที่: {} องศา (สั่งจาก: {})".format(angle, source))
# 5. ตั้งค่าปุ่มกด (GP27=เหลือง, GP22=แดง, GP21=เขียว)
# ใช้ Pin.PULL_DOWN เผื่อกรณีต่อสายตรงจาก 3V3 มาเข้าปุ่ม
sw_yellow = Pin(27, Pin.IN, Pin.PULL_DOWN)
sw_red_btn = Pin(22, Pin.IN, Pin.PULL_DOWN)
sw_green = Pin(21, Pin.IN, Pin.PULL_DOWN)
# 6. ตั้งค่า Potentiometer (ใช้ขา GP26)
pot = ADC(Pin(26))
# ข้อมูล Wi-Fi
ssid = "Wokwi-GUEST"
password = ""
def connect_wifi():
global wifi_connected, last_pot_val
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
oled.fill(0)
oled.text("Connecting to", 0, 0)
oled.text("WiFi...", 0, 15)
oled.show()
print("กำลังเชื่อมต่อ Wi-Fi...")
while not wlan.isconnected():
time.sleep(0.5)
ip_address = wlan.ifconfig()[0]
print("เชื่อมต่อสำเร็จ! IP:", ip_address)
# สลับไฟ LED เมื่อเชื่อมต่อสำเร็จ
led_red.value(0)
led_blue.value(1)
wifi_connected = True
last_pot_val = pot.read_u16()
return ip_address
# เริ่มเชื่อมต่อ Wi-Fi
ip = connect_wifi()
set_servo_angle(0, "เริ่มต้นระบบ")
# จัดการเวลาสำหรับ DHT22 (ทุก 5 นาที)
dht_interval = 5 * 60 * 1000
last_dht_read = time.ticks_ms() - dht_interval
temp = 0.0
hum = 0.0
# ลูปการทำงานหลัก
while True:
current_time = time.ticks_ms()
# --- 1. ตรวจสอบการกดปุ่ม (Polling) แบบเสถียร ---
if wifi_connected:
if sw_yellow.value() == 1:
set_servo_angle(0, "ปุ่มเหลือง")
last_pot_val = pot.read_u16() # รีเซ็ตค่า Pot ป้องกันการดึงกลับ
time.sleep(0.1) # หน่วงเวลาเล็กน้อยกันการกดซ้ำ (Debounce)
elif sw_red_btn.value() == 1:
set_servo_angle(90, "ปุ่มแดง")
last_pot_val = pot.read_u16()
time.sleep(0.1)
elif sw_green.value() == 1:
set_servo_angle(176, "ปุ่มเขียว")
last_pot_val = pot.read_u16()
time.sleep(0.1)
# --- 2. ตรวจสอบและอ่านค่า DHT22 ทุกๆ 5 นาที ---
if time.ticks_diff(current_time, last_dht_read) >= dht_interval:
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
print("อัปเดต DHT22 -> Temp: {:.1f} C, Hum: {:.1f} %".format(temp, hum))
except OSError as e:
print("อ่านค่าเซ็นเซอร์ล้มเหลว")
last_dht_read = current_time
# --- 3. ตรวจสอบการหมุน Potentiometer ---
if wifi_connected:
current_pot = pot.read_u16()
if abs(current_pot - last_pot_val) > 800:
angle = int((current_pot / 65535) * 180)
set_servo_angle(angle, "วอลลุ่ม")
last_pot_val = current_pot
# --- 4. อัปเดตหน้าจอ OLED ---
oled.fill(0)
oled.text("IP: {}".format(ip), 0, 0)
oled.text("Temp: {:.1f} C".format(temp), 0, 15)
oled.text("Hum: {:.1f} %".format(hum), 0, 30)
oled.text("Servo: {} deg".format(current_angle), 0, 45)
oled.show()
time.sleep(0.1)