23 explicit SetupPortal(
const char* network,
const char* password =
nullptr)
24 : network_(network), apPassword_(password ? password :
"") {}
27 void run(String& ssid, String& password) {
31 uint32_t lastReport = 0;
34 dns_.processNextRequest();
35 server_.handleClient();
36 if (
TWD_LOGGER.enabled() && millis() - lastReport > 2000) {
37 lastReport = millis();
38 int n = WiFi.softAPgetStationNum();
41 TWD_LOGGER.info(
"setup: %d device(s) connected to the setup network", n);
49 password = enteredPassword_;
53 String network_, apPassword_;
54 WebServer server_{80};
57 String ssid_, enteredPassword_;
58 bool submitted_ =
false;
60 static String escape(
const String& s) {
62 for (
size_t i = 0; i < s.length(); i++) {
64 if (c ==
'<') out +=
"<";
65 else if (c ==
'>') out +=
">";
66 else if (c ==
'&') out +=
"&";
67 else if (c ==
'"') out +=
""";
77 int n = WiFi.scanNetworks();
79 TWD_LOGGER.
error(
"setup: scan failed (%d), the network name must be typed in", n);
83 for (
int i = 0; i < n; i++) {
84 String name = WiFi.SSID(i);
85 TWD_LOGGER.
info(
"setup: '%s' channel %d, %d dBm", name.c_str(), (
int)WiFi.channel(i), (
int)WiFi.RSSI(i));
86 if (name.length()) options_ +=
"<option value=\"" + escape(name) +
"\">";
94 WiFi.softAPConfig(IPAddress(192, 168, 4, 1), IPAddress(192, 168, 4, 1), IPAddress(255, 255, 255, 0));
96 bool ap = apPassword_.length() ? WiFi.softAP(network_.c_str(), apPassword_.c_str()) : WiFi.softAP(network_.c_str());
98 TWD_LOGGER.
error(
"setup: could not open the setup network '%s'%s", network_.c_str(),
99 apPassword_.length() && apPassword_.length() < 8 ?
" (password shorter than 8 characters)" :
"");
101 if (!dns_.start(53,
"*", WiFi.softAPIP())) {
102 TWD_LOGGER.
error(
"setup: could not start the DNS server, the page does not open by itself");
104 TWD_LOGGER.
info(
"setup: setup network '%s' open, address %s", network_.c_str(), WiFi.softAPIP().toString().c_str());
105 server_.on(
"/", [
this]() {
109 server_.on(
"/save", HTTP_POST, [
this]() {
113 server_.onNotFound([
this]() {
115 TWD_LOGGER.
debug(
"setup: %s %s%s -> redirect", server_.method() == HTTP_GET ?
"GET" :
"other",
116 server_.hostHeader().c_str(), server_.uri().c_str());
118 server_.sendHeader(
"Location", String(
"http://") + WiFi.softAPIP().toString() +
"/");
119 server_.send(302,
"text/plain",
"");
127 WiFi.softAPdisconnect(
true);
134 server_.send(200,
"text/html",
135 String(
"<!doctype html><meta name=viewport content='width=device-width,initial-scale=1'>"
136 "<title>WiFi setup</title><h2>Choose your WiFi network</h2>"
137 "<form method=post action=/save>Network<br>"
138 "<input name=ssid list=networks required maxlength=32><datalist id=networks>") +
140 "</datalist><br><br>Password<br><input name=password type=password maxlength=64>"
141 "<br><br><button>Save</button></form>");
145 ssid_ = server_.arg(
"ssid");
146 if (ssid_.length() == 0) {
148 server_.send(400,
"text/plain",
"The network name is missing.");
151 enteredPassword_ = server_.arg(
"password");
152 server_.send(200,
"text/html",
153 "<!doctype html><meta name=viewport content='width=device-width,initial-scale=1'>"
154 "<h2>Saved</h2>The board now joins the network. You can close this page.");
SetupPortal(const char *network, const char *password=nullptr)
The access point is called network; password (at least 8 characters) protects it, nullptr leaves it o...