Topuino_Hardware/src/WifiConfigManager.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

2022-07-02 13:04:34 +08:00
#include "WifiConfigManager.h"
#include "StatusLed.h"
#include "UserData.h"
2021-08-29 22:14:08 +08:00
#define SSID "Topuino"
2022-06-27 20:43:02 +08:00
#define WIFI_PASSWORD "vvzero.com"
2021-08-29 22:14:08 +08:00
2022-07-02 13:04:34 +08:00
extern StatusLed* statusLed;
extern WifiConfigManager* wifiConfigManager;
2021-08-29 22:14:08 +08:00
extern UserData* userdataManager;
static String configPage("\
<!DOCTYPE html>\
<html>\
<head>\
<title>Topuino</title>\
<meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=no' />\
<link rel='shortcut icon' href='data:,'>\
</head>\
<body>\
<h1 style='text-align: center; font-size: 40px'>Topuino Config Page</h1>\
<form action='/setup' method='POST'>\
<div style='text-align: center'>\
<div style='font-size: 20px; margin-bottom: 15px'>\
<label for='ssid'><b>Wi-Fi SSID</b></label>\
<input type='text' name='ssid' required>\
</div>\
<div style='font-size: 20px; margin-bottom: 15px'>\
<label for='psw'><b>Wi-Fi Password</b></label>\
<input type='password' name='psw' required>\
</div>\
<div style='font-size: 20px'>\
<button type='submit'>Commit</button>\
</div>\
</div>\
</form>\
</body>\
</html>\
");
static void handleOnConnect()
{
2022-07-02 13:04:34 +08:00
wifiConfigManager->httpServer.send(200, "text/html", configPage);
2021-08-29 22:14:08 +08:00
}
static void handleOnCommit()
{
2022-07-02 13:04:34 +08:00
if (wifiConfigManager->httpServer.hasArg("ssid") && wifiConfigManager->httpServer.hasArg("psw")) {
userdataManager->SetWifiSsid(wifiConfigManager->httpServer.arg("ssid"));
userdataManager->SetWifiPasswd(wifiConfigManager->httpServer.arg("psw"));
2022-06-27 20:43:02 +08:00
userdataManager->ConfirmWifiData();
2022-07-02 13:04:34 +08:00
wifiConfigManager->httpServer.send(200, "text/html", "OK");
statusLed->SetBlinkRate(StatusLed::BlinkRate::RateAlwaysOn);
2021-08-29 22:14:08 +08:00
delay(200);
ESP.restart();
} else {
2022-07-02 13:04:34 +08:00
wifiConfigManager->httpServer.send(200, "text/html", "ERROR");
2021-08-29 22:14:08 +08:00
}
}
2022-07-02 13:04:34 +08:00
WifiConfigManager::WifiConfigManager() :
2021-08-29 22:14:08 +08:00
localIp(192,168,1,1),
gateway(192,168,1,1),
subnet(255,255,255,0),
httpServer(80)
{
2022-07-02 13:04:34 +08:00
statusLed->SetBlinkRate(StatusLed::BlinkRate::Rate0_5Hz);
2021-08-29 22:14:08 +08:00
WiFi.softAPConfig(localIp, gateway, subnet);
2022-06-27 20:43:02 +08:00
WiFi.softAP(SSID, WIFI_PASSWORD);
2021-08-29 22:14:08 +08:00
delay(100);
httpServer.on("/", handleOnConnect);
httpServer.on("/setup", handleOnCommit);
httpServer.begin();
}
2022-07-02 13:04:34 +08:00
void WifiConfigManager::ProcessConfig()
2021-08-29 22:14:08 +08:00
{
while (1) {
httpServer.handleClient();
}
}