Quellcode durchsuchen

Merge pull request #25 from ayushsharma82/v2

V2 of AsyncElegantOTA
Ayush Sharma vor 5 Jahren
Ursprung
Commit
d1ca342712
8 geänderte Dateien mit 99 neuen und 37 gelöschten Zeilen
  1. 0 2
      .github/FUNDING.yml
  2. 6 6
      README.md
  3. BIN
      docs/elegantOtaDemo.gif
  4. 2 2
      library.json
  5. 3 3
      library.properties
  6. 87 22
      src/AsyncElegantOTA.h
  7. 0 2
      src/elegantWebpage.h
  8. 1 0
      ui/README.md

+ 0 - 2
.github/FUNDING.yml

@@ -1,5 +1,3 @@
 # These are supported funding model platforms
 
-ko_fi: ayushsharma82
-patreon: asrocks5
 custom: [ 'https://www.buymeacoffee.com/6QGVpSj', 'https://www.paypal.me/ayushsharma82' ]

+ 6 - 6
README.md

@@ -1,4 +1,4 @@
-<p align="center"><img src="https://raw.githubusercontent.com/ayushsharma82/AsyncElegantOTA/master/docs/logo.svg?sanitize=true" width="400"></p>
+<p align="center"><img src="/docs/logo.svg?sanitize=true&raw=true" width="400"></p>
 
 <hr/>
 <p align="center">
@@ -8,12 +8,12 @@
 &nbsp;
 <img src="https://img.shields.io/github/license/ayushsharma82/AsyncElegantOTA.svg?style=for-the-badge" />
 &nbsp;
-<a href="https://www.patreon.com/asrocks5" target="_blank"><img src="https://img.shields.io/badge/patreon-donate-orange.svg?style=for-the-badge&logo=patreon" /></a>
+<a href="https://www.buymeacoffee.com/6QGVpSj" target="_blank"><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-%245-orange?style=for-the-badge&logo=buy-me-a-coffee" /></a>
 </p>
 <hr/>
 
 
-<p align="center">Push OTAs to ESP8266 / ESP32 Elegantly (Async)! </p>
+<p align="center">Perform OTAs for ESP8266 & ESP32 Asynchronously</p>
 <p align="center">
 AsyncElegantOTA provides a beautiful interface to upload Over the Air `.bin` updates to your ESP Modules with precise status and progress displayed over UI. This Library shows the current upload progress of your OTA and once finished, it will display the status of your OTA. This Version of Library uses AsyncWebServer. Thanks to @me-no-dev for a wonderful webserver library.
 </p>
@@ -22,7 +22,7 @@ AsyncElegantOTA provides a beautiful interface to upload Over the Air `.bin` upd
 <br>
 
 <h2 align="center">Preview</h2>
-<p align="center"><img src="https://raw.githubusercontent.com/ayushsharma82/AsyncElegantOTA/master/docs/elegantOtaDemo.gif"></p>
+<p align="center"><img src="/docs/elegantOtaDemo.gif?raw=true"></p>
 
 <br>
 <br>
@@ -158,9 +158,9 @@ void loop(void) {
 <h2>Contributions</h2>
 <p>Every Contribution to this repository is highly appriciated! Don't fear to create pull requests which enhance or fix the library as ultimatly you are going to help everybody.</p>
 <p>
-If you want to donate to the author then <b>you can become my patron</b>, It really helps me keep these libraries updated:
+If you want to donate to the author then <b>you can buy me a coffee</b>, It really helps me keep these libraries updated:
 <br/><br/>
-<a href="https://www.patreon.com/bePatron?u=16780597" target="_blank"><img src="https://img.shields.io/badge/patreon-donate-orange.svg?style=for-the-badge&logo=patreon" /></a>
+<a href="https://www.buymeacoffee.com/6QGVpSj" target="_blank"><img src="https://img.shields.io/badge/Buy%20me%20a%20coffee-%245-orange?style=for-the-badge&logo=buy-me-a-coffee" /></a>
 </p>
 <br/>
 <br/>

BIN
docs/elegantOtaDemo.gif


+ 2 - 2
library.json

@@ -1,7 +1,7 @@
 {
   "name": "AsyncElegantOTA",
   "keywords": "AsyncElegantOTA, OTA, Update, ESP8266, ESP32, Over, the, air",
-  "description": "Perform OTAs for ESP8266 Elegantly! Uses AsyncWebServer.",
+  "description": "Perform OTAs for ESP8266 & ESP32 Asynchronously.",
   "repository":
   {
     "type": "git",
@@ -15,7 +15,7 @@
       "maintainer": true
     }
   ],
-  "version": "1.0.6",
+  "version": "2.2.0",
   "frameworks": "arduino",
   "platforms": "espressif"
 }

+ 3 - 3
library.properties

@@ -1,9 +1,9 @@
 name=AsyncElegantOTA
-version=1.0.6
+version=2.2.0
 author=Ayush Sharma
 category=Communication
 maintainer=Ayush Sharma <asrocks5@gmail.com>
-sentence=Perform OTAs for ESP8266/ESP32 Elegantly! This Library uses AsyncWebServer.
-paragraph=A User Interface Library which provides interactive elements for your Over the Air Updates on ESP8266/ESP32. UI has a size of only 50Kb!
+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

+ 87 - 22
src/AsyncElegantOTA.h

@@ -6,35 +6,72 @@
 
 #if defined(ESP8266)
     #include "ESP8266WiFi.h"
-    #include <ESPAsyncTCP.h>
+    #include "ESPAsyncTCP.h"
 #elif defined(ESP32)
     #include "WiFi.h"
