166 lines
7.4 KiB
C++
166 lines
7.4 KiB
C++
#include <Arduino.h>
|
||
#include <string.h>
|
||
#include <WiFi.h>
|
||
#include <DNSServer.h>
|
||
#include <WebServer.h>
|
||
#include "portal.h"
|
||
#include "store.h"
|
||
|
||
static DNSServer s_dns;
|
||
static WebServer s_server(80);
|
||
static bool s_saved = false;
|
||
static bool s_routed = false;
|
||
|
||
static const char *PAGE_HEAD =
|
||
"<meta name=viewport content='width=device-width,initial-scale=1'>"
|
||
"<style>body{font-family:system-ui;background:#04121a;color:#cfe;padding:16px}"
|
||
"input,select,button{width:100%;padding:10px;margin:6px 0;font-size:16px}"
|
||
"h3{color:#4ee}</style>";
|
||
|
||
static void handle_root()
|
||
{
|
||
char api[96] = {0}, saved[33] = {0};
|
||
if (!store_get_str(K_API, api, sizeof(api))) strcpy(api, "https://cosmo.unisspace.com/api");
|
||
store_get_str(K_SSID, saved, sizeof(saved));
|
||
|
||
int n = WiFi.scanNetworks();
|
||
/* 第一项是"不动 WiFi 只改后端地址":换开发机 IP 时不必重输密码 */
|
||
String opts = "<option value=''>\xe4\xb8\x8d\xe6\x94\xb9\x20\x57\x69\x46\x69\xef\xbc\x8c\xe5\x8f\xaa\xe4\xbf\x9d\xe5\xad\x98\xe5\x90\x8e\xe7\xab\xaf\xe5\x9c\xb0\xe5\x9d\x80</option>";
|
||
for (int i = 0; i < n; i++) {
|
||
bool cur = saved[0] && WiFi.SSID(i) == saved;
|
||
opts += "<option value='";
|
||
opts += WiFi.SSID(i);
|
||
opts += "'";
|
||
if (cur) opts += " selected";
|
||
opts += ">";
|
||
opts += WiFi.SSID(i);
|
||
opts += " (";
|
||
opts += String(WiFi.RSSI(i));
|
||
opts += " dBm)";
|
||
if (cur) opts += " \xe2\x86\x90\xe5\xbd\x93\xe5\x89\x8d";
|
||
opts += "</option>";
|
||
}
|
||
|
||
/* 一个热点都没扫到(路由器只开 5G、或 SSID 隐藏)时,
|
||
下拉框会是空的,用户就没法配网了,这时给个手填入口 */
|
||
if (n <= 0) {
|
||
opts += "<option value=''>\xe6\x9c\xaa\xe6\x89\xab\xe6\x8f\x8f\xe5\x88\xb0\x20\x32\x2e\x34\x47\x20\xe7\x83\xad\xe7\x82\xb9</option>";
|
||
/* 扫不到但存过凭据:至少让用户把原网络再选回来 */
|
||
if (saved[0]) opts += "<option value='" + String(saved) + "' selected>" + String(saved) +
|
||
" (\xe5\xbd\x93\xe5\x89\x8d\xe4\xbf\x9d\xe5\xad\x98\x29</option>";
|
||
}
|
||
|
||
String page = "<html><head><meta charset=utf-8>";
|
||
page += PAGE_HEAD;
|
||
page += "</head><body><h3>COSMO \xe7\xbb\x88\xe7\xab\xaf\xe9\x85\x8d\xe7\xbd\x91</h3>"; /* 终端配网 */
|
||
page += "<form method=POST action=/join>";
|
||
page += "<label>WiFi</label><select name=ssid>" + opts + "</select>";
|
||
/* 列表里没有想连的热点时直接填名称,填了就覆盖下拉框的选择 */
|
||
page += "<label>\xe6\x89\x8b\xe5\x8a\xa8\xe8\xbe\x93\xe5\x85\xa5 SSID\xef\xbc\x88\xe5\x8f\xaf\xe7\x95\x99\xe7\xa9\xba\xef\xbc\x89</label><input name=ssid_manual>";
|
||
page += "<label>\xe5\xaf\x86\xe7\xa0\x81\xef\xbc\x88\xe8\xbf\x9e\xe5\x9b\x9e\xe5\xbd\x93\xe5\x89\x8d\xe7\xbd\x91\xe7\xbb\x9c\xe5\x8f\xaf\xe7\x95\x99\xe7\xa9\xba\xef\xbc\x89</label><input name=pass type=text>";
|
||
page += "<label>\xe5\x90\x8e\xe7\xab\xaf\xe5\x9c\xb0\xe5\x9d\x80</label><input name=api value='"; /* 后端地址 */
|
||
page += api;
|
||
page += "'><br><button type=submit>\xe4\xbf\x9d\xe5\xad\x98</button></form>"; /* 保存 */
|
||
page += "<p style='color:#8ab;font-size:13px'>\xe4\xbb\x85\xe6\x94\xaf\xe6\x8c\x81 2.4GHz\xef\xbc\x8c\xe4\xbf\x9d\xe5\xad\x98\xe5\x90\x8e\xe8\xae\xbe\xe5\xa4\x87\xe8\x87\xaa\xe5\x8a\xa8\xe9\x87\x8d\xe5\x90\xaf</p>";
|
||
page += "</body></html>";
|
||
s_server.send(200, "text/html", page);
|
||
}
|
||
static void handle_join()
|
||
{
|
||
String ssid = s_server.hasArg("ssid") ? s_server.arg("ssid") : "";
|
||
String pass = s_server.hasArg("pass") ? s_server.arg("pass") : "";
|
||
String api = s_server.hasArg("api") ? s_server.arg("api") : "";
|
||
String man = s_server.hasArg("ssid_manual") ? s_server.arg("ssid_manual") : "";
|
||
ssid.trim(); pass.trim(); api.trim(); man.trim();
|
||
if (man.length()) ssid = man;
|
||
|
||
char saved[33] = {0}, cur_api[96] = {0};
|
||
store_get_str(K_SSID, saved, sizeof(saved));
|
||
store_get_str(K_API, cur_api, sizeof(cur_api));
|
||
bool wifi_changed = ssid.length() && strcmp(ssid.c_str(), saved);
|
||
bool touched = false;
|
||
|
||
/* SSID 留空 = WiFi 一点不动,只改后端地址;
|
||
选回当前网络又没填密码 = 沿用存过的密码。
|
||
换开发机 IP 是最常见的一次改动,不该逼人把 WiFi 密码重新抄一遍。 */
|
||
if (ssid.length()) {
|
||
if (wifi_changed) { store_set_str(K_SSID, ssid.c_str()); touched = true; }
|
||
if (wifi_changed || pass.length()) { store_set_str(K_PASS, pass.c_str()); touched = true; }
|
||
}
|
||
if (api.length() && strcmp(api.c_str(), cur_api)) {
|
||
store_set_str(K_API, api.c_str());
|
||
touched = true;
|
||
}
|
||
Serial.printf("[portal] ssid=%s(%s) pass=%s api=%s\n",
|
||
ssid.length() ? ssid.c_str() : saved, wifi_changed ? "changed" : "kept",
|
||
pass.length() ? "updated" : "unchanged",
|
||
api.length() ? api.c_str() : "(kept)");
|
||
|
||
if (!touched) {
|
||
s_server.send(200, "text/html",
|
||
"<html><meta charset=utf-8><body style='font-family:system-ui;"
|
||
"background:#04121a;color:#cfe;padding:16px'>没有需要保存的改动</body></html>");
|
||
return;
|
||
}
|
||
s_server.send(200, "text/html",
|
||
"<html><meta charset=utf-8><body style='font-family:system-ui'>"
|
||
"已保存,设备正在重启</body></html>");
|
||
s_saved = true;
|
||
}
|
||
|
||
/* 手机/系统探测门户时回到配网页 */
|
||
static void handle_captive()
|
||
{
|
||
s_server.sendHeader("Location", "http://192.168.4.1/", true);
|
||
s_server.send(302, "text/plain", "");
|
||
}
|
||
|
||
void portal_start(const char *ap_ssid, const char *api_default, bool keep_sta)
|
||
{
|
||
/* keep_sta:调试用。原有 STA 连接不断,门户同时挂在局域网那一侧,
|
||
同一网段直接浏览器/curl 打开设备 IP 就能验证门户在不在服务,不用先连热点。 */
|
||
WiFi.mode(keep_sta ? WIFI_MODE_APSTA : WIFI_MODE_AP);
|
||
WiFi.softAP(ap_ssid);
|
||
s_saved = false;
|
||
s_dns.start(53, "*", WiFi.softAPIP());
|
||
if (!s_routed) { /* 路由只注册一次:反复开热点会越堆越多 */
|
||
s_server.on("/", HTTP_GET, handle_root);
|
||
s_server.on("/join", HTTP_POST, handle_join);
|
||
s_server.on("/generate_204", handle_captive);
|
||
s_server.on("/hotspot-detect.html", handle_captive);
|
||
s_server.on("/connecttest.txt", handle_captive);
|
||
s_server.onNotFound(handle_captive);
|
||
s_routed = true;
|
||
}
|
||
/* begin() 每轮都要调:portal_stop() 里的 close() 只关监听,路由留给下次。
|
||
放在 if 里会让第二次及以后开的热点能连上但打不开页面(换环境时最容易踩)。 */
|
||
s_server.begin();
|
||
Serial.printf("[portal] AP=%s api=%s ip=%s", ap_ssid, api_default,
|
||
WiFi.softAPIP().toString().c_str());
|
||
if (keep_sta) Serial.printf(" lan=%s", WiFi.localIP().toString().c_str());
|
||
Serial.println();
|
||
}
|
||
|
||
/* 退出配网:关掉热点和 DNS,把射频交还 STA,让主循环拿 NVS 里的旧凭据再试一次。
|
||
没有这一步,误进配网页就只能重输 WiFi 密码或拔电。 */
|
||
void portal_stop()
|
||
{
|
||
Serial.println(F("[portal] hotspot closed"));
|
||
s_server.close(); /* 只停监听,路由留给下次 */
|
||
s_dns.stop();
|
||
WiFi.softAPdisconnect(true);
|
||
WiFi.mode(WIFI_MODE_STA);
|
||
s_saved = false;
|
||
}
|
||
|
||
void portal_loop()
|
||
{
|
||
s_dns.processNextRequest();
|
||
s_server.handleClient();
|
||
}
|
||
|
||
bool portal_saved()
|
||
{
|
||
return s_saved;
|
||
}
|