Try   HackMD

新人資訊

技術-開發板 #11-把 wifi pwd 從程式撥離,簡易裝置註冊機制

自接觸開發板以來,成功抓取了 sensor 資料,連網回傳雲端,甚至 OTA 的簡單機制測通之後,開發狀況逐漸進入一團迷霧。萬物聯網的想像非常有趣,但牽涉到的技術和設計議題實在太多,不是裝置端程式做好就好,還要對應的雲端 api 和資料庫,沒有畫面的輕量化裝置端如何透過 web 管理,多個裝置和多使用者的環境中如何互不干擾的運作,想出哪一種 sensor 結合哪一種情境對哪一種特定對象可以產生效益,等等等。好在我是活在知識取得相當友善的時代,在迷霧中一點一點的理出頭緒,像拚一個 500 片大拼圖一樣總有一天讓輪廓漸漸完整,這樣的拼圖過程也是非常享受的。今天再來完成一個小拼圖,就是如何讓裝置「脫離我家,佈署出去」。

經過前幾回合的努力,裝置連網已經不成問題了,用 https 呼叫 api 執行 get, post 或是連結 MQ 都沒問題,但裝置一但出了我家就完了,沒有連網能力只能玩 local control 就太沒意思了;不然就是要一一詢問目標場域的 wifi 密碼,可想而知是行不通的,因此一定要有想一個機制,讓使用者自行設定 wifi 資訊。此流程一成功,裝置就有機會服務另外一個非特定人了,設想的流程如下:

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 ssid and pwd,應該也需要包含裝置的唯一性序號或一些重要的屬性,必須在裝置關機後持續存在的。這在 Arduino 是有的,就是 EEPROM (Electrically Erasable Programmble Read Only Memory),是個很正常一般的功能,其讀取與寫入均可輕易查到文件說明。(https://docs.arduino.cc/learn/built-in-libraries/eeprom)。首次開機預設為 AP Mode,且必須自帶一個 Web Server,用一個簡單網頁供使用者設定。使用者則使用手機或任何連網裝置連接 ssid=newmanAP 後開啟 browser 連接 ip: 192.168.4.1。網頁除了顯示裝置的基本資料以外,重點是讓使用者填寫三個欄位:Wifi Mode 改成 1,和 ssid and pwd,按下送出鍵後資料即寫入 EEPROM 永久儲存,重啟後開機程序會檢查 WIFI_MODE 欄位,若是 1 則會嘗試連接所提供的 wifi 網域,連網成功後向雲端回報自己的 ip,然後進入海闊天空的雲端協作應用模式,若 10 秒未成功連線則維持 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 →

以上已經逐步釐清裝置開機時必須執行的跨應用共通程序如下,整合在一個程序 nmInit():

  1. 從 EEPROM 讀取裝置註冊資訊
  2. 依據 MODE 設定連網
  3. 啟動裝置端小型 Web Server
  4. 檢查雲端設定的 OTA Flag,若為 1 時就從雲端設定的 URL 取得更新檔更新自己

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 →

以上開機程序可看出許多與雲端協作的部分,因此也連帶釐清了某些雲端的職責,如將裝置依序號建檔,設定裝置是否執行 OTA,和 OTA 上傳介面和檔案位置,裝置連線成功時回報其 local ip 以便同網域電腦可以連線管理等等,背後的資料庫和 api 邏輯也不須詳述,從 UI 就可看出。

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 →

Dashboard 中可以觀察裝置最後上傳資料的時間,上一次連網啟動的時間,和資料歷史趨勢,已經具備物聯網系統的基本要素了。這是一小塊拼圖拼上了,我樂意繼續拚更多。

By Newman Chen 2022/5/12

參考資料

https://docs.arduino.cc/learn/built-in-libraries/eeprom