-
Notifications
You must be signed in to change notification settings - Fork 8
/
WiFiManager.h
125 lines (118 loc) · 2.99 KB
/
WiFiManager.h
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
/*
* Function to handle unknown URLs
*/
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
/*
* Function for writing WiFi creds to EEPROM
* Returns: true if save successful, false if unsuccessful
*/
bool writeToMemory(String ssid, String pass){
char buff1[30];
char buff2[30];
ssid.toCharArray(buff1,30);
pass.toCharArray(buff2,30);
EEPROM.writeString(100,buff1);
EEPROM.writeString(200,buff2);
delay(100);
String s = EEPROM.readString(100);
String p = EEPROM.readString(200);
//#if DEBUG
Serial.print("Stored SSID, password, are: ");
Serial.print(s);
Serial.print(" / ");
Serial.print(p);
//#endif
if(ssid == s && pass == p){
return true;
}else{
return false;
}
}
/*
* Function for handling form
*/
void handleSubmit(){
String response_success="<h1>Success</h1>";
response_success +="<h2>Device will restart in 3 seconds</h2>";
String response_error="<h1>Error</h1>";
response_error +="<h2><a href='/'>Go back</a>to try again";
if(writeToMemory(String(server.arg("ssid")),String(server.arg("password")))){
server.send(200, "text/html", response_success);
EEPROM.commit();
delay(3000);
ESP.restart();
}else{
server.send(200, "text/html", response_error);
}
}
/*
* Function for home page
*/
void handleRoot() {
if (server.hasArg("ssid")&& server.hasArg("password")) {
handleSubmit();
}
else {
server.send(200, "text/html", INDEX_HTML);
}
}
/*
* Function for loading form
* Returns: false if no WiFi creds in EEPROM
*/
bool loadWIFICredsForm(){
String s = EEPROM.readString(100);
String p = EEPROM.readString(200);
//const char* ssid = "ESP32 WiFi Manager";
char APname[11];
sprintf(APname, "ESP_%X", ESP.getEfuseMac());
const char* password = "12345678";
Serial.println("Setting Access Point...");
WiFi.softAP(APname, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
while(s.length() <= 0 && p.length() <= 0){
server.handleClient();
delay(100);
}
return false;
}
/*
* Function checking WiFi creds in memory
* Returns: true if not empty, false if empty
*/
bool CheckWIFICreds(){
Serial.println("Checking WIFI credentials");
String s = EEPROM.readString(100);
String p = EEPROM.readString(200);
//#if DEBUG
Serial.print("Found credentials: ");
Serial.print(s);
Serial.print("/");
Serial.print(p);
delay(5000);
//#endif
if(s.length() > 0 && p.length() > 0){
return true;
}else{
return false;
}
}