-
Notifications
You must be signed in to change notification settings - Fork 29
/
WiFi_portal.ino
246 lines (211 loc) · 7.37 KB
/
WiFi_portal.ino
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Simple web configuration portal
When it is connected to a WiFi network, everything is fine
When connection fails, it create a portal to insert a new SSID and password,
If the new credentials works, it will be stored in ESP32 non volatile storage (NVS).
In a nutshell: it's a https://github.com/tzapu/WiFiManager for ESP32 with a non blocking implementation and simple design.
Connect to wifi network generated by ESP32 and tip in your browser:
AP IPv4: 192.168.4.1 or AP IPv6
by Evandro Luis Copercini - 2017
Public Domain
*/
/*
TODO: integrate with https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiScan/WiFiScan.ino
*/
#include "WiFi.h"
#include <Preferences.h>
#define AP_SSID "esp32" //can set ap hostname here
WiFiServer server(80);
Preferences preferences;
static volatile bool wifi_connected = false;
String wifiSSID, wifiPassword;
void WiFiEvent(WiFiEvent_t event)
{
switch (event) {
case SYSTEM_EVENT_AP_START:
//can set ap hostname here
WiFi.softAPsetHostname(AP_SSID);
//enable ap ipv6 here
WiFi.softAPenableIpV6();
break;
case SYSTEM_EVENT_STA_START:
//set sta hostname here
WiFi.setHostname(AP_SSID);
break;
case SYSTEM_EVENT_STA_CONNECTED:
//enable sta ipv6 here
WiFi.enableIpV6();
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
//both interfaces get the same event
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
Serial.print("AP IPv6: ");
Serial.println(WiFi.softAPIPv6());
break;
case SYSTEM_EVENT_STA_GOT_IP:
wifiOnConnect();
wifi_connected = true;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
wifi_connected = false;
wifiOnDisconnect();
break;
default:
break;
}
}
String urlDecode(const String& text)
{
String decoded = "";
char temp[] = "0x00";
unsigned int len = text.length();
unsigned int i = 0;
while (i < len)
{
char decodedChar;
char encodedChar = text.charAt(i++);
if ((encodedChar == '%') && (i + 1 < len))
{
temp[2] = text.charAt(i++);
temp[3] = text.charAt(i++);
decodedChar = strtol(temp, NULL, 16);
}
else {
if (encodedChar == '+')
{
decodedChar = ' ';
}
else {
decodedChar = encodedChar; // normal ascii char
}
}
decoded += decodedChar;
}
return decoded;
}
void setup()
{
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(AP_SSID);
Serial.println("AP Started");
Serial.print("AP SSID: ");
Serial.println(AP_SSID);
Serial.print("AP IPv4: ");
Serial.println(WiFi.softAPIP());
preferences.begin("wifi", false);
wifiSSID = preferences.getString("ssid", "none"); //NVS key ssid
wifiPassword = preferences.getString("password", "none"); //NVS key password
preferences.end();
Serial.print("Stored SSID: ");
Serial.println(wifiSSID);
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
server.begin();
}
void loop()
{
if (wifi_connected) {
wifiConnectedLoop();
} else {
wifiDisconnectedLoop();
}
}
//when wifi connects
void wifiOnConnect()
{
Serial.println("STA Connected");
Serial.print("STA SSID: ");
Serial.println(WiFi.SSID());
Serial.print("STA IPv4: ");
Serial.println(WiFi.localIP());
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
WiFi.mode(WIFI_MODE_STA); //close AP network
}
//when wifi disconnects
void wifiOnDisconnect()
{
Serial.println("STA Disconnected");
delay(1000);
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
}
//while wifi is connected
void wifiConnectedLoop()
{
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
delay(1000);
}
void wifiDisconnectedLoop()
{
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("New client"); // print a message out the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<form method='get' action='a'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
continue;
}
if (currentLine.startsWith("GET /a?ssid=") ) {
//Expecting something like:
//GET /a?ssid=blahhhh&pass=poooo
Serial.println("");
Serial.println("Cleaning old WiFi credentials from ESP32");
// Remove all preferences under opened namespace
preferences.clear();
String qsid;
qsid = urlDecode(currentLine.substring(12, currentLine.indexOf('&'))); //parse ssid
Serial.println(qsid);
Serial.println("");
String qpass;
qpass = urlDecode(currentLine.substring(currentLine.lastIndexOf('=') + 1, currentLine.lastIndexOf(' '))); //parse password
Serial.println(qpass);
Serial.println("");
preferences.begin("wifi", false); // Note: Namespace name is limited to 15 chars
Serial.println("Writing new ssid");
preferences.putString("ssid", qsid);
Serial.println("Writing new pass");
preferences.putString("password", qpass);
delay(300);
preferences.end();
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<h1>OK! Restarting in 5 seconds...</h1>");
client.println();
Serial.println("Restarting in 5 seconds...");
delay(5000);
ESP.restart();
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}