HandlePortal.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. HandlePortal.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 a way of not explicitly declaring ESP8266WebServer. It uses
  10. the ESP8266WebServer function without its declaration.
  11. I recommend that you consider this example compared to HandlePortalEX.ino.
  12. https://github.com/Hieromon/AutoConnect/blob/master/examples/HandlePortalEX/HandlePortalEX.ino
  13. It will help you understand AutoConnect usage.
  14. */
  15. #if defined(ARDUINO_ARCH_ESP8266)
  16. #include <ESP8266WiFi.h>
  17. #include <ESP8266WebServer.h>
  18. #elif defined(ARDUINO_ARCH_ESP32)
  19. #include <WiFi.h>
  20. #include <WebServer.h>
  21. #endif
  22. #include <AutoConnect.h>
  23. #ifndef BUILTIN_LED
  24. #define BUILTIN_LED 2 // backward compatibility
  25. #endif
  26. AutoConnect portal;
  27. void handleRoot() {
  28. String page = PSTR(
  29. "<html>"
  30. "<head>"
  31. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
  32. "<style type=\"text/css\">"
  33. "body {"
  34. "-webkit-appearance:none;"
  35. "-moz-appearance:none;"
  36. "font-family:'Arial',sans-serif;"
  37. "text-align:center;"
  38. "}"
  39. ".menu > a:link {"
  40. "position: absolute;"
  41. "display: inline-block;"
  42. "right: 12px;"
  43. "padding: 0 6px;"
  44. "text-decoration: none;"
  45. "}"
  46. ".button {"
  47. "display:inline-block;"
  48. "border-radius:7px;"
  49. "background:#73ad21;"
  50. "margin:0 10px 0 10px;"
  51. "padding:10px 20px 10px 20px;"
  52. "text-decoration:none;"
  53. "color:#000000;"
  54. "}"
  55. "</style>"
  56. "</head>"
  57. "<body>"
  58. "<div class=\"menu\">" AUTOCONNECT_LINK(BAR_32) "</div>"
  59. "BUILT-IN LED<br>"
  60. "GPIO(");
  61. page += String(BUILTIN_LED);
  62. page += String(F(") : <span style=\"font-weight:bold;color:"));
  63. page += digitalRead(BUILTIN_LED) ? String("Tomato\">HIGH") : String("SlateBlue\">LOW");
  64. page += String(F("</span>"));
  65. page += String(F("<p><a class=\"button\" href=\"/io?v=low\">low</a><a class=\"button\" href=\"/io?v=high\">high</a></p>"));
  66. page += String(F("</body></html>"));
  67. portal.host().send(200, "text/html", page);
  68. }
  69. void sendRedirect(String uri) {
  70. WebServerClass& server = portal.host();
  71. server.sendHeader("Location", uri, true);
  72. server.send(302, "text/plain", "");
  73. server.client().stop();
  74. }
  75. void handleGPIO() {
  76. WebServerClass& server = portal.host();
  77. if (server.arg("v") == "low")
  78. digitalWrite(BUILTIN_LED, LOW);
  79. else if (server.arg("v") == "high")
  80. digitalWrite(BUILTIN_LED, HIGH);
  81. sendRedirect("/");
  82. }
  83. bool atDetect(IPAddress& softapIP) {
  84. Serial.println("Captive portal started, SoftAP IP:" + softapIP.toString());
  85. return true;
  86. }
  87. void setup() {
  88. delay(1000);
  89. Serial.begin(115200);
  90. Serial.println();
  91. pinMode(BUILTIN_LED, OUTPUT);
  92. // Put the home location of the web site.
  93. // But in usually, setting the home uri is not needed cause default location is "/".
  94. //portal.home("/");
  95. // Starts user web site included the AutoConnect portal.
  96. portal.onDetect(atDetect);
  97. if (portal.begin()) {
  98. WebServerClass& server = portal.host();
  99. server.on("/", handleRoot);
  100. server.on("/io", handleGPIO);
  101. Serial.println("Started, IP:" + WiFi.localIP().toString());
  102. }
  103. else {
  104. Serial.println("Connection failed.");
  105. while (true) { yield(); }
  106. }
  107. }
  108. void loop() {
  109. portal.handleClient();
  110. if (WiFi.status() == WL_IDLE_STATUS) {
  111. #if defined(ARDUINO_ARCH_ESP8266)
  112. ESP.reset();
  113. #elif defined(ARDUINO_ARCH_ESP32)
  114. ESP.restart();
  115. #endif
  116. delay(1000);
  117. }
  118. }