MicroPython WIFI Mode ตอนที่ 2 (Station)

Sittipong Jansorn
Jul 20, 2017 · 2 min read

หลังจากที่เราได้ทำความรู้จักการใช้งาน WIFI ของ ESP8266 ในรูปแบบของ AP Mode ในหัวข้อนี้เราจะมาทดสอบการใชงานในรูปแบบ Station Mode ซึ่งเราต้อมี Access Point ที่จะใช้ในการทดสอบครั้งนี้ด้วย

ก็เหมือนกับ AP ใน MicroPython จะมี package ที่ใช้ในการติดต่อสื่อสารผ่านเครือข่ายนั่นก็คือ

import metwork

ในการสร้าง object WLAN สำหรับ Station Mode จะเหมือนกับ AP Mode ต่างกันตรงที่ interface_id จะต้องใช้เป็น network.STA_IF

classnetwork.WLAN(interface_id)

wlan

โดยที่ตั่งชื่อเป็น wlan0 (จะเปลี่ยนเป็นชื่ออื่นก็ได้) ตรวจสอบว่า ใน Mode Station ถูกกำหนดให้ทำงานหรือยัง

wlan0.active()

ถ้า return ค่ามาเป็น True แสดงว่าโหมด Station ได้ถูกเปิดใช้งานไว้เรียบร้อยแล้ว แต่ถ้า return มาเป็น False แสดงว่ายังไม่ได้เปิดใช่งานนั่นเอง

เปิดการใช้งานใน Mode Station

wlan0.active(True)

กำหนดการเชื่อมต่อ WIFI Network

wlan0.connect(‘<your ESSID>’, ‘<your password>’)

connect result

ตรวจสองสถานะการเชื่อมต่อ

wlan0.isconnected()

connect status

ถ้า return ค่ามาเป็น True แสดงว่าสามารถเชื่อมต่อกับ Access Point ได้เรียบร้อย แต่ถ้าเป็น False แสดงว่าไม่สามารถเชื่อมต่อกับ Access Point ได้ ให้ตรวจสอบ SSID กับ Password อีกครั้งแล้วทำการ connect ใหม่

ตรวจสอบสถานะของ IP Address

wlan0.ifconfig()

ifconfig

ปิดการใช้งานใน Mode Station

wlan0.active(False)

จากนี้ไป ESP8266 ก็จะสามารถโลดแล่นไปในโลกของ Internet ได้แล้วด้วย Micropython

)

    Sittipong Jansorn

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade