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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
  3. *
  4. * This file is part of Giess-o-mat.
  5. *
  6. * Giess-o-mat is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Giess-o-mat is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #if defined(ARDUINO_ARCH_ESP8266)
  20. #include <ESP8266HTTPUpdateServer.h>
  21. #elif defined(ARDUINO_ARCH_ESP32)
  22. #include <Update.h>
  23. #endif
  24. #include "SimpleUpdater.h"
  25. #if defined(ARDUINO_ARCH_ESP32)
  26. void SimpleUpdater::get(void) {
  27. String uploadPage = F(
  28. "<!DOCTYPE html>\n"
  29. "<html><head>\n"
  30. "<meta charset='utf-8'/>\n"
  31. "<meta name='viewport' content='width=device-width, initial-scale=1'/>\n"
  32. "<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>\n"
  33. "<title>SimpleUpdater ESP32</title>\n"
  34. "<style>\n"
  35. "@media (prefers-color-scheme: dark) {\n"
  36. "body {\n"
  37. "background-color: black;\n"
  38. "color: white;\n"
  39. "}\n"
  40. "}\n"
  41. "</style>\n"
  42. "</head><body>\n"
  43. "<h1>SimpleUpdater</h1>\n"
  44. "<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"
  45. "<form method='POST' action='#' enctype='multipart/form-data' id='upload_form'>\n"
  46. "<input type='file' name='update' accept='.bin'>\n"
  47. "<input type='submit' value='Update'>\n"
  48. "</form><br>\n"
  49. "<div id='prg'>progress: 0%</div>\n"
  50. "<p>After the update is finished, you will automatically be redirected to the main page.</p>\n"
  51. "<a href=\"/\">Back to Main Page</a>\n"
  52. "<script>\n"
  53. "$('form').submit(function(e){\n"
  54. "e.preventDefault();\n"
  55. "var form = $('#upload_form')[0];\n"
  56. "var data = new FormData(form);\n"
  57. " $.ajax({\n"
  58. "url: '/update',\n"
  59. "type: 'POST',\n"
  60. "data: data,\n"
  61. "contentType: false,\n"
  62. "processData:false,\n"
  63. "xhr: function() {\n"
  64. "var xhr = new window.XMLHttpRequest();\n"
  65. "xhr.upload.addEventListener('progress', function(evt) {\n"
  66. "if (evt.lengthComputable) {\n"
  67. "var per = evt.loaded / evt.total;\n"
  68. "$('#prg').html('progress: ' + Math.round(per*100) + '%');\n"
  69. "}\n"
  70. "}, false);\n"
  71. "return xhr;\n"
  72. "},\n"
  73. "success: function(d, s) {\n"
  74. "$('#prg').html('progress: success! redirecting...');\n"
  75. "setTimeout(function() {\n"
  76. "window.location.href = '/';\n"
  77. "}, 3000);\n"
  78. "},\n"
  79. "error: function(a, b, c) {\n"
  80. "$('#prg').html('progress: finished! redirecting...');\n"
  81. "setTimeout(function() {\n"
  82. "window.location.href = '/';\n"
  83. "}, 1000);\n"
  84. "}\n"
  85. "});\n"
  86. "});\n"
  87. "</script>\n"
  88. "</body></html>"
  89. );
  90. server->send(200, "text/html", uploadPage);
  91. }
  92. void SimpleUpdater::postResult(void) {
  93. server->sendHeader("Connection", "close");
  94. server->send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  95. ESP.restart();
  96. }
  97. void SimpleUpdater::postUpload(void) {
  98. HTTPUpload& upload = server->upload();
  99. if (upload.status == UPLOAD_FILE_START) {
  100. Serial.printf("Update: %s\n", upload.filename.c_str());
  101. // start with max available size
  102. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
  103. Update.printError(Serial);
  104. }
  105. } else if (upload.status == UPLOAD_FILE_WRITE) {
  106. // flashing firmware to ESP
  107. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  108. Update.printError(Serial);
  109. }
  110. } else if (upload.status == UPLOAD_FILE_END) {
  111. // true to set the size to the current progress
  112. if (Update.end(true)) {
  113. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  114. } else {
  115. Update.printError(Serial);
  116. }
  117. }
  118. }
  119. #endif
  120. #if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
  121. void SimpleUpdater::setup(UPDATE_WEB_SERVER *_server) {
  122. if (_server == NULL) {
  123. return;
  124. }
  125. server = _server;
  126. #if defined(ARDUINO_ARCH_ESP8266)
  127. updateServer.setup(server);
  128. #elif defined(ARDUINO_ARCH_ESP32)
  129. server->on(uri.c_str(), HTTP_POST, [this]() {
  130. postResult();
  131. }, [this]() {
  132. postUpload();
  133. });
  134. server->on(uri.c_str(), HTTP_GET, [this]() {
  135. get();
  136. });
  137. #endif
  138. }
  139. SimpleUpdater::SimpleUpdater(String _uri) {
  140. uri = _uri;
  141. server = NULL;
  142. }
  143. #endif