最近開發ESP8266時,線路都已經接好,如果要燒錄又要把線拔開,非常麻煩。
於是在Github上找尋有沒有能透過WiFi簡單OTA的library。
發現了一個好物 AsyncElegantOTA
主要功能就是透過AsyncTCP 與 AsyncWebServer , 做出非阻斷式的OTA更新功能。
完完全全符合我的需求阿,分享給大家
範例程式碼也相當簡單。
ESP8266的範例程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <ESP8266WiFi.h> #include <ESPAsyncTCP.h> #include <ESPAsyncWebServer.h> #include <AsyncElegantOTA.h> const char* ssid = "ESP-OTA-TEST"; const char* password = "12345678"; AsyncWebServer server(80); void setup(void) { Serial.begin(115200); WiFi.mode(WIFI_AP); WiFi.begin(ssid, password); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(200, "text/plain", "Hi! I am ESP8266."); }); AsyncElegantOTA.begin(&server); // Start ElegantOTA server.begin(); Serial.println("HTTP server started"); } void loop(void) { AsyncElegantOTA.loop(); } |
ESP32的範例程式碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <WiFi.h> #include <AsyncTCP.h> #include <ESPAsyncWebServer.h> #include <AsyncElegantOTA.h> const char* ssid = "ESP-OTA-TEST"; const char* password = "12345678"; AsyncWebServer server(80); void setup(void) { Serial.begin(115200); WiFi.mode(WIFI_AP); WiFi.begin(ssid, password); Serial.print("IP address: "); Serial.println(WiFi.localIP()); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(200, "text/plain", "Hi! I am ESP32."); }); AsyncElegantOTA.begin(&server); // Start ElegantOTA server.begin(); Serial.println("HTTP server started"); } void loop(void) { AsyncElegantOTA.loop(); } |
只須include 相關library , 開啟WiFi後,再呼叫AsyncElegantOTA.begin(&server),最後在loop定期更新。
這樣即可完成OTA功能
連接 http://192.168.4.1/update 即可看到要你上傳bin file的頁面。
即可上傳bin file做OTA更新!