import network
import time
from machine import Pin, I2C
import ssd1306
# ตั้งค่า I2C สำหรับจอ OLED (SDA=GP16, SCL=GP17) ตามรูปที่คุณต่อไว้
i2c = I2C(0, sda=Pin(16), scl=Pin(17))
# กำหนดขนาดจอ OLED เป็น 128x64 พิกเซล
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# ข้อมูล Wi-Fi ของระบบจำลอง Wokwi
ssid = "Wokwi-GUEST"
password = ""
def connect_wifi():
# ตั้งค่าโหมด Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# แสดงข้อความบนจอ OLED ระหว่างรอเชื่อมต่อ
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 มาเก็บไว้
ip_address = wlan.ifconfig()[0]
# แสดงผลบนจอ OLED และ Console
print("เชื่อมต่อสำเร็จ!")
print("IP Address:", ip_address)
oled.fill(0) # ล้างหน้าจออีกครั้ง
oled.text("WiFi Connected!", 0, 0)
oled.text("IP Address:", 0, 20)
oled.text(ip_address, 0, 35) # แสดงหมายเลข IP
oled.show()
# เรียกใช้งานฟังก์ชันเพื่อเริ่มทำงาน
connect_wifi()