From 617f5f1aaf794b976718e7701fe44cedc3e994fe Mon Sep 17 00:00:00 2001 From: Scott Baker Date: Wed, 13 Mar 2019 21:25:26 -0700 Subject: [PATCH] Initial checkin of code --- library.properties | 11 ++++ src/WebOTA.cpp | 153 +++++++++++++++++++++++++++++++++++++++++++++ src/WebOTA.h | 6 ++ 3 files changed, 170 insertions(+) create mode 100644 library.properties create mode 100644 src/WebOTA.cpp create mode 100644 src/WebOTA.h diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..48503e4 --- /dev/null +++ b/library.properties @@ -0,0 +1,11 @@ +# https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification#source-code +name=ESP32-OTA +version=0.1.0 +author=Scott Baker +maintainer=Scott Baker +sentence=Easily add a OTA via web to an existing project +paragraph=Easily add a OTA via web to an existing project +category=Communication +url=https://github.com/scottchiefbaker/ESP-WebOTA +architectures=esp32 +includes=WebOTA.h diff --git a/src/WebOTA.cpp b/src/WebOTA.cpp new file mode 100644 index 0000000..bdca57f --- /dev/null +++ b/src/WebOTA.cpp @@ -0,0 +1,153 @@ +// Arduino build process info: https://github.com/arduino/Arduino/wiki/Build-Process + +const char *WEBOTA_VERSION = "0.1.0"; + +#include +#include +#include +#include +#include +#include + +// Index page +const char* indexPage = "

ESP32 Index

"; + +// WebOTA Page +String get_ota_html() { + String ota_html = ""; + + ota_html += "\n"; + ota_html += "\n"; + ota_html += "

WebOTA Version: " + (String) WEBOTA_VERSION + "

\n"; + ota_html += "\n"; + ota_html += "
\n"; + ota_html += " \n"; + ota_html += " \n"; + ota_html += "
\n"; + ota_html += "\n"; + ota_html += "
\n"; + ota_html += "
\n"; + ota_html += "
\n"; + ota_html += "\n"; + ota_html += "\n"; + + return ota_html; +} + +String ip2string(IPAddress ip) { + String ret = String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]); + + return ret; +} + +int init_mdns(const char *host) { + /*use mdns for host name resolution*/ + if (!MDNS.begin(host)) { //http://esp32.local + Serial.println("Error setting up MDNS responder!"); + while (1) { + delay(1000); + } + } + + Serial.printf("mDNS started : %s\r\n", host); +} + +int init_wifi(const char *ssid, const char *password, const char *mdns_hostname) { + // Connect to WiFi network + 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()); + + //String macAddr = mac2string(WiFi.macAddress()); + Serial.printf("MAC address : %s \r\n", WiFi.macAddress().c_str()); + + init_mdns(mdns_hostname); + + Serial.printf("WebOTA url : http://%s.local:%d/webota\r\n\r\n", mdns_hostname, 8080); +} + +int init_web_ota(WebServer *server) { + // Login page + server->on("/", HTTP_GET, [server]() { + server->sendHeader("Connection", "close"); + server->send(200, "text/html", indexPage); + }); + + // Upload firmware page + server->on("/webota", HTTP_GET, [server]() { + server->sendHeader("Connection", "close"); + String ota_html = get_ota_html(); + server->send(200, "text/html", ota_html.c_str()); + }); + + // Handling uploading firmware file + server->on("/update", HTTP_POST, [server]() { + server->sendHeader("Connection", "close"); + server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK"); + ESP.restart(); + }, [server]() { + HTTPUpload& upload = server->upload(); + if (upload.status == UPLOAD_FILE_START) { + Serial.printf("Firmware update initiated: %s\r\n", upload.filename.c_str()); + if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //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); + } + } else if (upload.status == UPLOAD_FILE_END) { + if (Update.end(true)) { //true to set the size to the current progress + Serial.printf("Firmware update successful: %u bytes\r\nRebooting...\r\n", upload.totalSize); + } else { + Update.printError(Serial); + } + } + }); + + server->begin(); +} diff --git a/src/WebOTA.h b/src/WebOTA.h new file mode 100644 index 0000000..d0e425a --- /dev/null +++ b/src/WebOTA.h @@ -0,0 +1,6 @@ +#include + +int init_wifi(const char *ssid, const char *password, const char *mdns_hostname); +String ip2string(IPAddress ip); +int init_mdns(const char *host); +int init_web_ota(WebServer *server);