2021/07/25

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()
{
}

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

#include <AsyncElegantOTA.h>
const char *ssid = "........";
const char *password = "........";

/****************************************************************************************************************************
  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
    webServer.begin();
    Serial.println("HTTP server started");
}

void loop() {}

2021/07/11

Android control color RGB LED (Revised for ESP32)

ESP32 Source code on the bottom of this article! 

See the original complete project tutorial here: Android RGB LED Arduino Controller




In September 2014, I created this project with Arduino Mini Pro Mini and Android APP loved by so many readers!  There are more than 100,000 App downloads by now!  Since then, quite of few readers asked me how to change the sketch to be used on ESP32 with built-in Bluetooth.  Here it is!! 😀


Be aware that LED PWM pins used on ESP32 were completely different, since Arduino Mini Pro and ESP32 have different architectures. 

For ESP32, I used following pins:

  • Pin 13 = Red
  • Pin 12 = Green
  • Pin 14 = Blue
Caution: ESP32 does not allow to use pin 6, 7, 8, 9, 10, 11 or it will crash and reboot. I tried...😂


Also, there is no analogWrite() function in ESP32, so I need to change analogWrite() to ledcWrite() with 
  • Channel definition for PWM
    • R channel = 0
    • G channel = 1
    • B channel = 2
  • Setup PWM frequency: 5000
  • Setup PWM resolution: 8 (8bit = 255 colors)
Then I need to setup LED with:
  • ledcSetup( channel, frequency, resolution)
And then attach LED pins to channel
  • ledcAttachPin(Pin, channel)
Then write to LED pins with
  • ledcWrite( channel, value for resolution(0~255));


END

2021/07/10

ESP8266/ESP32 AsyncElegantOTA Wi-Fi OTA

It's always troublesome to update firmware for your IoT projects? Tried Arduino BasicOTA examples, but never get it to work? Then you must try AsyncElegantOTA! It's very easy to use and I've succeed the first time I try it. 



For those of you do not have time to watch it all, here is the video timeline for your quick reference.

Video Timeline:

00:13 Tutorial summary 00:53 What's OTA & Why 01:25 Tutorial begin 01:36 Install AsyncElegantOTA library 02:32 Download ESPAsyncWebServer & ESPAsyncTCP 02:57 Ensure Arduino board is capable of OTA 03:41 Where to place downloaded libraries 04:09 Open sample AsyncElegantOTA sketch 04:52 Upload sketch 05:05 Open serial monitor to see ESP8266 IP address 05:36 Access to ESP8266 from browser 06:10 Update ESP8266 firmware from browser 06:31 Where to find .bin file in VS Code 06:54 How to create .bin file in Arduino IDE 08:16 OTA update ESP8266 firmware 09:01 Next video tutorial - How to combine ESP8266 project with AsyncElegantOTA library


Library URL:

2021/06/20

VS Code - One of the best Arduino IDE


In recent years, Microsoft VS Code is considered the one of the best Arduino IDE!! 
I've made a video to show you how to use VS Code to write your Arduino codes, in Chinese only...