ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleUpdater.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SimpleUpdater.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #if defined(ARDUINO_ARCH_ESP8266)
  14. #include <ESP8266HTTPUpdateServer.h>
  15. #elif defined(ARDUINO_ARCH_ESP32)
  16. #include <Update.h>
  17. #endif
  18. #include "SimpleUpdater.h"
  19. #if defined(ARDUINO_ARCH_ESP32)
  20. void SimpleUpdater::get(void) {
  21. String uploadPage = F(
  22. "<!DOCTYPE html>\n"
  23. "<html><head>\n"
  24. "<meta charset='utf-8'/>\n"
  25. "<meta name='viewport' content='width=device-width, initial-scale=1'/>\n"
  26. "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>\n"
  27. "<title>SimpleUpdater ESP32</title>\n"
  28. "<style>\n"
  29. "@media (prefers-color-scheme: dark) {\n"
  30. "body {\n"
  31. "background-color: black;\n"
  32. "color: white;\n"
  33. "}\n"
  34. "}\n"
  35. "</style>\n"
  36. "</head><body>\n"
  37. "<h1>SimpleUpdater</h1>\n"
  38. "<p>Select the update file. If you have built this project with PlatformIO, you can find a firmware.bin in the .pio folder.</p>\n"
  39. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>\n"
  40. "<input type='file' name='update' accept='.bin'>\n"
  41. "<input type='submit' value='Update'>\n"
  42. "</form><br>\n"
  43. "<div id='prg'>progress: 0%</div>\n"
  44. "<p>After the update is finished, you will automatically be redirected to the main page.</p>\n"
  45. "<a href=\"/\">Back to Main Page</a>\n"
  46. "<script>\n"
  47. "$('form').submit(function(e){\n"
  48. "e.preventDefault();\n"
  49. "var form = $('#upload_form')[0];\n"
  50. "var data = new FormData(form);\n"
  51. " $.ajax({\n"
  52. "url: '/update',\n"
  53. "type: 'POST',\n"
  54. "data: data,\n"
  55. "contentType: false,\n"
  56. "processData:false,\n"
  57. "xhr: function() {\n"
  58. "var xhr = new window.XMLHttpRequest();\n"
  59. "xhr.upload.addEventListener('progress', function(evt) {\n"
  60. "if (evt.lengthComputable) {\n"
  61. "var per = evt.loaded / evt.total;\n"
  62. "$('#prg').html('progress: ' + Math.round(per*100) + '%');\n"
  63. "}\n"
  64. "}, false);\n"
  65. "return xhr;\n"
  66. "},\n"
  67. "success: function(d, s) {\n"
  68. "$('#prg').html('progress: success! redirecting...');\n"
  69. "setTimeout(function() {\n"
  70. "window.location.href = '/';\n"
  71. "}, 3000);\n"
  72. "},\n"
  73. "error: function(a, b, c) {\n"
  74. "$('#prg').html('progress: finished! redirecting...');\n"
  75. "setTimeout(function() {\n"
  76. "window.location.href = '/';\n"
  77. "}, 1000);\n"
  78. "}\n"
  79. "});\n"
  80. "});\n"
  81. "</script>\n"
  82. "</body></html>"
  83. );
  84. server->send(200, "text/html", uploadPage);
  85. }
  86. void SimpleUpdater::postResult(void) {
  87. server->sendHeader("Connection", "close");
  88. server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  89. ESP.restart();
  90. }
  91. void SimpleUpdater::postUpload(void) {
  92. HTTPUpload& upload = server->upload();
  93. if (upload.status == UPLOAD_FILE_START) {
  94. Serial.printf("Update: %s\n", upload.filename.c_str());
  95. // start with max available size
  96. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
  97. Update.printError(Serial);
  98. }
  99. } else if (upload.status == UPLOAD_FILE_WRITE) {
  100. // flashing firmware to ESP
  101. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  102. Update.printError(Serial);
  103. }
  104. } else if (upload.status == UPLOAD_FILE_END) {
  105. // true to set the size to the current progress
  106. if (Update.end(true)) {
  107. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  108. } else {
  109. Update.printError(Serial);
  110. }
  111. }
  112. }
  113. #endif
  114. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  115. void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
  116. if (_server == NULL) {
  117. return;
  118. }
  119. server = _server;
  120. #if defined(ARDUINO_ARCH_ESP8266)
  121. updateServer.setup(server);
  122. #elif defined(ARDUINO_ARCH_ESP32)
  123. server->on(uri.c_str(), HTTP_POST, [this]() {
  124. postResult();
  125. }, [this]() {
  126. postUpload();
  127. });
  128. server->on(uri.c_str(), HTTP_GET, [this]() {
  129. get();
  130. });
  131. #endif
  132. }
  133. SimpleUpdater::SimpleUpdater(String _uri) {
  134. uri = _uri;
  135. server = NULL;
  136. }
  137. #endif