2022/08/14

Android #36 IoT 基本程式庫 Wifimanager FOTA MQTT (IOT Essential Code base with wifimanager, ElegantOTA, & MQTT)

Hi, Welcome to weekend project with Stonez56

This week's Arduino episode #37, "Essential IoT Code Base- Wifimanager FOTA + MQTT"

Here is the source code: https://stonez56.blogspot.com/2022/08/android-36-iot-wifimanager-fota-mqtt.html 


Today, I will show how to combine three commonly used IoT libraries;

WiFimanager, AsyncElegantOTA, and MQTT To become a common IoT code base. So, for our future projects, ESP8266 or ESP32, would have these basic functions.

Let's first look at these three libraries:

  • Wifimanager = Wi-Fi password management, no need to re-burn the code to change the Wi-Fi SSID & password
  • FOTA = Use Wi-Fi to update ESP32 code, you can burn code without connecting to USB
  • MQTT = Streamlined and lightweight communication protocol (Message Queueing Telemetry Transport)


This episode will only introduce how to integrate MQTT into Wifimanager + AsyncElegantFOTA.

To learn how to start Wifimanager + AsyncElegantOTA, Please watch Android #27 Wifimanager + OTA for ESP32/ESP8266 (ESPAsyncWifimanager / ESPAsyncElegantOTA)) https://youtu.be/UlRLTvl4DRc 


==References==

Arduino Library URL:


==Sample code URL==

ESPAsync_WiFiManager minimal: https://github.com/khoih-prog/ESPAsync_WiFiManager/blob/master/examples/Async_AutoConnect_ESP32_minimal/Async_AutoConnect_ESP32_minimal.ino 


ESP32 Async OTA : https://github.com/ayushsharma82/AsyncElegantOTA/blob/master/examples/ESP32_Async_Demo/ESP32_Async_Demo.ino 


If you don't have enough time, you can also select the part you want to watch from the timeline below!

Video Timeline: 00:00 Start 01:39 Demonstration of project results 01:57 mqttgo.io MQTT server, free, fast, & stable 05:52 Install three libraries 07:14 Added two ESP32 PINs as MQTT example 07:41 Encrypt the firmware update (ElegantOTA) page 11:44 Explain how the MQTT callback function works 13:07 Control ESP32 I/O pins with MQTT 15:16 Future program can be written from here

=====中文版本======

大家好, 歡迎收看本週的Weekend project with Stonez56。 

本周要進行的是 Arduino 第36集,"IoT 基本程式庫 - Wifimanager FOTA + MQTT”

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


  • Wifimanager = WiFi 密碼管理, 不用重新燒 Code 改 Wifi SSID & 密碼
  • FOTA = 用Wi-Fi更新ESP32程式碼, 不用接USB就可以燒Code
  • MQTT = 精簡輕量化的通訊協定(Message Queueing Telemetry Transport)


本集僅會介紹如何把 MQTT 整合到 Wifimanager + AsyncElegantFOTA

若想了解如何整合 Wifimanager + AsyncElegantOTA, 

請觀看 Arduino #26 AsyncWifimanager ElegantOTA ESP32 (WiFi 密碼管理+ WiFi 韌體更新) https://youtu.be/zepkinJ46Zg  




==參考資料==

Arduino Library URL: 


==Sample code URL==


如果時間不夠的朋友, 也可以從以下的時間軸挑選想看的部份即可! 

影片時間軸:

影片時間軸: 00:00 Start 01:39 專案成果示範 01:57 mqttgo.io 快速免費的 MQTT 伺服器 05:52 安裝三支程式庫 07:14 加入兩個 ESP32 PIN做為MQTT範例 07:41 為韌體更新(ElegantOTA)頁面加密 11:44 說明MQTT callback 函數運作方式 13:07 MQTT由此控制ESP32 PIN 15:16 把你的程式可以寫在這裡


IoT基本程式庫原始程式:


