diff --git a/examples/Callback_Demo/Callback_Demo.ino b/examples/Callback_Demo/Callback_Demo.ino new file mode 100644 index 0000000..3f9c0dd --- /dev/null +++ b/examples/Callback_Demo/Callback_Demo.ino @@ -0,0 +1,86 @@ + /* + ElegantOTA callback Demo Example - This example will work for both ESP8266 & ESP32 microcontrollers. + ----- + Author: Ayush Sharma ( https://github.com/ayushsharma82 ) + + Important Notice: Star the repository on Github if you like the library! :) + Repository Link: https://github.com/ayushsharma82/ElegantOTA +*/ + +#if defined(ESP8266) + #include + #include + #include +#elif defined(ESP32) + #include + #include + #include +#endif + +#include + +const char* ssid = "........"; +const char* password = "........"; + +#if defined(ESP8266) + ESP8266WebServer server(80); +#elif defined(ESP32) + WebServer server(80); +#endif + +int iCallBackCount = 0; +unsigned long lStartTime ; + +void MyAction_onOTAStart() { + iCallBackCount = 0; + Serial.printf("OTA update started\n\r"); + lStartTime = millis(); +} + +void MyAction_onOTAProgress() { + iCallBackCount = iCallBackCount + 1; + Serial.printf("OTA progress, %5.3fs elapsed\n\r", (float)(millis()-lStartTime)/1000.0); +} + +void MyAction_onOTAEnd() { + Serial.printf("OTA update ended, %5.3fs elapsed\n\r", (float)(millis()-lStartTime)/1000.0); + iCallBackCount = 0 ; +} + +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! This is a sample response."); + }); + + ElegantOTA.begin(&server); // Start ElegantOTA + server.begin(); + Serial.println("HTTP server started"); + +// Add the AsyncElegantOTA callbacks +// Not all are required you can add each callback individually +// +// Watch the output on the serial monitor during OTA update. + ElegantOTA.onOTAStart(MyAction_onOTAStart); + ElegantOTA.onOTAProgress(MyAction_onOTAProgress); + ElegantOTA.onOTAEnd(MyAction_onOTAEnd); +} + +void loop(void) { + server.handleClient(); +} diff --git a/keywords.txt b/keywords.txt index 7332f54..1abe284 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,3 +1,6 @@ ElegantOTA KEYWORD1 -begin KEYWORD2 -setID KEYWORD2 \ No newline at end of file +begin KEYWORD2 +setID KEYWORD2 +onOTAStart KEYWORD2 +onOTAEnd KEYWORD2 +onOTAProgress KEYWORD2 diff --git a/src/ElegantOTA.cpp b/src/ElegantOTA.cpp index d60ca32..6a5f303 100644 --- a/src/ElegantOTA.cpp +++ b/src/ElegantOTA.cpp @@ -72,8 +72,10 @@ void ElegantOtaClass::setID(const char* id){ HTTPUpload& upload = _server->upload(); if (upload.status == UPLOAD_FILE_START) { - Serial.setDebugOutput(true); - Serial.printf("Update Received: %s\n", upload.filename.c_str()); +// Serial output must be active to see the callback serial prints +// Serial.setDebugOutput(true); +// Serial.printf("Update Received: %s\n", upload.filename.c_str()); + if (_preUpdateRequired) preUpdateCallback(); if (upload.name == "filesystem") { if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { //start with max available size Update.printError(Serial); @@ -87,18 +89,36 @@ void ElegantOtaClass::setID(const char* id){ if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) { Update.printError(Serial); } + if (_progressUpdateRequired) progressUpdateCallback(); } 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); +// Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); } else { Update.printError(Serial); } - Serial.setDebugOutput(false); + if (_postUpdateRequired) postUpdateCallback(); +// Serial.setDebugOutput(false); } else { - Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); +// Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status); } }); #endif } +void ElegantOtaClass::onOTAStart(void callable(void)){ + preUpdateCallback = callable; + _preUpdateRequired = true ; +} + +void ElegantOtaClass::onOTAProgress(void callable(void)){ + progressUpdateCallback= callable; + _progressUpdateRequired = true ; +} + +void ElegantOtaClass::onOTAEnd(void callable(void)){ + postUpdateCallback = callable; + _postUpdateRequired = true ; +} + + ElegantOtaClass ElegantOTA; diff --git a/src/ElegantOTA.h b/src/ElegantOTA.h index c61ef6a..91773e3 100644 --- a/src/ElegantOTA.h +++ b/src/ElegantOTA.h @@ -25,7 +25,9 @@ class ElegantOtaClass{ ElegantOtaClass(); void setID(const char* id); - + void onOTAStart(void callable(void)); + void onOTAProgress(void callable(void)); + void onOTAEnd(void callable(void)); #if defined(ESP8266) void begin(ESP8266WebServer *server, const char * username = "", const char * password = ""); #elif defined(ESP32) @@ -45,6 +47,12 @@ class ElegantOtaClass{ char _password[64]; char _id[128]; bool authenticate; + bool _preUpdateRequired = false; + bool _progressUpdateRequired = false; + bool _postUpdateRequired = false; + void (*preUpdateCallback)(); + void (*progressUpdateCallback)(); + void (*postUpdateCallback)(); }; extern ElegantOtaClass ElegantOTA;