Ayush Sharma 6 роки тому
батько
коміт
605323ea25

+ 2 - 2
examples/ESP32_Async_Demo/ESP32_Async_Demo.ino

@@ -28,7 +28,7 @@ void setup(void) {
   Serial.println(WiFi.localIP());
 
   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
-    request->send(200, "text/plain", "Hi! I am ESP8266.");
+    request->send(200, "text/plain", "Hi! I am ESP32.");
   });
 
   AsyncElegantOTA.begin(server);    // Start ElegantOTA
@@ -37,5 +37,5 @@ void setup(void) {
 }
 
 void loop(void) {
-
+  AsyncElegantOTA.loop();
 }

+ 1 - 1
examples/ESP8266_Async_Demo/ESP8266_Async_Demo.ino

@@ -37,5 +37,5 @@ void setup(void) {
 }
 
 void loop(void) {
-
+  AsyncElegantOTA.loop();
 }

+ 3 - 2
keywords.txt

@@ -1,2 +1,3 @@
-ElegantOTA	KEYWORD1
-begin	KEYWORD2
+AsyncElegantOTA	KEYWORD1
+begin	KEYWORD2
+loop	KEYWORD2

+ 50 - 42
src/AsyncElegantOTA.h

@@ -5,12 +5,10 @@
 #include "stdlib_noniso.h"
 
 #if defined(ESP8266)
-    #define HARDWARE "ESP8266"
     #include "ESP8266WiFi.h"
     #include <Hash.h>
     #include <ESPAsyncTCP.h>
 #elif defined(ESP32)
-    #define HARDWARE "ESP32"
     #include "WiFi.h"
     #include <Hash.h>
     #include <AsyncTCP.h>
@@ -24,54 +22,64 @@
 class AsyncElegantOtaClass{
     public:
 
-            void begin(AsyncWebServer &server){
-                server.on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){
-                    AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", ELEGANT_HTML, ELEGANT_HTML_SIZE);
-                    response->addHeader("Content-Encoding", "gzip");
-                    request->send(response);
-                });
+        void begin(AsyncWebServer &server){
+            server.on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){
+                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) {
-                    // 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");
-                    response->addHeader("Connection", "close");
-                    response->addHeader("Access-Control-Allow-Origin", "*");
-                    request->send(response);
-                    ESP.restart();
-                }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
-                    //Upload handler chunks in data
-                    if (!index) {
+            server.on("/update", HTTP_POST, [&](AsyncWebServerRequest *request) {
+                // 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");
+                response->addHeader("Connection", "close");
+                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) {
+                //Upload handler chunks in data
+                if (!index) {
+                
+                    #if defined(ESP8266)
+                        uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;      
+                        if (!Update.begin(maxSketchSpace)){ // Start with max available size
+                    #endif
                     
-                        #if defined(ESP8266)
-                            uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;      
-                            if (!Update.begin(maxSketchSpace)){ // Start with max available size
-                        #endif
-                        
-                        #if defined(ESP32)
-                            if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { // Start with max available size
-                        #endif
-                                Update.printError(Serial);   
-                            }
+                    #if defined(ESP32)
+                        if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { // Start with max available size
+                    #endif
+                            Update.printError(Serial);   
+                        }
+
+                    #if defined(ESP8266)
+                        Update.runAsync(true); // Tell the updaterClass to run in async mode
+                    #endif
+                
+                }
 
-                        #if defined(ESP8266)
-                            Update.runAsync(true); // Tell the updaterClass to run in async mode
-                        #endif
+                // Write chunked data to the free sketch space
+                if (Update.write(data, len) != len) {
+                    Update.printError(Serial); 
+                }
                     
-                    }
+                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
 
-                    // Write chunked data to the free sketch space
-                    if (Update.write(data, len) != len) {
-                        Update.printError(Serial); 
                     }
-                        
-                    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
+                }
+            });
+        }
 
-                        }
-                    }
-                });
+        void loop(){
+            if(restartRequired){
+                ESP.restart();
             }
+        }
+
+    private:
+        bool restartRequired = false;
+
 };
 
 AsyncElegantOtaClass AsyncElegantOTA;