AsyncElegantOTA.h 3.6 KB

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