WebUpdate.ino 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. WebUpdate.ino, Example for the AutoConnect library.
  3. Copyright (c) 2018, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This example is an implementation of a lightweight update feature
  6. that updates the ESP8266's firmware from your web browser. It embeds
  7. ESP8266HTTPUpdateServer into the AutoConnect menu and can invoke the
  8. firmware update UI via a Web browser.
  9. You need a compiled sketch binary file to the actual update and can
  10. retrieve it using Arduino-IDE menu: [Sketck] -> [Export compiled binary].
  11. Then you will find the .bin file in your sketch folder. Select the.bin
  12. file on the update UI page to update the firmware.
  13. Notes:
  14. 1. To experience this example, your client OS needs to be running a
  15. service that can respond to multicast DNS.
  16. For Mac OSX support is built in through Bonjour already.
  17. For Linux, install Avahi.
  18. For Windows10, available since Windows10 1803(April 2018 Update/RS4).
  19. 2. If you receive an error as follows:
  20. Update error: ERROR[11]: Invalid bootstrapping state, reset ESP8266 before updating.
  21. You need reset the module before sketch running.
  22. Refer to https://hieromon.github.io/AutoConnect/faq.html#hang-up-after-reset for details.
  23. This software is released under the MIT License.
  24. https://opensource.org/licenses/MIT
  25. */
  26. #if defined(ARDUINO_ARCH_ESP8266)
  27. #include <ESP8266WiFi.h>
  28. #include <ESP8266WebServer.h>
  29. #include <ESP8266mDNS.h>
  30. #include <ESP8266HTTPUpdateServer.h>
  31. #define HOSTIDENTIFY "esp8266"
  32. #define mDNSUpdate(c) do { c.update(); } while(0)
  33. using WebServerClass = ESP8266WebServer;
  34. using HTTPUpdateServerClass = ESP8266HTTPUpdateServer;
  35. #elif defined(ARDUINO_ARCH_ESP32)
  36. #include <WiFi.h>
  37. #include <WebServer.h>
  38. #include <ESPmDNS.h>
  39. #include "HTTPUpdateServer.h"
  40. #define HOSTIDENTIFY "esp32"
  41. #define mDNSUpdate(c) do {} while(0)
  42. using WebServerClass = WebServer;
  43. using HTTPUpdateServerClass = HTTPUpdateServer;
  44. #endif
  45. #include <WiFiClient.h>
  46. #include <AutoConnect.h>
  47. // This page for an example only, you can prepare the other for your application.
  48. static const char AUX_AppPage[] PROGMEM = R"(
  49. {
  50. "title": "Hello world",
  51. "uri": "/",
  52. "menu": true,
  53. "element": [
  54. {
  55. "name": "caption",
  56. "type": "ACText",
  57. "value": "<h2>Hello, world</h2>",
  58. "style": "text-align:center;color:#2f4f4f;padding:10px;"
  59. },
  60. {
  61. "name": "content",
  62. "type": "ACText",
  63. "value": "In this page, place the custom web page handled by the sketch application."
  64. }
  65. ]
  66. }
  67. )";
  68. // Fix hostname for mDNS. It is a requirement for the lightweight update feature.
  69. static const char* host = HOSTIDENTIFY "-webupdate";
  70. #define HTTP_PORT 80
  71. // ESP8266WebServer instance will be shared both AutoConnect and UpdateServer.
  72. WebServerClass httpServer(HTTP_PORT);
  73. #define USERNAME "user" //*< Replace the actual username you want */
  74. #define PASSWORD "pass" //*< Replace the actual password you want */
  75. // Declare AutoConnectAux to bind the HTTPWebUpdateServer via /update url
  76. // and call it from the menu.
  77. // The custom web page is an empty page that does not contain AutoConnectElements.
  78. // Its content will be emitted by ESP8266HTTPUpdateServer.
  79. HTTPUpdateServerClass httpUpdater;
  80. AutoConnectAux update("/update", "Update");
  81. // Declare AutoConnect and the custom web pages for an application sketch.
  82. AutoConnect portal(httpServer);
  83. AutoConnectAux hello;
  84. void setup() {
  85. delay(1000);
  86. Serial.begin(115200);
  87. Serial.println("\nBooting Sketch...");
  88. // Prepare the ESP8266HTTPUpdateServer
  89. // The /update handler will be registered during this function.
  90. httpUpdater.setup(&httpServer, USERNAME, PASSWORD);
  91. // Load a custom web page for a sketch and a dummy page for the updater.
  92. hello.load(AUX_AppPage);
  93. portal.join({ hello, update });
  94. if (portal.begin()) {
  95. if (MDNS.begin(host)) {
  96. MDNS.addService("http", "tcp", HTTP_PORT);
  97. Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", host);
  98. }
  99. else
  100. Serial.println("Error setting up MDNS responder");
  101. }
  102. }
  103. void loop() {
  104. // Sketches the application here.
  105. // Invokes mDNS::update and AutoConnect::handleClient() for the menu processing.
  106. mDNSUpdate(MDNS);
  107. portal.handleClient();
  108. delay(1);
  109. }