2021/07/25

Arduino #27 AsyncWifimanager ElegantOTA ESP32 (WiFi Password Mgnt. + WiFi Firmware Update)

*** If you are using the ESP8266, Please see this: ESP8266

It would be great if every IoT device deployed with OTA capability with Wifimanager.  Then the IoT device would be able to change SSID & password without connecting the device to the computer to update SSID and password as well as FOTA at any time when you wanted.

In this tutorial, let’s combine ESPAsync_Wifimanager and AsyncElegantOTA into a code base to become a handy tool for future IoT projects! 

I have made WiFiManager & AsyncElegantOTA tutorial videos. If you don't know what they are and how they work for IoT projects, I strongly recommend you to watch these videos first.

This time, I used ESPAsync_WiFiManager library, it's because WiFiManager does not work with AsyncElegnatOTA together. We have to change to ESPAsync_WiFiManager.  The main reason for this is that AsnycWiFiManager is using Asynchronous technology. Which means it can accept connections from different sources at the same time. So, while WiFiManager is running on the device, the device can also be a FOTA server to allow firmware to be remotely updated!

        Watch these videos first, if haven't heard about these.

        GitHub Library:

        Sample code:


For those of you do not have the time to watch the entire video, here is the Video Timeline 00:00 Project introduction 00:16 Arduino libraries used in this project 00:29 Short Wifimanager briefing (Check out my previous video) 00:59 Short AsyncElegnatOTA briefing (Check out my previous video) 01:34 Tutorial begin 02:04 Copy ESPAsyncWifimanager sample code to your editor 02:44 Check AsyncElegantOTA sample code 02:50 Start to merge the two libraries 05:16 Setup your IoT AP name 05:58 Copy AsyncElegantOTA code & modify 06:20 Modify server variables 07:05 What does webServer.on do? 07:35 Upload sketch 08:11 Test AsyncWifimanager and setup SSID & Password 09:10 Access ESP32 from browser 09:44 Access ESP32 OTA page

The End.

AsyncWifimanager ElegantOTA ESP32 (WiFi 密碼管理+ WiFi 韌體更新)


*** 如果你用 ESP8266, 請看ESP8266這篇

今天來跟大家介紹如何把 一般的 IoT 專案常用到的兩個 WiFiManager 以及WiFi 韌體更新FOTA (Firmware Over the Air) 兩個整合在一起, 變成一個常用的 Code base.  往後你的IoT 專案呢, 就可以俱備這些基本功能了! 


我曾做過 WiFiManager & AsyncElegantOTA 影片介紹, 如果不了解或是沒看過的朋友, 我建議你先看一下. 這樣比較能清楚的知道這兩個工具的好處!

我這次將會使用 ESPAsync_WiFiManager library, 因為之前的 WiFiManager 無法同時和 AsyncElegnatOTA 一起使用, 一定要改用 ESPAsync_WiFiManager 才行, 主要原因是 AsnycWiFiManager 採用AsyncWebServer library, 而這個程式庫使用了非同步(Async)的方式, 也就是說它可以同時接收不同的連線及不同的指令. 所以不但可以執行 WiFiManager 的同時, 也能當做 FOTA server 來遠端更新韌體!

        GitHub Library 位置:         Sample code 位置:

請直接看影片:

沒時間全部看完的朋友, 可以由時間軸來挑選想知道的片段: 00:00 基本介紹 00:16 本專案用到的 Arduino 程式庫 00:29 Wifimanager 簡介 00:59 AsyncElegnatOTA 簡介 01:34 教學開始 02:04 複製 ESPAsyncWifimanager 程式碼 02:44 檢查 AsyncElegantOTA 程式碼 02:50 開始程式與程式的結合 XD... 05:16 設定 IoT AP 名稱 05:58 複製 AsyncElegantOTA 部份程式 06:20 變更 server 變數 07:05 webServer.on 的作用為何? 07:35 上傳程式 08:11 測試 AsyncWifimanager 並設定 SSID & Password 09:10 由流覽器連到 ESP32 09:44 由流覽器連到 ESP32 OTA page


Source Code for ESP32 (27_AsyncWifimanager-FOTA-ESP32.ino)

#include <AsyncElegantOTA.h>
const char *FOTA_USERNAME = "un";
const char *FOTA_PASSWORD = "pw";

/****************************************************************************************************************************
  Async_AutoConnect_ESP32_minimal.ino
  For ESP8266 / ESP32 boards
  Built by Khoi Hoang https://github.com/khoih-prog/ESPAsync_WiFiManager
  Licensed under MIT license
 *****************************************************************************************************************************/
#if !(defined(ESP32))
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
#endif
#include <ESPAsync_WiFiManager.h>
AsyncWebServer webServer(80);

DNSServer dnsServer;


void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    while (!Serial)
        ;
    delay(200);
    Serial.print("\nStarting Async_AutoConnect_ESP32_minimal on " + String(ARDUINO_BOARD));
    Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION);

    ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "Async_AutoConnect");

    //ESPAsync_wifiManager.resetSettings();   //reset saved settings
    //ESPAsync_wifiManager.setAPStaticIPConfig(IPAddress(192, 168, 132, 1), IPAddress(192, 168, 132, 1), IPAddress(255, 255, 255, 0));
    ESPAsync_wifiManager.autoConnect("MyESP32S");
    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.print(F("Connected. Local IP: "));
        Serial.println(WiFi.localIP());
    }
    else
    {
        Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status()));
    }

    webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
                 { request->send(200, "text/plain", "Hi! I am MyESP32S."); });

    //AsyncElegantOTA.begin(&webServer); // Start ElegantOTA WITHOUT username & password
    AsyncElegantOTA.begin(&webServer, FOTA_USERNAME, FOTA_PASSWORD); // Start ElegantOTA with username & password
    webServer.begin();
    Serial.println("HTTP server started");
}

void loop() {}

4 comments:

  1. Fantastic work Kevin.
    I played around and get it work. But there are still some questions.

    I found that a AP with SSID "ESP32 Async WiFi Manager" is still active. Even after changing the hostname and configuring the a local network.
    So I can access the OTA page via "http://192.168.0.123/update" when connected to my local WiFi, the new hostname is shown correct.
    But if I connect to SSID "ESP32 Async WiFi Manager" I can access "http://192.168.4.1". No config options, but displayed hostname is "ESP32 Async WiFi Manager".

    So first question is, how to disable the "ESP32 Async WiFi Manager" AP after config ?
    And second is, how to start the config page without starting the accesspoint ?

    Henning

    ReplyDelete
  2. 謝謝老師還放了code在這, 太好了

    ReplyDelete
  3. 老師我在Arduino IDE安裝ESPAsync_WiFiManager程式庫時, 問我要安裝相關程式庫否, 回答是則出現"no protocol"訊息, 並未安裝。

    ReplyDelete
  4. 不過安裝1.12.1版好像就通過了

    ReplyDelete