Browse Source

Added ESP32 Support ( Tested )

Ayush Sharma 4 years ago
parent
commit
9a4ab0b453
3 changed files with 145 additions and 7 deletions
  1. 40 0
      examples/ESP32_Demo/ESP32_Demo.ino
  2. 1 1
      library.properties
  3. 104 6
      src/ElegantOTA.h

+ 40 - 0
examples/ESP32_Demo/ESP32_Demo.ino

@@ -0,0 +1,40 @@
+#include <WiFi.h>
+#include <WiFiClient.h>
+#include <WebServer.h>
+#include <ElegantOTA.h>
+
+const char* ssid = "hotspotk2c";
+const char* password = "frenzy99";
+
+WebServer server(80);
+
+
+void setup(void) {
+  Serial.begin(115200);
+  WiFi.mode(WIFI_STA);
+  WiFi.begin(ssid, password);
+  Serial.println("");
+
+  // Wait for connection
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
+    Serial.print(".");
+  }
+  Serial.println("");
+  Serial.print("Connected to ");
+  Serial.println(ssid);
+  Serial.print("IP address: ");
+  Serial.println(WiFi.localIP());
+
+  server.on("/", []() {
+    server.send(200, "text/plain", "Hi! I am ESP32.");
+  });
+
+  ElegantOTA.begin(&server);    // Start ElegantOTA
+  server.begin();
+  Serial.println("HTTP server started");
+}
+
+void loop(void) {
+  server.handleClient();
+}

+ 1 - 1
library.properties

@@ -6,4 +6,4 @@ maintainer=Ayush Sharma <asrocks5@gmail.com>
 sentence=Perform OTAs for ESP8266 Elegantly!
 paragraph=A User Interface Library which provides interactive elements for your Over the Air Updates on ESP8266. UI has a size of only 50Kb!
 url=https://github.com/ayushsharma82/ElegantOTA
-architectures=esp8266
+architectures=esp8266,esp32

+ 104 - 6
src/ElegantOTA.h

@@ -11,6 +11,12 @@
     #include "WiFiClient.h"
     #include "ESP8266WebServer.h"
     #include "ESP8266HTTPUpdateServer.h"
+#elif defined(ESP32)
+    #define HARDWARE "ESP32"
+    #include "WiFi.h"
+    #include "WiFiClient.h"
+    #include "WebServer.h"
+    #include "Update.h"
 #endif
 
 
@@ -21,6 +27,7 @@ class ElegantOtaClass{
             _id = id;
         }
 
+        // ESP8266 Codebase
         #if defined(ESP8266)
         
             void begin(ESP8266WebServer *server, const char * username = "", const char * password = ""){
@@ -72,6 +79,8 @@ class ElegantOtaClass{
 				
                 }
             }
+        
+        // ESP32 Codebase
         #elif defined(ESP32)
 
             void begin(WebServer *server, const char * username = "", const char * password = ""){
@@ -94,14 +103,56 @@ class ElegantOtaClass{
                         if (!_server->authenticate(_username.c_str(), _password.c_str())) {
 						  return _server->requestAuthentication();
 						}
-
                         #if defined(ESP8266)
-                            _server->send(200, "application/json", "{\"id\": "+_id+", \"hardware\": \"ESP8266\"}");
+                            _server->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP8266\"}");
                         #elif defined(ESP32)
-                            _server->send(200, "application/json", "{\"id\": "+_id+", \"hardware\": \"ESP32\"}");
+                            _server->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP32\"}");
                         #endif
 					});
 
+                    _server->on("/update", HTTP_POST, [&](){
+                        // Check Authentication before processing request
+                        if (!_server->authenticate(_username.c_str(), _password.c_str())) {
+						  return;
+						}
+                        _server->sendHeader("Connection", "close");
+                        _server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
+                        ESP.restart();
+                    }, [&](){
+                        // Check Authentication before processing request
+                        if (!_server->authenticate(_username.c_str(), _password.c_str())) {
+						  return;
+						}
+                        // Perform upload
+                        HTTPUpload& upload = _server->upload();
+                        if (upload.status == UPLOAD_FILE_START) {
+                            Serial.setDebugOutput(true);
+                            Serial.printf("Update: %s\n", upload.filename.c_str());
+                            if (upload.name == "filesystem") {
+                                if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { //start with max available size
+                                    Update.printError(Serial);
+                                }
+                            } else {
+                                if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { //start with max available size
+                                    Update.printError(Serial);
+                                }
+                            }
+                        } else if (upload.status == UPLOAD_FILE_WRITE) {
+                            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);
+                            }
+                            Serial.setDebugOutput(false);
+                        } else {
+                            Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status);
+                        }
+                    });
+
 				} else {
 
 					_server->on("/update", HTTP_GET, [&](){
@@ -111,11 +162,45 @@ class ElegantOtaClass{
 
                     _server->on("/update/identity", HTTP_GET, [&](){
                         #if defined(ESP8266)
-                            _server->send(200, "application/json", "{\"id\": "+_id+", \"hardware\": \"ESP8266\"}");
+                            _server->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP8266\"}");
                         #elif defined(ESP32)
-                            _server->send(200, "application/json", "{\"id\": "+_id+", \"hardware\": \"ESP32\"}");
+                            _server->send(200, "application/json", "{\"id\": \""+_id+"\", \"hardware\": \"ESP32\"}");
                         #endif
 					});
+
+                    _server->on("/update", HTTP_POST, [&](){
+                        _server->sendHeader("Connection", "close");
+                        _server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
+                        ESP.restart();
+                    }, [&](){
+                        // Perform upload
+                        HTTPUpload& upload = _server->upload();
+                        if (upload.status == UPLOAD_FILE_START) {
+                            Serial.setDebugOutput(true);
+                            if (upload.name == "filesystem") {
+                                if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { //start with max available size
+                                    Update.printError(Serial);
+                                }
+                            } else {
+                                if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { //start with max available size
+                                    Update.printError(Serial);
+                                }
+                            }
+                        } else if (upload.status == UPLOAD_FILE_WRITE) {
+                            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);
+                            }
+                            Serial.setDebugOutput(false);
+                        } else {
+                            Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status);
+                        }
+                    });
 				
                 }
             }
@@ -129,9 +214,22 @@ class ElegantOtaClass{
         #if defined(ESP32)
             WebServer *_server;
         #endif
+
+        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 _username;
         String _password;
-        String _id = String(ESP.getChipId());
+        String _id = getID();
+        
 };
 
 ElegantOtaClass ElegantOTA;