-
Notifications
You must be signed in to change notification settings - Fork 0
/
APManager.cpp
66 lines (48 loc) · 1.92 KB
/
APManager.cpp
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
#include "APManager.h"
#define SSID_SIZE 32
#define PASSWORD_SIZE 64
#define MAX_ATTEMPTS 10
APManager::APManager(const char* apSSID, ESP8266WebServer& server, EEPROMConfig& eepromConfig)
: _apSSID(apSSID), _server(server), _eepromConfig(eepromConfig) {}
void APManager::startAPMode() {
WiFi.softAP(_apSSID);
Serial.println("Entered AP Mode. Connect to WiFi network: " + String(_apSSID));
_server.on("/", HTTP_GET, std::bind(&APManager::handleRoot, this));
_server.on("/setup", HTTP_POST, std::bind(&APManager::handleSetup, this));
_server.begin(); // Start the web server
}
void APManager::handleClient() {
_server.handleClient();
}
void APManager::handleRoot() {
String html = "<form action='/setup' method='post'>"
"<input name='ssid' length=32 placeholder='SSID'>"
"<input type='text' name='password' length=64 placeholder='Password'>"
"<input type='submit' value='Save'>"
"</form>";
_server.send(200, "text/html", html);
}
void APManager::handleSetup() {
String ssid = _server.arg("ssid");
String password = _server.arg("password");
if (ssid.length() > SSID_SIZE || password.length() > PASSWORD_SIZE) {
_server.send(400, "text/plain", "SSID or Password too long!");
return;
}
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
char ssidCharArray[SSID_SIZE + 1];
char passwordCharArray[PASSWORD_SIZE + 1];
ssid.toCharArray(ssidCharArray, sizeof(ssidCharArray));
password.toCharArray(passwordCharArray, sizeof(passwordCharArray));
storeCredentials(ssidCharArray, passwordCharArray);
_server.send(200, "text/plain", "Saved. Restarting...");
delay(3000);
ESP.restart();
}
void APManager::storeCredentials(char* ssid, char* password) {
_eepromConfig.writeSSID(ssid);
_eepromConfig.writePassword(password);
}