-
Notifications
You must be signed in to change notification settings - Fork 191
/
Copy pathCredential.ino
173 lines (151 loc) · 5.02 KB
/
Credential.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
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.
*/
#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&);
// Specified the offset if the user data exists.
// 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
/**
* 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.
*/
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) };
// URL path as '/'
PageElement elmList(html,
{{ "SSID", viewCredential },
{ "AUTOCONNECT_MENU", [](PageArgument& args) {
return String(FPSTR(autoconnectMenu));} }
});
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;
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);
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;
// If the input number is valid, delete that entry.
int8_t de = ac.load(e - 1, &entry); // A base of entry num is 0.
if (de > 0) {
Serial.printf("Delete for %s ", (char *)entry.ssid);
Serial.printf("%s\n", ac.del((char *)entry.ssid) ? "completed" : "failed");
// 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
if (Portal.begin()) {
Serial.println("WiFi connected: " + WiFi.localIP().toString());
}
}
void loop() {
Portal.handleClient();
}