-    #include <AsyncTCP.h>
-    #include <Update.h>
-    #include <esp_int_wdt.h>
-    #include <esp_task_wdt.h>
+    #include "AsyncTCP.h"
+    #include "Update.h"
+    #include "esp_int_wdt.h"
+    #include "esp_task_wdt.h"
 #endif
 
-#include <ESPAsyncWebServer.h>
+#include "ESPAsyncWebServer.h"
+#include "FS.h"
 
 #include "elegantWebpage.h"
 
 
-size_t content_len;
-
 class AsyncElegantOtaClass{
+
     public:
 
-        void begin(AsyncWebServer *server){
+        void setID(const char* id){
+            _id = id;
+        }
+
+        void begin(AsyncWebServer *server, const char* username = "", const char* password = ""){
             _server = server;
 
+            if(strlen(username) > 0){
+                _authRequired = true;
+                _username = username;
+                _password = password;
+            }else{
+                _authRequired = false;
+                _username = "";
+                _password = "";
+            }
+
+            _server->on("/update/identity", HTTP_GET, [&](AsyncWebServerRequest *request){
+                if(_authRequired){
+                    if(!request->authenticate(_username.c_str(), _password.c_str())){
+                        return request->requestAuthentication();
+                    }
+                }
+                #if defined(ESP8266)
+                    request->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP8266\"}");
+                #elif defined(ESP32)
+                    request->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP32\"}");
+                #endif
+            });
+
             _server->on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){
+                if(_authRequired){
+                    if(!request->authenticate(_username.c_str(), _password.c_str())){
+                        return request->requestAuthentication();
+                    }
+                }
                 AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", ELEGANT_HTML, ELEGANT_HTML_SIZE);
                 response->addHeader("Content-Encoding", "gzip");
                 request->send(response);
             });
 
             _server->on("/update", HTTP_POST, [&](AsyncWebServerRequest *request) {
+                if(_authRequired){
+                    if(!request->authenticate(_username.c_str(), _password.c_str())){
+                        return request->requestAuthentication();
+                    }
+                }
                 // the request handler is triggered after the upload has finished... 
                 // create the response, add header, and send response
                 AsyncWebServerResponse *response = request->beginResponse((Update.hasError())?500:200, "text/plain", (Update.hasError())?"FAIL":"OK");
@@ -42,33 +79,44 @@ class AsyncElegantOtaClass{
                 response->addHeader("Access-Control-Allow-Origin", "*");
                 request->send(response);
                 restartRequired = true;
-            }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
+            }, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
                 //Upload handler chunks in data
+                if(_authRequired){
+                    if(!request->authenticate(_username.c_str(), _password.c_str())){
+                        return request->requestAuthentication();
+                    }
+                }
+
                 if (!index) {
-                
-                    content_len = request->contentLength();
                     #if defined(ESP8266)
-                        int cmd = (filename.indexOf("spiffs") > -1) ? U_FS : U_FLASH;
+                        int cmd = (filename == "filesystem") ? U_FS : U_FLASH;
                         Update.runAsync(true);
-                        if (!Update.begin(content_len, cmd)){ // Start with max available size
+                        size_t fsSize = ((size_t) &_FS_end - (size_t) &_FS_start);
+                        uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
+                        if (!Update.begin((cmd == U_FS)?fsSize:maxSketchSpace, cmd)){ // Start with max available size
                     #elif defined(ESP32)
-                        int cmd = (filename.indexOf("spiffs") > -1) ? U_SPIFFS : U_FLASH;
+                        int cmd = (filename == "filesystem") ? U_SPIFFS : U_FLASH;
                         if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) { // Start with max available size
                     #endif
-                            Update.printError(Serial);   
-                        }
-                
+                        Update.printError(Serial);
+                        return request->send(400, "text/plain", "OTA could not begin");
+                    }
                 }
 
                 // Write chunked data to the free sketch space
-                if (Update.write(data, len) != len) {
-                    Update.printError(Serial); 
+                if(len){
+                    if (Update.write(data, len) != len) {
+                        return request->send(400, "text/plain", "OTA could not begin");
+                    }
                 }
                     
                 if (final) { // if the final flag is set then this is the last frame of data
-                    if (Update.end(true)) { //true to set the size to the current progress
-
+                    if (!Update.end(true)) { //true to set the size to the current progress
+                        Update.printError(Serial);
+                        return request->send(400, "text/plain", "Could not end OTA");
                     }
+                }else{
+                    return;
                 }
             });
         }
@@ -81,6 +129,7 @@ class AsyncElegantOtaClass{
                 #if defined(ESP8266)
                     ESP.restart();
                 #elif defined(ESP32)
+                    // ESP32 will commit sucide
                     esp_task_wdt_init(1,true);
                     esp_task_wdt_add(NULL);
                     while(true);
@@ -90,6 +139,22 @@ class AsyncElegantOtaClass{
 
     private:
         AsyncWebServer *_server;
+
+        String getID(){
+            String id = "";
+            #if defined(ESP8266)
+                id = String(ESP.getChipId());
+            #elif defined(ESP32)
+                id = String((uint32_t)ESP.getEfuseMac(), HEX);
+            #endif
+            id.toUpperCase();
+            return id;
+        }
+
+        String _id = getID();
+        String _username = "";
+        String _password = "";
+        bool _authRequired = false;
         bool restartRequired = false;
 
 };

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 2
src/elegantWebpage.h


+ 1 - 0
ui/README.md

@@ -0,0 +1 @@
+Find source code of user interface in original ElegantOTA repository. [Click Here](https://github.com/ayushsharma82/ElegantOTA/tree/master/ui).

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.