2019-03-14 04:25:26 +00:00
|
|
|
// Arduino build process info: https://github.com/arduino/Arduino/wiki/Build-Process
|
|
|
|
|
2023-02-28 04:30:05 +00:00
|
|
|
#define WEBOTA_VERSION "0.1.6"
|
2019-03-19 16:24:46 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
#include "WebOTA.h"
|
2019-03-14 04:25:26 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <WiFiClient.h>
|
2019-03-16 16:35:25 +00:00
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
#include <WebServer.h>
|
2019-03-14 04:25:26 +00:00
|
|
|
#include <ESPmDNS.h>
|
|
|
|
#include <Update.h>
|
2019-03-16 16:35:25 +00:00
|
|
|
#include <WiFi.h>
|
|
|
|
|
|
|
|
WebServer OTAServer(9999);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266mDNS.h>
|
|
|
|
|
|
|
|
ESP8266WebServer OTAServer(9999);
|
|
|
|
#endif
|
2019-03-14 04:25:26 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
WebOTA webota;
|
2019-03-16 05:13:47 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2019-03-16 02:35:31 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
int WebOTA::init(const unsigned int port, const char *path) {
|
|
|
|
this->port = port;
|
|
|
|
this->path = path;
|
2019-03-14 04:25:26 +00:00
|
|
|
|
2019-03-19 16:29:12 +00:00
|
|
|
// Only run this once
|
|
|
|
if (this->init_has_run) {
|
2019-03-19 05:50:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_http_routes(&OTAServer, path);
|
|
|
|
OTAServer.begin(port);
|
|
|
|
|
|
|
|
Serial.printf("WebOTA url : http://%s.local:%d%s\r\n\r\n", this->mdns.c_str(), port, path);
|
|
|
|
|
|
|
|
// Store that init has already run
|
2019-03-19 16:29:12 +00:00
|
|
|
this->init_has_run = true;
|
2019-03-19 05:50:35 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// One param
|
|
|
|
int WebOTA::init(const unsigned int port) {
|
|
|
|
return WebOTA::init(port, "/webota");
|
|
|
|
}
|
|
|
|
|
|
|
|
// No params
|
|
|
|
int WebOTA::init() {
|
|
|
|
return WebOTA::init(8080, "/webota");
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebOTA::handle() {
|
2019-03-19 16:29:12 +00:00
|
|
|
// If we haven't run the init yet run it
|
|
|
|
if (!this->init_has_run) {
|
2019-03-19 05:50:35 +00:00
|
|
|
WebOTA::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
OTAServer.handleClient();
|
2019-03-19 18:31:16 +00:00
|
|
|
#ifdef ESP8266
|
2019-03-19 05:50:35 +00:00
|
|
|
MDNS.update();
|
2019-03-19 18:31:16 +00:00
|
|
|
#endif
|
2019-08-13 15:18:16 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-03-19 05:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long WebOTA::max_sketch_size() {
|
2019-03-16 16:34:35 +00:00
|
|
|
long ret = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-19 16:24:46 +00:00
|
|
|
// R Macro string literal https://en.cppreference.com/w/cpp/language/string_literal
|
2020-09-28 11:43:49 +00:00
|
|
|
const char ota_version_html[] PROGMEM = "<h1>WebOTA Version: " WEBOTA_VERSION "</h1>";
|
|
|
|
|
|
|
|
const char ota_upload_form[] PROGMEM = R"!^!(
|
2019-03-19 16:24:46 +00:00
|
|
|
|
|
|
|
<form method="POST" action="#" enctype="multipart/form-data" id="upload_form">
|
|
|
|
<input type="file" name="update" id="file">
|
|
|
|
<input type="submit" value="Update">
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<div id="prg_wrap" style="border: 0px solid; width: 100%;">
|
|
|
|
<div id="prg" style="text-shadow: 2px 2px 3px black; padding: 5px 0; display: none; border: 1px solid #008aff; background: #002180; text-align: center; color: white;"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
var domReady = function(callback) {
|
|
|
|
document.readyState === "interactive" || document.readyState === "complete" ? callback() : document.addEventListener("DOMContentLoaded", callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
domReady(function() {
|
|
|
|
var myform = document.getElementById('upload_form');
|
|
|
|
var filez = document.getElementById('file');
|
|
|
|
|
|
|
|
myform.onsubmit = function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var formData = new FormData();
|
|
|
|
var file = filez.files[0];
|
|
|
|
|
|
|
|
if (!file) { return false; }
|
|
|
|
|
|
|
|
formData.append("files", file, file.name);
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.upload.addEventListener("progress", function(evt) {
|
|
|
|
if (evt.lengthComputable) {
|
|
|
|
var per = Math.round((evt.loaded / evt.total) * 100);
|
|
|
|
var prg = document.getElementById('prg');
|
|
|
|
|
|
|
|
prg.innerHTML = per + "%"
|
|
|
|
prg.style.width = per + "%"
|
|
|
|
prg.style.display = "block"
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
xhr.open('POST', location.href, true);
|
|
|
|
|
|
|
|
// Set up a handler for when the request finishes.
|
|
|
|
xhr.onload = function () {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
//alert('Success');
|
|
|
|
} else {
|
|
|
|
//alert('An error occurred!');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>)!^!";
|
|
|
|
|
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
#ifdef ESP8266
|
|
|
|
int WebOTA::add_http_routes(ESP8266WebServer *server, const char *path) {
|
|
|
|
#endif
|
|
|
|
#ifdef ESP32
|
|
|
|
int WebOTA::add_http_routes(WebServer *server, const char *path) {
|
|
|
|
#endif
|
|
|
|
// Index page
|
|
|
|
server->on("/", HTTP_GET, [server]() {
|
|
|
|
server->send(200, "text/html", "<h1>WebOTA</h1>");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Upload firmware page
|
|
|
|
server->on(path, HTTP_GET, [server,this]() {
|
2020-09-28 11:43:49 +00:00
|
|
|
String html = "";
|
2020-09-28 16:00:16 +00:00
|
|
|
if (this->custom_html != NULL) {
|
2020-09-28 11:43:49 +00:00
|
|
|
html += this->custom_html;
|
2020-09-28 16:00:16 +00:00
|
|
|
} else {
|
2021-02-12 19:30:55 +00:00
|
|
|
html += FPSTR(ota_version_html);
|
2020-09-28 11:43:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 19:30:55 +00:00
|
|
|
html += FPSTR(ota_upload_form);
|
2020-09-28 11:43:49 +00:00
|
|
|
server->send_P(200, "text/html", html.c_str());
|
2019-03-19 05:50:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Handling uploading firmware file
|
|
|
|
server->on(path, HTTP_POST, [server,this]() {
|
|
|
|
server->send(200, "text/plain", (Update.hasError()) ? "Update: fail\n" : "Update: OK!\n");
|
|
|
|
delay(500);
|
|
|
|
ESP.restart();
|
|
|
|
}, [server,this]() {
|
|
|
|
HTTPUpload& upload = server->upload();
|
|
|
|
|
|
|
|
if (upload.status == UPLOAD_FILE_START) {
|
|
|
|
Serial.printf("Firmware update initiated: %s\r\n", upload.filename.c_str());
|
|
|
|
|
|
|
|
//uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
|
|
|
uint32_t maxSketchSpace = this->max_sketch_size();
|
|
|
|
|
|
|
|
if (!Update.begin(maxSketchSpace)) { //start with max available size
|
|
|
|
Update.printError(Serial);
|
|
|
|
}
|
|
|
|
} else if (upload.status == UPLOAD_FILE_WRITE) {
|
|
|
|
/* flashing firmware to ESP*/
|
|
|
|
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
|
|
|
Update.printError(Serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the next milestone to output
|
|
|
|
uint16_t chunk_size = 51200;
|
|
|
|
static uint32_t next = 51200;
|
|
|
|
|
|
|
|
// Check if we need to output a milestone (100k 200k 300k)
|
|
|
|
if (upload.totalSize >= next) {
|
|
|
|
Serial.printf("%dk ", next / 1024);
|
|
|
|
next += chunk_size;
|
|
|
|
}
|
|
|
|
} else if (upload.status == UPLOAD_FILE_END) {
|
|
|
|
if (Update.end(true)) { //true to set the size to the current progress
|
|
|
|
Serial.printf("\r\nFirmware update successful: %u bytes\r\nRebooting...\r\n", upload.totalSize);
|
|
|
|
} else {
|
|
|
|
Update.printError(Serial);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server->begin();
|
2019-08-13 15:18:16 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-03-19 05:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the MCU is in a delay() it cannot respond to HTTP OTA requests
|
|
|
|
// We do a "fake" looping delay and listen for incoming HTTP requests while waiting
|
2020-04-29 17:25:40 +00:00
|
|
|
void WebOTA::delay(unsigned int ms) {
|
2020-04-29 23:22:52 +00:00
|
|
|
// Borrowed from mshoe007 @ https://github.com/scottchiefbaker/ESP-WebOTA/issues/8
|
2020-07-23 14:06:01 +00:00
|
|
|
decltype(millis()) last = millis();
|
2019-03-14 04:25:26 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
while ((millis() - last) < ms) {
|
|
|
|
OTAServer.handleClient();
|
2019-03-19 16:17:44 +00:00
|
|
|
::delay(5);
|
2019-03-19 05:50:35 +00:00
|
|
|
}
|
2019-03-14 04:25:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 16:00:16 +00:00
|
|
|
void WebOTA::set_custom_html(char const * const html) {
|
2020-09-28 11:43:49 +00:00
|
|
|
this->custom_html = html;
|
|
|
|
}
|
2019-03-19 05:50:35 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-03-14 04:25:26 +00:00
|
|
|
int init_mdns(const char *host) {
|
2019-03-16 05:13:47 +00:00
|
|
|
// Use mdns for host name resolution
|
|
|
|
if (!MDNS.begin(host)) {
|
2019-03-14 04:25:26 +00:00
|
|
|
Serial.println("Error setting up MDNS responder!");
|
2019-03-16 05:13:47 +00:00
|
|
|
|
|
|
|
return 0;
|
2019-03-14 04:25:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 18:33:52 +00:00
|
|
|
Serial.printf("mDNS started : %s.local\r\n", host);
|
2019-03-16 05:13:47 +00:00
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
webota.mdns = host;
|
|
|
|
|
2019-03-16 05:13:47 +00:00
|
|
|
return 1;
|
2019-03-14 04:25:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-19 05:50:35 +00:00
|
|
|
String ip2string(IPAddress ip) {
|
|
|
|
String ret = String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-14 04:25:26 +00:00
|
|
|
int init_wifi(const char *ssid, const char *password, const char *mdns_hostname) {
|
2019-03-16 05:13:47 +00:00
|
|
|
WiFi.mode(WIFI_STA);
|
2019-03-14 04:25:26 +00:00
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
Serial.print("Connecting to Wifi");
|
|
|
|
|
|
|
|
// Wait for connection
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
}
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
Serial.printf("Connected to '%s'\r\n\r\n",ssid);
|
|
|
|
|
|
|
|
String ipaddr = ip2string(WiFi.localIP());
|
|
|
|
Serial.printf("IP address : %s\r\n", ipaddr.c_str());
|
|
|
|
Serial.printf("MAC address : %s \r\n", WiFi.macAddress().c_str());
|
|
|
|
|
|
|
|
init_mdns(mdns_hostname);
|
2019-08-13 15:18:16 +00:00
|
|
|
|
|
|
|
return 1;
|
2019-03-14 21:50:29 +00:00
|
|
|
}
|