first commit

This commit is contained in:
pangodream
2022-06-06 17:51:44 +02:00
commit dceefa2d2f
6 changed files with 156 additions and 0 deletions

13
keywords.txt Normal file
View File

@@ -0,0 +1,13 @@
#######################################
# Syntax Coloring Map For ESP2SOTA #
#######################################
#######################################
# Datatypes (KEYWORD1) #
#######################################
ESP2SOTA KEYWORD1
#######################################
# Methods and Functions (KEYWORD2) #
#######################################
begin KEYWORD2

21
library.json Normal file
View File

@@ -0,0 +1,21 @@
{
"name": "ESP2SOTA",
"keywords": "Self Sufficient, OTA, Update, ESP, ESP8266, ESP32, AP",
"description": "Async OTA (AP & Client WiFi modes) for ESP32/ESP8266 with no external dependencies (no jQuery needed)",
"repository":
{
"type": "git",
"url": "https://github.com/pangodream/ESP2SOTA.git"
},
"authors":
[
{
"name": "Alberto Iriberri",
"email": "alberto.iriberri@pangodream.es",
"maintainer": true
}
],
"version": "1.0.0",
"frameworks": "arduino",
"platforms": "espressif"
}

9
library.properties Normal file
View File

@@ -0,0 +1,9 @@
name=AsyncElegantOTA
version=2.2.4
author=Ayush Sharma
category=Communication
maintainer=Ayush Sharma <asrocks5@gmail.com>
sentence=Perform OTAs for ESP8266 & ESP32 Asynchronously.
paragraph=A User Interface Library which provides interactive elements for your Over the Air Updates on ESP8266/ESP32.
url=https://github.com/ayushsharma82/AsyncElegantOTA
architectures=esp8266,esp32

47
src/ESP2SOTA.cpp Normal file
View File

@@ -0,0 +1,47 @@
#include "ESP2SOTA.h"
#include "index_html.h"
//Class constructor
ESP2SOTAClass::ESP2SOTAClass(){
}
#if defined(ESP8266)
void ESP2SOTAClass::begin(ESP8266WebServer *server){
#elif defined(ESP32)
void ESP2SOTAClass::begin(WebServer *server){
#endif
_server = server;
//Returns index.html page
_server->on("/", HTTP_GET, [&]() {
_server->sendHeader("Connection", "close");
_server->send(200, "text/html", indexHtml);
});
/*handling uploading firmware file */
_server->on("/update", HTTP_POST, [&]() {
_server->sendHeader("Connection", "close");
_server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
ESP.restart();
}, [&]() {
HTTPUpload& upload = _server->upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.printf("Update: %s\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("Update Success: %u\nRebooting...\n", upload.totalSize);
} else {
Update.printError(Serial);
}
}
});
}
ESP2SOTAClass ESP2SOTA;

33
src/ESP2SOTA.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef ESP2SOTA_h
#define ESP2SOTA_h
#include "Arduino.h"
#include "stdlib_noniso.h"
#if defined(ESP8266)
#define HARDWARE "ESP8266"
#include "ESP8266WebServer.h"
#include "ESP8266HTTPUpdateServer.h"
#elif defined(ESP32)
#define HARDWARE "ESP32"
#include "WebServer.h"
#include "Update.h"
#endif
class ESP2SOTAClass{
public:
ESP2SOTAClass();
#if defined(ESP8266)
void begin(ESP8266WebServer *server);
#elif defined(ESP32)
void begin(WebServer *server);
#endif
private:
#if defined(ESP8266)
ESP8266WebServer *_server;
ESP8266HTTPUpdateServer _httpUpdater;
#endif
#if defined(ESP32)
WebServer *_server;
#endif
};
extern ESP2SOTAClass ESP2SOTA;
#endif

33
src/index_html.h Normal file
View File

@@ -0,0 +1,33 @@
const char* indexHtml =
"<body style='font-family: Verdana,sans-serif; font-size: 14px;'>"
"<div style='width:400px;padding:20px;border-radius:10px;border:solid 2px #e0e0e0;margin:auto;margin-top:20px;'>"
"<div style='width:100%;text-align:center;font-size:18px;font-weight:bold;margin-bottom:12px;'>ESP Self Sufficient OTA</div>"
"<form method='POST' action='#' enctype='multipart/form-data' id='upload-form' style='width:100%;margin-bottom:8px;'>"
"<input type='file' name='update'>"
"<input type='submit' value='Update' style='float:right;'>"
"</form>"
"<div style='width:100%;background-color:#e0e0e0;border-radius:8px;'>"
"<div id='prg' style='width:0%;background-color:#2196F3;padding:2px;border-radius:8px;color:white;text-align:center;'>0%</div>"
"</div>"
"</div>"
"</body>"
"<script>"
"var prg = document.getElementById('prg');"
"var form = document.getElementById('upload-form');"
"form.addEventListener('submit', e=>{"
"e.preventDefault();"
"var data = new FormData(form);"
"var req = new XMLHttpRequest();"
"req.open('POST', '/update');"
"req.upload.addEventListener('progress', p=>{"
"let w = Math.round((p.loaded / p.total)*100) + '%';"
"if(p.lengthComputable){"
"prg.innerHTML = w;"
"prg.style.width = w;"
"}"
"if(w == '100%') prg.style.backgroundColor = '#04AA6D';"
"});"
"req.send(data);"
"});"
"</script>";