/** 
 * Arduino code base for IoT Projects
 * By: Stonez56 aka Kevin Chen 2022-08-16
 * 
 * This source code combined three most common Arduino libraries for IoT Projects, 
 * ESPAsync_Wifimanager, AsyncElegantOTA, & Pubsubclient MQTT Client.
 * 
 * 1. ESPAsync_WiFiManager: https://github.com/khoih-prog/ESPAsync_WiFiManager 
 * 2. AsyncElegantOTA: https://github.com/ayushsharma82/AsyncElegantOTA 
 * 3. Pubsubclient MQTT Client: https://github.com/knolleary/pubsubclient 
 * 
 * YouTube video: https://youtu.be/giFHp9RSMvQ
 * Source code is available my blog: https://stonez56.blogspot.com/2022/08/android-36-iot-wifimanager-fota-mqtt.html 
 **/

/*
 * Define Relay pins as examples to send/receive MQTT messages
 * **/
#define RELAY_PIN_1 12
#define RELAY_PIN_2 14

/** 
 * Perform OTAs for ESP8266 / ESP32 Elegantly with password protection! 
 * https://github.com/ayushsharma82/AsyncElegantOTA
 * **/
#include <AsyncElegantOTA.h>
const char *FOTA_USERNAME = "un";
const char *FOTA_PASSWORD = "pw";

/** 
 * Add MQTT client here` PubSubClient V2.8 by Nick O'Leary
 * https://github.com/knolleary/pubsubclient 
*/
#include <WiFiClient.h>
#include <PubSubClient.h>
#define MSG_BUFFER_SIZE (1024)
char msg[MSG_BUFFER_SIZE];

/** Taiwan No. 1 Free MQTT Server!! **/
const char *mqtt_server = "mqttgo.io";
const int mqtt_port = 1883;

/****************************************************************************************************************************
  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);

//Start DNS server
DNSServer dnsServer;

//MQTT - Start WiFi client and connect to MQTT server
WiFiClient espClient;
PubSubClient mqtt_client(espClient);

//Define device name
String DEVICE_NAME = "Dual_Relay_Switch";
String home_page_message = "";

void setup()
{
    Serial.begin(115200);
    pinMode(RELAY_PIN_1, OUTPUT);
    pinMode(RELAY_PIN_2, OUTPUT);

    while (!Serial)
        ;
    delay(200);
    Serial.print("\nAsyncWifimanager started on " + String(ARDUINO_BOARD) + "\n");

    //Initialize ESPAsyncWifimanager instance and assign Wi-Fi Client name "AsyncAutoConnect"!
    //This name and IP address can be checked from the router
    ESPAsync_WiFiManager ESPAsync_wifiManager(&webServer, &dnsServer, "Dual_Relay_Switch");

    //####### RESET SAVED WIFI SETTINGS #############
    //ESPAsync_wifiManager.resetSettings();   

    //ESPAsync_wifiManager.setAPStaticIPConfig(IPAddress(192, 168, 132, 1), IPAddress(192, 168, 132, 1), IPAddress(255, 255, 255, 0));
    //ESPAsync_wifiManager.autoConnect("Stonez_ESP32S", "AP-NAME", "AP-PASSWORD");
    
    ESPAsync_wifiManager.autoConnect("Dual_Switch_ESP32S");
    if (WiFi.status() == WL_CONNECTED)
    {
        Serial.print(DEVICE_NAME);
        Serial.print(" is on Local IP: ");
        Serial.println(WiFi.localIP());
    }
    else
    {
        Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status()));
    }

    //Setup home page access content when visite
    home_page_message = "<!DOCTYPE html><html><head><title>" + DEVICE_NAME 
        + "</title></head><body><p><h2> Hi! This is " + DEVICE_NAME + "</h2>"
        + "To update firmware, <a href='/update'>Click here!!</a><br/><span>Username & Password required!</span></p><body></html>";

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

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

    //MQTT starts here!
    mqtt_client.setServer(mqtt_server, mqtt_port);
    mqtt_client.setCallback(callback);
}

/**
//When MQTT lost connection, this function will be called to reconnect MQTT//
 * Modified to accept multiple MQTT topics 
 * https://www.baldengineer.com/multiple-mqtt-topics-pubsubclient.html
 */
