DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "</head><body>\n"
  29. "<h1>SimpleUpdater</h1>\n"
  30. "<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"
  31. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>\n"
  32. "<input type='file' name='update' accept='.bin'>\n"
  33. "<input type='submit' value='Update'>\n"
  34. "</form><br>\n"
  35. "<div id='prg'>progress: 0%</div>\n"
  36. "<p>After the update is finished, you will automatically be redirected to the main page.</p>\n"
  37. "<a href=\"/\">Back to Main Page</a>\n"
  38. "<script>\n"
  39. "$('form').submit(function(e){\n"
  40. "e.preventDefault();\n"
  41. "var form = $('#upload_form')[0];\n"
  42. "var data = new FormData(form);\n"
  43. " $.ajax({\n"
  44. "url: '/update',\n"
  45. "type: 'POST',\n"
  46. "data: data,\n"
  47. "contentType: false,\n"
  48. "processData:false,\n"
  49. "xhr: function() {\n"
  50. "var xhr = new window.XMLHttpRequest();\n"
  51. "xhr.upload.addEventListener('progress', function(evt) {\n"
  52. "if (evt.lengthComputable) {\n"
  53. "var per = evt.loaded / evt.total;\n"
  54. "$('#prg').html('progress: ' + Math.round(per*100) + '%');\n"
  55. "}\n"
  56. "}, false);\n"
  57. "return xhr;\n"
  58. "},\n"
  59. "success: function(d, s) {\n"
  60. "$('#prg').html('progress: success! redirecting...');\n"
  61. "setTimeout(function() {\n"
  62. "window.location.href = '/';\n"
  63. "}, 3000);\n"
  64. "},\n"
  65. "error: function(a, b, c) {\n"
  66. "$('#prg').html('progress: finished! redirecting...');\n"
  67. "setTimeout(function() {\n"
  68. "window.location.href = '/';\n"
  69. "}, 1000);\n"
  70. "}\n"
  71. "});\n"
  72. "});\n"
  73. "</script>\n"
  74. "</body></html>"
  75. );
  76. server->send(200, "text/html", uploadPage);
  77. }
  78. void SimpleUpdater::postResult(void) {
  79. server->sendHeader("Connection", "close");
  80. server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  81. ESP.restart();
  82. }
  83. void SimpleUpdater::postUpload(void) {
  84. HTTPUpload& upload = server->upload();
  85. if (upload.status == UPLOAD_FILE_START) {
  86. Serial.printf("Update: %s\n", upload.filename.c_str());
  87. // start with max available size
  88. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
  89. Update.printError(Serial);
  90. }
  91. } else if (upload.status == UPLOAD_FILE_WRITE) {
  92. // flashing firmware to ESP
  93. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  94. Update.printError(Serial);
  95. }
  96. } else if (upload.status == UPLOAD_FILE_END) {
  97. // true to set the size to the current progress
  98. if (Update.end(true)) {
  99. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  100. } else {
  101. Update.printError(Serial);
  102. }
  103. }
  104. }
  105. #endif
  106. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  107. void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
  108. if (_server == NULL) {
  109. return;
  110. }
  111. server = _server;
  112. #if defined(ARDUINO_ARCH_ESP8266)
  113. updateServer.setup(server);
  114. #elif defined(ARDUINO_ARCH_ESP32)
  115. server->on(uri.c_str(), HTTP_POST, [this]() {
  116. postResult();
  117. }, [this]() {
  118. postUpload();
  119. });
  120. server->on(uri.c_str(), HTTP_GET, [this]() {
  121. get();
  122. });
  123. #endif
  124. }
  125. SimpleUpdater::SimpleUpdater(String _uri) {
  126. uri = _uri;
  127. server = NULL;
  128. }
  129. #endif