Credential.ino 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Credential.ino, AutoConnect for ESP8266.
  3. https://github.com/Hieromon/AutoConnect
  4. Copyright 2018, Hieromon Ikasamo.
  5. Licensed under The MIT License.
  6. https://opensource.org/licenses/MIT
  7. An example sketch for an Arduino library for ESP8266 WLAN configuration
  8. via the Web interface. This sketch provides a conservation measures
  9. utility for saved credentials in EEPROM.
  10. By accessing the root path, you can see the list of currently saved
  11. credentials via the browser. Enter an entry number of the credential,
  12. that entry will be deleted from EEPROM.
  13. This sketch uses PageBuilder to support handling of operation pages.
  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. #include <AutoConnectCredential.h>
  24. #include <PageBuilder.h>
  25. #if defined(ARDUINO_ARCH_ESP8266)
  26. ESP8266WebServer Server;
  27. #elif defined(ARDUINO_ARCH_ESP32)
  28. WebServer Server;
  29. #endif
  30. AutoConnect Portal(Server);
  31. String viewCredential(PageArgument&);
  32. String delCredential(PageArgument&);
  33. // Specified the offset if the user data exists.
  34. // The following two lines define the boundalyOffset value to be supplied to
  35. // AutoConnectConfig respectively. It may be necessary to adjust the value
  36. // accordingly to the actual situation.
  37. #define CREDENTIAL_OFFSET 0
  38. //#define CREDENTIAL_OFFSET 64
  39. /**
  40. * An HTML for the operation page.
  41. * In PageBuilder, the token {{SSID}} contained in an HTML template below is
  42. * replaced by the actual SSID due to the action of the token handler's
  43. * 'viewCredential' function.
  44. * The number of the entry to be deleted is passed to the function in the
  45. * POST method.
  46. */
  47. static const char PROGMEM html[] = R"*lit(
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
  52. <style>
  53. html {
  54. font-family:Helvetica,Arial,sans-serif;
  55. -ms-text-size-adjust:100%;
  56. -webkit-text-size-adjust:100%;
  57. }
  58. .menu > a:link {
  59. position: absolute;
  60. display: inline-block;
  61. right: 12px;
  62. padding: 0 6px;
  63. text-decoration: none;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <div class="menu">{{AUTOCONNECT_MENU}}</div>
  69. <form action="/del" method="POST">
  70. <ol>
  71. {{SSID}}
  72. </ol>
  73. <p>Enter deleting entry:</p>
  74. <input type="number" min="1" name="num">
  75. <input type="submit">
  76. </form>
  77. </body>
  78. </html>
  79. )*lit";
  80. static const char PROGMEM autoconnectMenu[] = { AUTOCONNECT_LINK(BAR_24) };
  81. // URL path as '/'
  82. PageElement elmList(html,
  83. {{ "SSID", viewCredential },
  84. { "AUTOCONNECT_MENU", [](PageArgument& args) {
  85. return String(FPSTR(autoconnectMenu));} }
  86. });
  87. PageBuilder rootPage("/", { elmList });
  88. // URL path as '/del'
  89. PageElement elmDel("{{DEL}}", {{ "DEL", delCredential }});
  90. PageBuilder delPage("/del", { elmDel });
  91. // Retrieve the credential entries from EEPROM, Build the SSID line
  92. // with the <li> tag.
  93. String viewCredential(PageArgument& args) {
  94. AutoConnectCredential ac(CREDENTIAL_OFFSET);
  95. station_config_t entry;
  96. String content = "";
  97. uint8_t count = ac.entries(); // Get number of entries.
  98. for (int8_t i = 0; i < count; i++) { // Loads all entries.
  99. ac.load(i, &entry);
  100. // Build a SSID line of an HTML.
  101. content += String("<li>") + String((char *)entry.ssid) + String("</li>");
  102. }
  103. // Returns the '<li>SSID</li>' container.
  104. return content;
  105. }
  106. // Delete a credential entry, the entry to be deleted is passed in the
  107. // request parameter 'num'.
  108. String delCredential(PageArgument& args) {
  109. AutoConnectCredential ac(CREDENTIAL_OFFSET);
  110. if (args.hasArg("num")) {
  111. int8_t e = args.arg("num").toInt();
  112. Serial.printf("Request deletion #%d\n", e);
  113. if (e > 0) {
  114. station_config_t entry;
  115. // If the input number is valid, delete that entry.
  116. int8_t de = ac.load(e - 1, &entry); // A base of entry num is 0.
  117. if (de > 0) {
  118. Serial.printf("Delete for %s ", (char *)entry.ssid);
  119. Serial.printf("%s\n", ac.del((char *)entry.ssid) ? "completed" : "failed");
  120. // Returns the redirect response. The page is reloaded and its contents
  121. // are updated to the state after deletion. It returns 302 response
  122. // from inside this token handler.
  123. Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/"));
  124. Server.send(302, "text/plain", "");
  125. Server.client().flush();
  126. Server.client().stop();
  127. // Cancel automatic submission by PageBuilder.
  128. delPage.cancel();
  129. }
  130. }
  131. }
  132. return "";
  133. }
  134. void setup() {
  135. delay(1000);
  136. Serial.begin(115200);
  137. Serial.println();
  138. rootPage.insert(Server); // Instead of Server.on("/", ...);
  139. delPage.insert(Server); // Instead of Server.on("/del", ...);
  140. // Set an address of the credential area.
  141. AutoConnectConfig Config;
  142. Config.boundaryOffset = CREDENTIAL_OFFSET;
  143. Portal.config(Config);
  144. // Start
  145. if (Portal.begin()) {
  146. Serial.println("WiFi connected: " + WiFi.localIP().toString());
  147. }
  148. }
  149. void loop() {
  150. Portal.handleClient();
  151. }