void callback(char *topic, byte *payload, unsigned int length)
{
    //Print message received
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (int i = 0; i < length; i++)
    {
        Serial.print((char)payload[i]);
    }
    Serial.println();

    //Process multiple topics here//
    payload[length] = '\0';
    String message = (char*)payload;
    
    if (strcmp(topic, "studio/humd_switch") == 0)
    {
      if(message == "true"){
        digitalWrite(RELAY_PIN_1, HIGH);
        Serial.println("humd_switch true");
      }
      if(message == "false"){
        digitalWrite(RELAY_PIN_1, LOW);
        Serial.println("humd_switch false");
      }
    }
    if (strcmp(topic, "studio/humd_switch2") == 0)
    {
      if(message == "true"){
        digitalWrite(RELAY_PIN_2, HIGH);
        Serial.println("humd_switch2 true");
      }
      if(message == "false"){
        digitalWrite(RELAY_PIN_2, LOW);
        Serial.println("humd_switch2 false");
      }
    }
}

//============= MQTT ===================
//When MQTT lost connection, this function will be called to reconnect MQTT//
void reconnect(){
    //Loop while MQTT connected
    while (!mqtt_client.connected())
    {
        Serial.print("Attempting MQTT connection... ");
        // Create a random client ID
        String clientId = "Stonez_ESP32Client-";
        clientId += String(random(0xffff), HEX);
        // Attempt to connect
        if (mqtt_client.connect(clientId.c_str()))
        {
            Serial.print("connected to ");
            Serial.print(mqtt_server);
            // Once connected, publish an announcement...
            mqtt_client.publish("outTopic", "Hello world");
            // ... and resubscribe
            mqtt_client.subscribe("studio/humd_switch");
            mqtt_client.subscribe("studio/humd_switch2");
        }
        else
        {
            Serial.print("failed, rc=");
            Serial.print(mqtt_client.state());
            Serial.println(" try again in 5 seconds");
            // Wait 5 seconds before retrying
            delay(5000);
        }
    }
}
void loop() {
    //============= MQTT ===================
    //if can't connect to MQTT server, then re-connect
    if (!mqtt_client.connected())
        { reconnect(); }
    //constantly check MQTT to see for messages sending/receiving
    mqtt_client.loop();

    /**
     * Write your own code here....
     * 
     */
}

Async_AutoConnect_ESP32_minimal.ino

