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

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

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 ESP8266 (WiFi 密碼管理+ WiFi 韌體更新)

*** 如果你用 ESP32S, 請看ESP32這篇

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

今天來跟大家介紹如何把 一般的 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


*** 影片介紹為ESP32, 但基本的概念不變***

Source code for ESP2266

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

//Customized home page
String myHostName = "My-ESP8266";
String myESP8266page = "<a href='/update'>Update Firmware</span></a>";
String myNotFoundPage = "<h2>Error, page not found! <a href='/'>Go back to main page!</a></h2>";

//ESP8266 Access credential
const char *myUsername = "myUserName";
const char *myPass = "myPass";

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    while (!Serial)
        ;
    delay(200);
    Serial.print("\nStarting Async_AutoConnect_ESP8266_minimal on " + String(ARDUINO_BOARD));
    Serial.println(ESP_ASYNC_WIFIMANAGER_VERSION);
    ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "My-ESP8266");
    //ESPAsync_wifiManager.resetSettings();   //reset saved settings
    //ESPAsync_wifiManager.setAPStaticIPConfig(IPAddress(192,168,186,1), IPAddress(192,168,186,1), IPAddress(255,255,255,0));
    Serial.println("Connect to previously saved AP...");
    ESPAsync_wifiManager.autoConnect("My-ESP8266");

    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.print(F("Connected. Local IP: "));
        Serial.println(WiFi.localIP());
    }
    else
    {
        Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status()));
        Serial.println("Can't connect! Enter WiFi config mode...");
        Serial.println("Restart...");
        ESP.reset();
    }

    webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
                 { request->send(200, "text/html", myESP8266page); });
    webServer.onNotFound([](AsyncWebServerRequest *request)
                         { request->send(404, "text/html", myNotFoundPage); });

    AsyncElegantOTA.begin(&webServer, myUsername, myPass); // Start ElegantOTA
    webServer.begin();
    Serial.println("FOTA server ready!");
}

void loop()
{
}

Comments

Popular posts from this blog

Arduino - DFPlayer Mini MP3 Module

Android control color RGB LED using HC-05 Bluetooth with Arduino (Part I)

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