使用Arduino OTA 內建之ESP8266HTTPUpdateServer 與 ESP8266WebServer
http://dns.local/update 更新韌體
回應網頁 在
C:\Users\你的電腦名稱\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266HTTPUpdateServer\src下
ESP8266HTTPUpdateServer-impl.h
//OTA 樣板
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
const char* host = "dns";//mDNS
const char* ssid = "********";//填上wifi賬號
const char* password = "**********";//填上wifi密碼
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
void setup(void) {
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, password);
delay(100);
}
WiFi.softAP(String("IPGGx-"+ String((WiFi.localIP().toString()))),"");
//啟動mdns服務
MDNS.begin(host);
//配置webserver為更新server
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
/*你的程式碼 */
}
void loop(void) {
/*你的程式碼*/
httpServer.handleClient();
MDNS.update();
}
//使用方法如下:
//自動嘗試聯網 若連不上
//WiFi搜尋 espAP 登入 (若搜尋到IPGGx-192.168.xxx.xxx 表示已連上你的區網)
//再以瀏覽器開啟 192.168.4.1 設定SSID與PASS
//顯示註冊 SSID, PASS確認網頁後 reset ESP
//若WiFi搜尋到 IPGGx-192.168.xxx.xxx 表示已連上你的區網
//連上區網後 在同一區網 就可使用OTA將.bin檔上傳至ESP
// 使用 espUpdate.local/update 即可開啟上傳網頁 上傳firmware
// 若找不到網頁 再使用WiFi搜尋到的IP
//192.168.xxx.xxx/update 即可開啟上傳網頁 上傳firmware
.bin 可由Arduino IDE 草稿碼->匯出已編譯的二進位檔 產生
再由 草稿碼->顯示草稿碼資料夾 取得
//你的程式 以上述樣板程式再加入你的程式(紅色)--預設LED閃爍
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#define led d4 //d1 mini 內建LED
const char* host = "espUpdate";
const char* ssid = "********";//填上wifi賬號
const char* password = "*********";//填上wifi密碼
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
void setup(void) {
pinMode(led,OUTPUT);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
WiFi.begin(ssid, password);
delay(100);
/*此處可放你的程式碼*/
}
WiFi.softAP(String("IPGGx-"+String((WiFi.localIP().toString()))),"");
//啟動mdns服務
MDNS.begin(host);
//配置webserver為更新server
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
}
void loop(void) {
/*你的程式碼 開始*/
digitalWrite(D4,0);
delay(2000);//增長時間 以便區別
digitalWrite(D4,1);
delay(1000);
/*你的程式碼 結束*/
httpServer.handleClient();
MDNS.update();
}
OTA回應網頁 在
C:\Users\你的電腦名稱\ AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.0.2\libraries\ESP8266HTTPUpdateServer\src下
ESP8266HTTPUpdateServer-impl.h
可將該檔中有關回應網頁部分依你的需求修改如下例:
static const char serverIndex[] PROGMEM =
R"(<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'/>
<style>
body {text-align: center;}
input[type=file] {background-color: #50FF50;font-size: 24px;}
input[type=text]{font-size: 24px;max-width: 200px;}
input[type=submit]{font-size: 24px;max-width: 200px;}
p {font-size: 22px;}
</style>
</head>
<body>
<form method='POST' action='' enctype='multipart/form-data'>
Firmware:<br>
<input type='file' accept='.bin,.bin.gz' name='firmware'>
<input type='submit' value='Update Firmware'>
</form>
<form method='POST' action='' enctype='multipart/form-data'>
FileSystem:<br>
<input type='file' accept='.bin,.bin.gz' name='filesystem'>
<input type='submit' value='Update FileSystem'>
</form>
</body>
</html>)";
static const char successResponse[] PROGMEM =
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...";