HandleClient.ino 3.2 KB

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