Try   HackMD

新人資訊

技術-開發板 #10-來踢 OTA 這鐵板了,測通了但難的還在後面的雲端管理機制

OTA,Over The Air,無線更新板子的韌體,有三種方式:一為採用 Arduino IDE 透過無線連線上傳到板子;這個我認為是脫褲子放屁,一般用 IDE 開發時,為了給板子供電都會插著 USB 線,所以我連測都懶得測。二為透過板上的小型 Web Server,用網頁連上去上傳檔案更新韌體;這有點用,但一般情況板子並未擁有對外 IP,所以只能在同網域內操作,用途也挺有限。第三種才是測底解決無所不在的萬物連網裝置其韌體更新的問題,就是透過公共 Internet 網路更新韌體。

第二種用途有限如雞肋,但有測通還是紀錄一下。主要是參考這篇:https://randomnerdtutorials.com/esp8266-nodemcu-ota-over-the-air-arduino/
經過整理程式碼超級短,也就是說 AsyncElegntOTA 這套件真是做得太優雅了,只有它上面兩個 dependency 要用匯入 zip 的方式安裝稍微麻煩,真正的程式只有一行,就是在 Web Server 啟動前一行把 server 指標傳給它就行了!

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 →

上傳後觀察 COM Port 輸出得到板子 IP,輸入 browser 確認 web server 正常。

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 →

接下來就絕了,只要在網址列後面輸入 /update 立即出現一個簡潔的介面可以選取檔案更新韌體,當然韌體要先編譯成 bin 檔。若板子當時是 AP 模式也可以,只是 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 →

接下來才是我所最關心的方式,把更新檔放置在 Internet 某個位置,板子在某種條件下觸發更新機制,自己去抓取來更新自己。網上看到許多範例大都非常複雜,也有公司專門提供 OTA 服務的,可見技術門檻頗高。想到要事先定義產品名稱或 ID,適用的硬體型號,韌體類型和版本,該如何比對而觸發更新動作,當裝置很多時如何劃分區域或群組依序更新等等!看得暈頭轉向。但我決定從基本著手:放置一個檔案在雲端,要有提供 http 存取方法的;這時好朋友 Azure 就來了。隨意開一個 Storage Account 和 Container,上傳一個簡單的程式標註版本號 1.0.0.1,我欲在本地端寫一個版本 1.0.0.0 的,開機後稍待片刻就去抓取這個檔案執行雲端更新。

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 →

本地端程式如下,連網是基本動作已不須贅述,然後等 20 秒一到執行更新,必要的涵式庫為 ESP8266httpUpdate.h,關鍵程式也只有一行 ESPhttpUpdate.update()

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 →

觀察 COM Port 輸出,上傳完成後首先執行連網,印出版本號 1.0.0.0,過 20 秒執行更新,沒有錯誤訊息,直接重開機,再次執行連網程序,印出版本號 1.0.0.1。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 →

完成這基本動作後,下一步挑戰是最難的雲與端協同合作機制,判斷何時該更新,當新版本發送到雲端空間後,裝置端如何得知,如何確認檔案是自己本身適用的,該由雲端人員觸發還是要等裝置端人員確認,一連串問題牽涉到程式技術和多方人員互動,開發端多角色,使用者端也有多角色,這就是物連網系統的運作核心!也是要生意有做起來才有機會面對這些問題,秉持開發熱情就一直試下去看能走到甚麼程度了。

By Newman Chen 2022/5/4