Skip to content

Commit

Permalink
Merge pull request #91 from PeterAckermans/With-CALLBACK-options
Browse files Browse the repository at this point in the history
Duplicated ASYNC changes
  • Loading branch information
ayushsharma82 committed Apr 23, 2023
2 parents c231246 + 4adde76 commit 52717e9
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
86 changes: 86 additions & 0 deletions examples/Callback_Demo/Callback_Demo.ino
Original file line number Diff line number Diff line change
@@ -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 <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#endif

#include <ElegantOTA.h>

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();
}
7 changes: 5 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ElegantOTA KEYWORD1
begin KEYWORD2
setID KEYWORD2
begin KEYWORD2
setID KEYWORD2
onOTAStart KEYWORD2
onOTAEnd KEYWORD2
onOTAProgress KEYWORD2
30 changes: 25 additions & 5 deletions src/ElegantOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
10 changes: 9 additions & 1 deletion src/ElegantOTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 52717e9

Please sign in to comment.