HandlePortalEX.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. HandlePortalEX.ino, Example for the AutoConnect library.
  3. Copyright (c) 2018, Hieromon Ikasamo
  4. https://github.com/Hieromon/AutoConnect
  5. This software is released under the MIT License.
  6. https://opensource.org/licenses/MIT
  7. */
  8. /*
  9. This is an explicit declaration of ESP8266WebServer. AutoConnect uses
  10. its declaration. Also, by using PageBuilder for HTML assembly, you can
  11. display the web page without using the ESP8266WebServer::send() function.
  12. I recommend that you consider this example compared to HandlePortal.ino.
  13. https://github.com/Hieromon/AutoConnect/blob/master/examples/HandlePortal/HandlePortal.ino
  14. It will help you understand AutoConnect usage.
  15. */
  16. #if defined(ARDUINO_ARCH_ESP8266)
  17. #include <ESP8266WiFi.h>
  18. #include <ESP8266WebServer.h>
  19. #elif defined(ARDUINO_ARCH_ESP32)
  20. #include <WiFi.h>
  21. #include <WebServer.h>
  22. #endif
  23. #include <PageBuilder.h>
  24. #include <AutoConnect.h>
  25. #ifndef BUILTIN_LED
  26. #define BUILTIN_LED 2 // backward compatibility
  27. #endif
  28. #if defined(ARDUINO_ARCH_ESP8266)
  29. ESP8266WebServer server;
  30. #elif defined(ARDUINO_ARCH_ESP32)
  31. WebServer server;
  32. #endif
  33. AutoConnect portal(server);
  34. void sendRedirect(String);
  35. static const char PROGMEM mold_page[] = R"*lit(
  36. <html>
  37. <head>
  38. <meta name="viewport" content="width=device-width, initial-scale=1">
  39. <style type="text/css">
  40. body {
  41. -webkit-appearance:none;
  42. -moz-appearance:none;
  43. font-family:'Arial',sans-serif;
  44. text-align:center;
  45. }
  46. .menu > a:link {
  47. position: absolute;
  48. display: inline-block;
  49. right: 12px;
  50. padding: 0 6px;
  51. text-decoration: none;
  52. }
  53. .button {
  54. display:inline-block;
  55. border-radius:7px;
  56. background:#73ad21;
  57. margin:0 10px 0 10px;
  58. padding:10px 20px 10px 20px;
  59. text-decoration:none;
  60. color:#000000;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="menu">{{AUTOCONNECT_MENU}}</div>
  66. BUILT-IN LED<br>
  67. GPIO({{LED}}) : <span style="font-weight:bold;color:{{COLOR}}">{{GPIO}}</span>
  68. <p><a class="button" href="/io?v=low">low</a><a class="button" href="/io?v=high">high</a></p>
  69. </body>
  70. </html>
  71. )*lit";
  72. static const char PROGMEM autoconnectMenu[] = { AUTOCONNECT_LINK(BAR_24) };
  73. String getLEDPort(PageArgument& args) {
  74. return String(BUILTIN_LED);
  75. }
  76. String setColor(PageArgument& args) {
  77. return digitalRead(BUILTIN_LED) ? "Tomato" : "SlateBlue";
  78. }
  79. String readLEDPort(PageArgument& args) {
  80. return digitalRead(BUILTIN_LED) ? "HIGH" : "LOW";
  81. }
  82. PageElement elm_gpio(mold_page, {
  83. {"LED", getLEDPort},
  84. {"COLOR", setColor},
  85. {"GPIO", readLEDPort},
  86. {"AUTOCONNECT_MENU", [](PageArgument& args) {
  87. return String(FPSTR(autoconnectMenu));}}
  88. });
  89. PageBuilder root("/", { elm_gpio });
  90. String gpio(PageArgument& args) {
  91. if (args.arg("v") == "low")
  92. digitalWrite(BUILTIN_LED, LOW);
  93. else if (args.arg("v") == "high")
  94. digitalWrite(BUILTIN_LED, HIGH);
  95. sendRedirect("/");
  96. return "";
  97. }
  98. PageElement elm_io("{{IO}}", { {"IO", gpio} });
  99. PageBuilder io("/io", { elm_io });
  100. // This function is for redirect only.
  101. // The actual sending the HTML performs in PageBuilder.
  102. void sendRedirect(String uri) {
  103. server.sendHeader("Location", uri, true);
  104. server.send(302, "text/plain", "");
  105. server.client().stop();
  106. io.cancel();
  107. }
  108. bool atDetect(IPAddress& softapIP) {
  109. Serial.println("Captive portal started, SoftAP IP:" + softapIP.toString());
  110. return true;
  111. }
  112. void setup() {
  113. delay(1000);
  114. Serial.begin(115200);
  115. Serial.println();
  116. pinMode(BUILTIN_LED, OUTPUT);
  117. // Put the home location of the web site.
  118. // But in usually, setting the home uri is not needed cause default location is "/".
  119. //portal.home("/");
  120. // Starts user web site included the AutoConnect portal.
  121. portal.onDetect(atDetect);
  122. if (portal.begin()) {
  123. // Register the page request handlers.
  124. root.insert(server);
  125. io.insert(server);
  126. Serial.println("Started, IP:" + WiFi.localIP().toString());
  127. }
  128. else {
  129. Serial.println("Connection failed.");
  130. while (true) { yield(); }
  131. }
  132. }
  133. void loop() {
  134. portal.handleClient();
  135. if (WiFi.status() == WL_IDLE_STATUS) {
  136. #if defined(ARDUINO_ARCH_ESP8266)
  137. ESP.reset();
  138. #elif defined(ARDUINO_ARCH_ESP32)
  139. ESP.restart();
  140. #endif
  141. delay(1000);
  142. }
  143. }