新人資訊
技術-開發板 #8-搞懂 wifi 的兩種模式 station and ap
檢討目前的研究成果,雖然可以連網傳遞資料到雲端,但是需要把 ssid and pwd 寫死在程式裡,一旦裝置換了地點就會使資料流程中斷,也無從管理裝置的狀態。有沒有一種方式能夠克服無 wifi 的情境?有的,就是把板子當成 access point,本身的 ssid, pwd and ip 可以事先設好固定並公布給使用者,就可在無線訊號涵蓋的周邊範圍用任何連網設備,連接到板子並取得同網域的 ip。再加上運行在板子的小型 web server,可讓同網域裝置開啟 browser 執行簡單的管理任務。
如下簡易示意圖,AP Mode 相對單純,不需要任何既有的網路環境而可獨立運作,Station Mode 則相對複雜;面對一個新的環境,若可先用 AP Mode 對板子做設定,設定資料用程式化寫入板子的永久記憶體,在重開機之後再轉為 Station Mode,開啟雲端多樣化的應用模式,這是個理想的使用流程。其實目前市面上面對社會大眾的網路產品,大多已經實現此方式,若我能獨立開發出來那真是太酷了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
之前誤打誤撞隨意亂買的 WeMos D1 R1,擁有 wifi 晶片的整合和跟 Arduino 完全一樣的尺寸和開發方式,接腳稍有不同但是都可對應到所以覺得不錯;後來又入手了一塊 WeMos D1 R2,沒想到就踩雷了!持續一天不斷跟這個錯誤訊息奮戰「esptool FatalError Timed out waiting for packet content」,完全無法上傳燒錄,讓人好生氣!最後終於決定暫停正面對決另闢出路,換了這一片更小更原生的 ESP8266 NodeMcu,整個豁然開朗,wifi 的各種功能和 web server, OTA 等測試均順利推進,太優秀了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
首先測試 Station Mode,include 一個函式庫,執行兩個功能:scan_wifi() 找出當前環境有廣播出來的無線訊號,connect_wifi() 則是連上事先已知的家庭網路,相關訊息均透過 COM Port 輸出,如下很快測試成功了。連線成功後可以透過 MQTT Client, Http Client 網雲端丟資料,這在先前已經寫過了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
現在要切入另一個角度了,就是不僅可往雲端丟資料,還要實現使用者透過網路來控制這塊板子,這塊板子必須有回應使用者訊息的能力;既然板子已經擁有一個 Local IP 可以讓使用者找到,下一步就是在其上運行一個小的 Web Server 來回應使用者的訊息了。在標準範例中找到 ESP8266WebServer 函式庫有測試成功,但後來在讀 OTA 文章時發現另一個 ESPAsyncWebServer 寫法更精簡,所以列為首選。只是它和另一個 dependency 都沒有在 Arduino IDE 的程式庫管理員裏,必須自行下載 zip 檔再執行「匯入程式庫」
https://github.com/me-no-dev/ESPAsyncWebServer
https://github.com/me-no-dev/ESPAsyncTCP
環境準備好後就幾行程式可搞定,比 ESP8266WebServer 還好寫多了。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
打開 Browser 輸入 IP,正常回應,運行的非常正常,哈哈。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Web Server 搞定之後就可以開始測試 AP Mode 了,關鍵程式如下:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
打開電腦的 wifi 設定畫面,果然出現 ESPap,打入密碼成功連線;打開 Browser 也成功得到回應訊息,YA!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接下來的議題還有,如何用一個簡單的介面讓使用者設定自己的 ssid and pwd 並寫入長期記憶體,重開機時可以判斷之並轉為 station 模式;還有韌體更新 OTA 的方式等,待續。
By Newman Chen 2022/4/30