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