123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #if defined(ARDUINO_ARCH_ESP8266)
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #elif defined(ARDUINO_ARCH_ESP32)
- #include <WiFi.h>
- #include <WebServer.h>
- #endif
- #include <AutoConnect.h>
- #include <AutoConnectCredential.h>
- #include <PageBuilder.h>
- #if defined(ARDUINO_ARCH_ESP8266)
- ESP8266WebServer Server;
- #elif defined(ARDUINO_ARCH_ESP32)
- WebServer Server;
- #endif
- AutoConnect Portal(Server);
- String viewCredential(PageArgument&);
- String delCredential(PageArgument&);
- #define CREDENTIAL_OFFSET 0
- static const char PROGMEM html[] = R"*lit(
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1">
- <style>
- html {
- font-family:Helvetica,Arial,sans-serif;
- -ms-text-size-adjust:100%;
- -webkit-text-size-adjust:100%;
- }
- .menu > a:link {
- position: absolute;
- display: inline-block;
- right: 12px;
- padding: 0 6px;
- text-decoration: none;
- }
- </style>
- </head>
- <body>
- <div class="menu">{{AUTOCONNECT_MENU}}</div>
- <form action="/del" method="POST">
- <ol>
- {{SSID}}
- </ol>
- <p>Enter deleting entry:</p>
- <input type="number" min="1" name="num">
- <input type="submit">
- </form>
- </body>
- </html>
- )*lit";
- static const char PROGMEM autoconnectMenu[] = { AUTOCONNECT_LINK(BAR_24) };
- PageElement elmList(html,
- {{ "SSID", viewCredential },
- { "AUTOCONNECT_MENU", [](PageArgument& args) {
- return String(FPSTR(autoconnectMenu));} }
- });
- PageBuilder rootPage("/", { elmList });
- PageElement elmDel("{{DEL}}", {{ "DEL", delCredential }});
- PageBuilder delPage("/del", { elmDel });
- String viewCredential(PageArgument& args) {
- AutoConnectCredential ac(CREDENTIAL_OFFSET);
- station_config_t entry;
- String content = "";
- uint8_t count = ac.entries();
- for (int8_t i = 0; i < count; i++) {
- ac.load(i, &entry);
-
- content += String("<li>") + String((char *)entry.ssid) + String("</li>");
- }
-
- return content;
- }
- String delCredential(PageArgument& args) {
- AutoConnectCredential ac(CREDENTIAL_OFFSET);
- if (args.hasArg("num")) {
- int8_t e = args.arg("num").toInt();
- Serial.printf("Request deletion #%d\n", e);
- if (e > 0) {
- station_config_t entry;
-
- int8_t de = ac.load(e - 1, &entry);
- if (de > 0) {
- Serial.printf("Delete for %s ", (char *)entry.ssid);
- Serial.printf("%s\n", ac.del((char *)entry.ssid) ? "completed" : "failed");
-
-
-
- Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/"));
- Server.send(302, "text/plain", "");
- Server.client().flush();
- Server.client().stop();
-
- delPage.cancel();
- }
- }
- }
- return "";
- }
- void setup() {
- delay(1000);
- Serial.begin(115200);
- Serial.println();
- rootPage.insert(Server);
- delPage.insert(Server);
-
- AutoConnectConfig Config;
- Config.boundaryOffset = CREDENTIAL_OFFSET;
- Portal.config(Config);
-
- if (Portal.begin()) {
- Serial.println("WiFi connected: " + WiFi.localIP().toString());
- }
- }
- void loop() {
- Portal.handleClient();
- }
|