Initial checkin of code

This commit is contained in:
Scott Baker 2019-03-13 21:25:26 -07:00
parent c4cddf4348
commit 617f5f1aaf
3 changed files with 170 additions and 0 deletions

11
library.properties Normal file
View File

@ -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 <scott.baker@gmail.com>
maintainer=Scott Baker <scott.baker@gmail.com>
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

153
src/WebOTA.cpp Normal file
View File

@ -0,0 +1,153 @@
// Arduino build process info: https://github.com/arduino/Arduino/wiki/Build-Process
const char *WEBOTA_VERSION = "0.1.0";
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ESPmDNS.h>
#include <Update.h>
#include <WebServer.h>
// Index page
const char* indexPage = "<h1>ESP32 Index</h1>";
// WebOTA Page
String get_ota_html() {
String ota_html = "";
ota_html += "<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js\"></script>\n";
ota_html += "\n";
ota_html += "<h1>WebOTA Version: " + (String) WEBOTA_VERSION + "</h1>\n";
ota_html += "\n";
ota_html += "<form method=\"POST\" action=\"#\" enctype=\"multipart/form-data\" id=\"upload_form\">\n";
ota_html += " <input type=\"file\" name=\"update\">\n";
ota_html += " <input type=\"submit\" value=\"Update\">\n";
ota_html += "</form>\n";
ota_html += "\n";
ota_html += "<div id=\"prg_wrap\" style=\"border: 0px solid; width: 100%;\">\n";
ota_html += " <div id=\"prg\" style=\"display: none; border: 1px solid #008aff; background: #002180; text-align: center; color: white;\"></div>\n";
ota_html += "</div>\n";
ota_html += "\n";
ota_html += "<script>\n";
ota_html += "$(\"form\").submit(function(e){\n";
ota_html += " e.preventDefault();\n";
ota_html += " var form = $(\"#upload_form\")[0];\n";
ota_html += " var data = new FormData(form);\n";
ota_html += "\n";
ota_html += " $.ajax({\n";
ota_html += " url: \"/update\",\n";
ota_html += " type: \"POST\",\n";
ota_html += " data: data,\n";
ota_html += " contentType: false,\n";
ota_html += " processData:false,\n";
ota_html += " xhr: function() {\n";
ota_html += " var xhr = new window.XMLHttpRequest();\n";
ota_html += " xhr.upload.addEventListener(\"progress\", function(evt) {\n";
ota_html += " if (evt.lengthComputable) {\n";
ota_html += " var per = Math.round((evt.loaded / evt.total) * 100);\n";
ota_html += " $(\"#prg\").html(per + \"%\").css(\"width\", per + \"%\").show();\n";
ota_html += " }\n";
ota_html += " }, false);\n";
ota_html += "\n";
ota_html += " return xhr;\n";
ota_html += " },\n";
ota_html += " success:function(d, s) {\n";
ota_html += " console.log(\"success!\")\n";
ota_html += " },\n";
ota_html += " error: function (a, b, c) {}\n";
ota_html += " });\n";
ota_html += "});\n";
ota_html += "</script>\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();
}

6
src/WebOTA.h Normal file
View File

@ -0,0 +1,6 @@
#include <WebServer.h>
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);