/****************************************************************************************************************************
  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>              //https://github.com/khoih-prog/ESPAsync_WiFiManager
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, "AutoConnectAP");
  //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("AutoConnectAP");
  if (WiFi.status() == WL_CONNECTED) { Serial.print(F("Connected. Local IP: ")); Serial.println(WiFi.localIP()); }
  else { Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status())); }
}

void loop() {  }

Async_AutoConnect_ESP8266_minimal.ino

/****************************************************************************************************************************
  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;

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, "AutoConnectAP");
  //ESPAsync_wifiManager.resetSettings();   //reset saved settings
  //ESPAsync_wifiManager.setAPStaticIPConfig(IPAddress(192,168,186,1), IPAddress(192,168,186,1), IPAddress(255,255,255,0));
  ESPAsync_wifiManager.autoConnect("AutoConnectAP");
  if (WiFi.status() == WL_CONNECTED) { Serial.print(F("Connected. Local IP: ")); Serial.println(WiFi.localIP()); }
  else { Serial.println(ESPAsync_wifiManager.getStatus(WiFi.status())); }
}

void loop() {  }


2022/07/09

一起來學樹莓派系列 - “如何使用 Log2RAM” 來增加SD卡的壽命, 及Pi運作的速度.

 大家好, 歡迎收看本週的weekend project with Stonez56. 

今天來和大家分享 “一起來學樹莓派” 系列 - “如何使用 Log2RAM” 來增加SD卡的壽命, 及Pi運作的速度. 


Log 主要是由系統上執行的一些程式在背景寫入到你的磁碟裡.  它可能是幾分鐘寫入一次, 或是幾秒鐘就寫入一次, 頻繁的讀寫 很容易造成SD卡的損毀並且影響運作的速度.


如果我們將 Log 直接寫入RAM, 就可以減少讀寫的次數, 提升Pi的運作速度, 及延長SD卡的壽命. 這些寫入到RAM的 Log 並不會消失, 它會每天再把這些 Log 寫回到SD卡. 所以你還是可以存取這些 Log的.  


使用 Log2RAM 也不是沒缺點, 如果你的 Pi 突然斷掉電源, 那麼你的 Log 就會完全消失了.

但仔細想想, 你上次查看 Log 是什麼時候呢? 如果不常使用 Log, 那麼這個 Log2RAM 就對你非常有幫助. 另外如果你的 Pi 的記憶體只有1GB或是512MB, 那可能就不合適來使用這個軟體的. 


半年前我曾經做過一個簡單的線上調查, 看看各位使用樹莓派時, 用什麼儲存裝置.
調查的結果有75%的朋友都是使用SD卡. 那我們就開始今天的練習吧!

Step: 

  1. 更新你的 Pi

    • >sudo apt update

    • >sudo apt full-upgrade

  2. 安裝 rsync (用來同步兩地的資料)

    • >sudo apt install rsync

  3. 取得 Log2RAM

    • >wget https://github.com/azlux/log2ram/archive/master.tar.gz -O log2ram.tar.gz

  4. 使用 tar 來解壓縮

    • >tar xf log2ram.tar.gz

    • >cd /home/pi/log2ram-master

    • >sudo ./install.sh

  5. 先檢查system log 的大小

    • >sudo du -sh /var/log 41M

    • 先用3倍大小 = 120M

  6. 重新開機

    • >sudo reboot

  7. 檢查 log2ram 是否正常運作

    • systemctl status log2ram

  8. 如果想要檢查 log 怎麼辦呢? 把 log 寫回到 /var/log

    • >log2ram write

  9. 如果想要暫時停止或重新開始 log2RAM 怎麼辦呢?

    • >log2ram stop

    • >log2ram start



以後任何程式有寫入Log 到  /var/log, 就會被寫入到RAM裡面 


如何設定 Log2RAM

  1. 使用 Nano 來編輯設定檔 log2ram.conf

    • >sudo nano /etc/log2ram.conf

    • 找到 SIZE=40M, 看你係統的設定如果你的系統有產生很多少個話可以把設成120MB

    • PATH_DISK=”/var/log” 如果你還有其他的程式會時時寫入到SD, 可以在PATH_DISK後面加入分號, 再加入其它的路徑, 如: PATH_DISK=”/var/log; /var/mylog”

    • 儲存後並退出

    • 重新啟動樹莓派


想了解有哪些Log會寫入SD卡, 請到 /var/hdd.log/ 去看看有那些檔案被寫入.


2022/07/03

App Inventor #5 無線搖控 ESP32-CAM APP (Part 2)

 大家好, 歡迎收看本週的Weekend project with Stonez56。 本周要進行的是 App Inventor 第5集 - “無線搖控 ESP32-CAM APP Part 2”! 這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。


因內容較多, 本影片分為上, 下兩集. 本集為 App Inventor程式設計. 

而上集為 UI 界面設計(https://youtu.be/x545mFSZWgg ),
如果你還沒有觀看的朋友, 請記得先去看一下, 再回來看這一集


再前一集是ESP32 程式的說明在這:
Arduino #35 使用 MQTT 無線搖控 ESP32-CAM 模組https://youtu.be/ECHBRdtV-yw 


好, 那我們就開始吧今天的練習吧~



2022/07/02

App Inventor #4 無線搖控 ESP32-CAM APP (Part 1)

 大家好, 歡迎收看本週的Weekend project with Stonez56。 本周要進行的是 App Inventor 第4集 - “無線搖控 ESP32-CAM APP Part 1”! 這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。


因內容較多, 本影片分為上, 下兩集. 上集為 UI 界面設計, 下集為 App Inventor程式設計. 


上次為大家示範了如何使用HTML + CSS + Javascript來寫網頁程式(Arduino #35  https://youtu.be/ECHBRdtV-yw), 利用網頁版的MQTT來遠端搖控ESP32 , 收到了不錯的回響.

今天我們使用 App Inventor 2來開發遠端搖控的 APP來取代網頁版本. 在ESP32程式的撰寫不需做任何的改變, 因為只要 MQTT Topic 一樣, APP inventor 就可以發佈訊息到ESP32了! 超方便的!


App Inventor 使用 WebView 想串流影像時, 會出現: header fields are too long for the server to interpret. 這問題會在下集時, 跟各位說明. 


另外, 針對 ESP32 C++上的程式說明, 請看 Arduino #35  https://youtu.be/ECHBRdtV-yw


好, 那我們就開始吧今天的練習吧~



2022/06/18

Android #35 使用 MQTT 無線搖控 ESP32-CAM 模組

 大家好, 歡迎收看本週的Weekend project with Stonez56。

本周要進行的是 Arduino 第35集,"使用 MQTT 無線搖控 ESP32-CAM 模組”。這個是一個專門為初學者來開發的一個系列, 所以會講的比較詳細, 相信各位只要仔細收看內容並進行實作, 一定會收獲滿滿滿。好, 那我們就開始吧今天的練習吧~


!!! ESP32-CAM streaming 的程式是由這裡取得:
https://github.com/wjsanek/wjsanek/blob/main/Working_ESP32_CameraWebServer_V2_with_OTA.zip

 這位高手把一大堆沒有用到的ESP32-CAM的程式移除, 所以我才有機會把程式寫成可以OTA.


在 VScode 裡, ESP32-CAM 的模組, 設定如下:

Board: ESP32 Wrover Module

  • Upload Speed: 115200

  • Flash Frequency: 80MHz

  • Flash Mode: QIO

  • Partition Scheme: Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)


本次學習的目標是能遠端遙控 ESP32-CAM, 所以會用到下列的程式

  • ESP32-CAM - Camera 伺服器 串流影像

  • Servo - 控制 SG90 伺服器擺動

  • ESPAsyncWifimanager - 隨時改變 Wi-Fi 設定

  • AsyncElegantOTA - FOTA - 隨時隨地的更新 firmware

  • MQTT - PubSubClient - 由ESP32-CAM 收送訊息

  • Paho MQTT Javascript client - 由瀏覽器 收送訊息 / HTML 編寫



本專案會用到下列的電子元件:

 

  • ESP32-CAM * 1

  • 天線 * 1

  • SG90 Servo 伺服器 * 2 - 上下 & 左右擺動各一個

  • 1000uf 電容一顆 * 1 - 過濾影像的水波紋

  • 杜邦線 * 4 - Power, GND, IO12, IO13




參考資料:


2022/05/22

ESP32-CAM module not stable, black screen, disconnect all the time? (模組 影像串流一直斷線不穩 - 先檢查電阻)

I have bought two ESP32-CAM modules and doing a weekend project. However, the video streaming from these two ESP-32CAM modules are not stable and just constantly disconnect and get black screens after few minutes or even few seconds. It's quite annoying.  After search and search, I found this article...

https://randomnerdtutorials.com/esp32-cam-connect-external-antenna/

If you have encountered similar issues on your ESP32-CAM modules, remember to check the location of the resistors.

Left: External antenna resistor location

Right: Internal antenna resistor location

Photo from the refereed website above. Thanks!


Thanks for the vendor who sold me these two ESP32-CAM modules that put on these external antenna resistors for me....but at least tell me in advance.   I was debug here and there for few days until I found out why! ha..  😅



===中文版====

我買了2 個ESP32-CAM 模組, 搞不定影像串流一直斷線不穩定….直到看到這一篇!  找了外接天線來裝上後, 穩定多了, 不再斷線黑畫面. 

如果你也遇到同樣的問題, 記得先檢查 ESP32-CAM 上的天線電阻位置. 

https://randomnerdtutorials.com/esp32-cam-connect-external-antenna/

左側圖: 外接天線電阻
右側圖: 內接天線電阻


但這老闆也太好心了, 給我的 2個模組都焊上了外接天線電阻...也沒有告訴我,  於是我一直 debug … debug … 再debug 就這樣花了好久時間~~ 😅



2022/05/09

App Inventor #2 “自己來寫mqtt APP Android & iOS都能用” (Create MQTT Apps with App Inventor 2)

本週我要進行教授的課程是 App Inventor 第二集。  “自己來寫mqtt APP Android & iOS都能用”.

這個影片適合初學者來觀看學習, 想拿來這個單元來學習 App Inventor 2 也是很 Ok 的! 

~~ 更新 2023-01-15~~~~~~~~~~~~~~~~~~~~~~~~~

有少數朋友來詢問, 這個範例要怎麼連線, 所以我把 ESP32 + DH11 的連線方式寫在此分享:



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

上次為大家介紹了 Android MQTT App - MQTTDash, 但是苦於沒有免費好用的 iOS MQTT Client, 那就用 App Inventor 2 來自己寫吧~  據 App Inventor 2 官網資料, 它可以同步開發 Android & iOS App! 但是我沒有使用 iOS 手機, 有成功的朋友再來和大家回報一下, 謝謝! 

 

觀看本集後, 那要如何在 ESP32 上如何自己寫程式呢?

請看: Arduino #32 用一塊ESP32來學 IOT: 

https://www.youtube.com/watch?v=x3k_dZXua1I 




本影片用到的程式碼:

https://stonez56.blogspot.com/2022/04/android-34-esp32iot-android-learn-iot.html 


影片中有錯字更正, 不然收不到溫溼度喔~~

Typo here...

13:32 影片中口誤把 RWD (Responsive Web Design) 說成 RDW 

33:49 Correction: stonez56/esp32s_temp  

34:05 Correction: stonez56/esp32s_humd 

35:27 Correction: stonez56/esp32s_temp  

46:49 Correction: stonez56/esp32s_humd


App Inventor 程式碼提供在此, 以方便偵錯使用!




參考資料:

MIT App Inventor: https://appinventor.mit.edu/ 

MQTT extension: https://ullisroboterseite.de/android-AI2-PahoMQTT-en.html 

MQTT extension download: https://ullisroboterseite.de/android-AI2-PahoDown.html 

Apple store download: https://apps.apple.com/tw/app/mit-app-inventor/id1422709355 


如果時間不夠的朋友, 也可以從以下的時間軸挑選想看的部份即可!


影片時間軸:

00:00 Start

01:01 先到 MIT App Inventor 網站

01:50 導入 MQTT extension (外掛)

04:26 上傳App 所需要用的圖片

05:23 設置 Screen1

07:07 啟用可滾動樣式 - 可更容易排列元件

08:43 如何佈局螢幕

12:47 設置燈泡圖片

14:04 RWD設計, 元件皆以百分比來設定!

17:20 在標籤上顯示溫度/濕度

21:52 設置 MQTT 連接按鈕

24:56 將圖片設計成按鈕; 稍候方可點選!

25:26 設置 MQTT Extension (外掛)

27:31 點擊“Blocks”開始寫程式

30:10 MQTT連線狀態變更的設定

35:53 發布主題並打開/關閉燈泡

43:23 處理接收所訂閱的 topics

47:34 為 Android 生成 .apk 執行檔

49:30 如何在Apple 手機運行程式? (未實作! 慎入)