AsyncElegantOTA.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef AsyncElegantOTA_h
  2. #define AsyncElegantOTA_h
  3. #include "Arduino.h"
  4. #include "stdlib_noniso.h"
  5. #if defined(ESP8266)
  6. #include "ESP8266WiFi.h"
  7. #include <ESPAsyncTCP.h>
  8. #elif defined(ESP32)
  9. #include "WiFi.h"
  10. #include <AsyncTCP.h>
  11. #include <Update.h>
  12. #include <esp_int_wdt.h>
  13. #include <esp_task_wdt.h>
  14. #endif
  15. #include <ESPAsyncWebServer.h>
  16. #include "elegantWebpage.h"
  17. size_t content_len;
  18. class AsyncElegantOtaClass{
  19. public:
  20. void begin(AsyncWebServer *server){
  21. _server = server;
  22. _server->on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){
  23. AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", ELEGANT_HTML, ELEGANT_HTML_SIZE);
  24. response->addHeader("Content-Encoding", "gzip");
  25. request->send(response);
  26. });
  27. _server->on("/update", HTTP_POST, [&](AsyncWebServerRequest *request) {
  28. // the request handler is triggered after the upload has finished...
  29. // create the response, add header, and send response
  30. AsyncWebServerResponse *response = request->beginResponse((Update.hasError())?500:200, "text/plain", (Update.hasError())?"FAIL":"OK");
  31. response->addHeader("Connection", "close");
  32. response->addHeader("Access-Control-Allow-Origin", "*");
  33. request->send(response);
  34. restartRequired = true;
  35. }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  36. //Upload handler chunks in data
  37. if (!index) {
  38. content_len = request->contentLength();
  39. #if defined(ESP8266)
  40. int cmd = (filename.indexOf("spiffs") > -1) ? U_FS : U_FLASH;
  41. Update.runAsync(true);
  42. if (!Update.begin(content_len, cmd)){ // Start with max available size
  43. #elif defined(ESP32)
  44. int cmd = (filename.indexOf("spiffs") > -1) ? U_SPIFFS : U_FLASH;
  45. if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) { // Start with max available size
  46. #endif
  47. Update.printError(Serial);
  48. }
  49. }
  50. // Write chunked data to the free sketch space
  51. if (Update.write(data, len) != len) {
  52. Update.printError(Serial);
  53. }
  54. if (final) { // if the final flag is set then this is the last frame of data
  55. if (Update.end(true)) { //true to set the size to the current progress
  56. }
  57. }
  58. });
  59. }
  60. void loop(){
  61. if(restartRequired){
  62. yield();
  63. delay(1000);
  64. yield();
  65. #if defined(ESP8266)
  66. ESP.restart();
  67. #elif defined(ESP32)
  68. esp_task_wdt_init(1,true);
  69. esp_task_wdt_add(NULL);
  70. while(true);
  71. #endif
  72. }
  73. }
  74. private:
  75. AsyncWebServer *_server;
  76. bool restartRequired = false;
  77. };
  78. AsyncElegantOtaClass AsyncElegantOTA;
  79. #endif