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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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) || defined(ARDUINO_ARCH_ESP32)
  14. #if defined(ARDUINO_ARCH_ESP8266)
  15. #include <ESP8266HTTPUpdateServer.h>
  16. #elif defined(ARDUINO_ARCH_ESP32)
  17. #include <Update.h>
  18. #endif
  19. #include "SimpleUpdater.h"
  20. #if defined(ARDUINO_ARCH_ESP32)
  21. void SimpleUpdater::get(void) {
  22. String uploadPage = F(
  23. "<html><head>"
  24. "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>"
  25. "<title>SimpleUpdater ESP32</title>"
  26. "</head><body>"
  27. "<h1>SimpleUpdater</h1>"
  28. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>"
  29. "<input type='file' name='update'>"
  30. "<input type='submit' value='Update'>"
  31. "</form>"
  32. "<div id='prg'>progress: 0%</div>"
  33. "<a href=\"/\">Back to Main Page</a>"
  34. "<script>"
  35. "$('form').submit(function(e){"
  36. "e.preventDefault();"
  37. "var form = $('#upload_form')[0];"
  38. "var data = new FormData(form);"
  39. " $.ajax({"
  40. "url: '/update',"
  41. "type: 'POST',"
  42. "data: data,"
  43. "contentType: false,"
  44. "processData:false,"
  45. "xhr: function() {"
  46. "var xhr = new window.XMLHttpRequest();"
  47. "xhr.upload.addEventListener('progress', function(evt) {"
  48. "if (evt.lengthComputable) {"
  49. "var per = evt.loaded / evt.total;"
  50. "$('#prg').html('progress: ' + Math.round(per*100) + '%');"
  51. "}"
  52. "}, false);"
  53. "return xhr;"
  54. "},"
  55. "success:function(d, s) {"
  56. "console.log('success!')"
  57. "},"
  58. "error: function (a, b, c) {"
  59. "}"
  60. "});"
  61. "});"
  62. "</script>"
  63. "</body></html>"
  64. );
  65. server->send(200, "text/html", uploadPage);
  66. }
  67. void SimpleUpdater::postResult(void) {
  68. server->sendHeader("Connection", "close");
  69. server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  70. ESP.restart();
  71. }
  72. void SimpleUpdater::postUpload(void) {
  73. HTTPUpload& upload = server->upload();
  74. if (upload.status == UPLOAD_FILE_START) {
  75. Serial.printf("Update: %s\n", upload.filename.c_str());
  76. // start with max available size
  77. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
  78. Update.printError(Serial);
  79. }
  80. } else if (upload.status == UPLOAD_FILE_WRITE) {
  81. // flashing firmware to ESP
  82. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  83. Update.printError(Serial);
  84. }
  85. } else if (upload.status == UPLOAD_FILE_END) {
  86. // true to set the size to the current progress
  87. if (Update.end(true)) {
  88. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  89. } else {
  90. Update.printError(Serial);
  91. }
  92. }
  93. }
  94. #endif
  95. void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
  96. if (_server == NULL) {
  97. return;
  98. }
  99. server = _server;
  100. #if defined(ARDUINO_ARCH_ESP8266)
  101. updateServer.setup(server);
  102. #elif defined(ARDUINO_ARCH_ESP32)
  103. server->on(uri.c_str(), HTTP_POST, [this]() {
  104. postResult();
  105. }, [this]() {
  106. postUpload();
  107. });
  108. server->on(uri.c_str(), HTTP_GET, [this]() {
  109. get();
  110. });
  111. #endif
  112. }
  113. SimpleUpdater::SimpleUpdater(String _uri) {
  114. uri = _uri;
  115. server = NULL;
  116. }
  117. #endif