You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AutoConnect/examples/Credential/Credential.ino

174 lines
5.0 KiB

7 years ago
/*
Credential.ino, AutoConnect for ESP8266.
https://github.com/Hieromon/AutoConnect
Copyright 2018, Hieromon Ikasamo.
Licensed under The MIT License.
https://opensource.org/licenses/MIT
An example sketch for an Arduino library for ESP8266 WLAN configuration
via the Web interface. This sketch provides a conservation measures
utility for saved credentials in EEPROM.
By accessing the root path, you can see the list of currently saved
credentials via the browser. Enter an entry number of the credential,
that entry will be deleted from EEPROM.
This sketch uses PageBuilder to support handling of operation pages.
*/
6 years ago
#if defined(ARDUINO_ARCH_ESP8266)
7 years ago
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
6 years ago
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#include <WebServer.h>
#endif
7 years ago
#include <AutoConnect.h>
#include <AutoConnectCredential.h>
7 years ago
#include <PageBuilder.h>
6 years ago
#if defined(ARDUINO_ARCH_ESP8266)
7 years ago
ESP8266WebServer Server;
6 years ago
#elif defined(ARDUINO_ARCH_ESP32)
WebServer Server;
#endif
7 years ago
AutoConnect Portal(Server);
String viewCredential(PageArgument&);
String delCredential(PageArgument&);
// Specified the offset if the user data exists.
7 years ago
// The following two lines define the boundalyOffset value to be supplied to
// AutoConnectConfig respectively. It may be necessary to adjust the value
// accordingly to the actual situation.
#define CREDENTIAL_OFFSET 0
//#define CREDENTIAL_OFFSET 64
7 years ago
/**
* An HTML for the operation page.
* In PageBuilder, the token {{SSID}} contained in an HTML template below is
* replaced by the actual SSID due to the action of the token handler's
* 'viewCredential' function.
* The number of the entry to be deleted is passed to the function in the
* POST method.
*/
7 years ago
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) };
7 years ago
// URL path as '/'
7 years ago
PageElement elmList(html,
{{ "SSID", viewCredential },
{ "AUTOCONNECT_MENU", [](PageArgument& args) {
return String(FPSTR(autoconnectMenu));} }
});
7 years ago
PageBuilder rootPage("/", { elmList });
// URL path as '/del'
PageElement elmDel("{{DEL}}", {{ "DEL", delCredential }});
PageBuilder delPage("/del", { elmDel });
// Retrieve the credential entries from EEPROM, Build the SSID line
// with the <li> tag.
String viewCredential(PageArgument& args) {
AutoConnectCredential ac(CREDENTIAL_OFFSET);
station_config_t entry;
7 years ago
String content = "";
uint8_t count = ac.entries(); // Get number of entries.
for (int8_t i = 0; i < count; i++) { // Loads all entries.
ac.load(i, &entry);
// Build a SSID line of an HTML.
content += String("<li>") + String((char *)entry.ssid) + String("</li>");
}
// Returns the '<li>SSID</li>' container.
return content;
}
// Delete a credential entry, the entry to be deleted is passed in the
// request parameter 'num'.
String delCredential(PageArgument& args) {
AutoConnectCredential ac(CREDENTIAL_OFFSET);
7 years ago
if (args.hasArg("num")) {
int8_t e = args.arg("num").toInt();
Serial.printf("Request deletion #%d\n", e);
7 years ago
if (e > 0) {
station_config_t entry;
7 years ago
// If the input number is valid, delete that entry.
int8_t de = ac.load(e - 1, &entry); // A base of entry num is 0.
7 years ago
if (de > 0) {
Serial.printf("Delete for %s ", (char *)entry.ssid);
Serial.printf("%s\n", ac.del((char *)entry.ssid) ? "completed" : "failed");
7 years ago
// Returns the redirect response. The page is reloaded and its contents
// are updated to the state after deletion. It returns 302 response
// from inside this token handler.
Server.sendHeader("Location", String("http://") + Server.client().localIP().toString() + String("/"));
Server.send(302, "text/plain", "");
Server.client().flush();
Server.client().stop();
// Cancel automatic submission by PageBuilder.
delPage.cancel();
}
}
}
return "";
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
rootPage.insert(Server); // Instead of Server.on("/", ...);
delPage.insert(Server); // Instead of Server.on("/del", ...);
// Set an address of the credential area.
AutoConnectConfig Config;
Config.boundaryOffset = CREDENTIAL_OFFSET;
Portal.config(Config);
// Start
7 years ago
if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
}
}
void loop() {
Portal.handleClient();
}