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

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