-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicWifi.cpp
156 lines (145 loc) · 4.2 KB
/
DynamicWifi.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
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
#include "DynamicWifi.h"
DynamicWifi::DynamicWifi(char* ssid)
:server(new WiFiServer(80)) {
WiFi.disconnect(true);
WiFi.softAP(ssid);
debug_dw_printf("Created network '%s'\n", ssid);
debug_dw_print("IP: ");
debug_dw_println(WiFi.softAPIP());
server->begin();
debug_dw_println("Server ready");
}
DynamicWifi::~DynamicWifi() {
delete server;
WiFi.softAPdisconnect(true);
debug_dw_println("Wifi off");
}
bool DynamicWifi::tryConfigure() {
WiFiClient client = server->available();
if (client) {
debug_dw_println("Client connected");
while (client.connected()) {
if (client.available()) {
String verb = client.readStringUntil(' ');
String path = client.readStringUntil(' ');
String protocol = client.readStringUntil('\r');
client.read();
{
String header;
debug_dw_println("Headers:");
do {
header = client.readStringUntil('\r');
client.read();
debug_dw_println(header);
} while (header.length() > 0);
}
String body = client.readString();
debug_dw_print("Verb: ");
debug_dw_println(verb);
debug_dw_print("Path: ");
debug_dw_println(path);
debug_dw_print("Protocol: ");
debug_dw_println(protocol);
debug_dw_print("Body: ");
debug_dw_println(body);
if (protocol != "HTTP/1.1") {
status(client, 505); // HTTP Version Not Supported
break;
}
// Only accept requests on root path
if (path != "/") {
status(client, 404);
debug_dw_print("Path '");
debug_dw_print(path);
debug_dw_println("' not allowed'");
break;
}
if (verb.equals("GET")) {
handleGet(client);
} else if (verb.equals("POST")) {
handlePost(client, body);
} else {
status(client, 405); // Method not allowed
}
break;
}
}
client.flush();
client.stop();
debug_dw_println("\nClient disconnected");
}
return WiFi.status() == WL_CONNECTED;
}
void DynamicWifi::handleGet(WiFiClient client) {
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"\r\n" +
"<!DOCTYPE HTML>" +
"<html>" +
"<body>"
"<form method=\"post\">"
"SSID:<br>"
"<input type=\"text\" name=\"ssid\">"
"<br>"
"Password:<br>"
"<input type=\"password\" name=\"password\">"
"<br>"
"<input type=\"submit\" value=\"Submit\">"
"</form>"
"</body>"
"</html>" +
"\r\n";
client.print(htmlPage);
}
void DynamicWifi::handlePost(WiFiClient client, String body) {
int i = 0;
do {
int indexOfAmp = body.indexOf('&', i);
String keyVal = "";
if (indexOfAmp > 0) {
keyVal = body.substring(i, indexOfAmp);
i = indexOfAmp + 1;
debug_dw_println("PreKey");
} else {
keyVal = body.substring(i, body.length());
debug_dw_println("PostKey");
i = body.length();
}
debug_dw_print("KeyVal: ");
debug_dw_println(keyVal);
if (keyVal.startsWith("ssid=")) {
ssid = keyVal.substring(5, keyVal.length());
} else if (keyVal.startsWith("password=")) {
password = keyVal.substring(9, keyVal.length());
} else {
debug_dw_print("Ignoring ");
debug_dw_println(keyVal);
}
} while(i < body.length());
decodeFields();
WiFi.begin(ssid.c_str(), password.c_str());
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
status(client, 200);
client.print("Connected to '");
client.print(ssid);
client.write('\'');
} else {
status(client, 400);
client.print("Could not connect to '");
client.print(ssid);
client.print("'. (");
client.print(WiFi.status());
client.print(")");
}
}
void DynamicWifi::status(WiFiClient client, short status) {
client.print("HTTP/1.1 ");
client.print(status);
client.print(" \r\n\r\n");
}
void DynamicWifi::decodeFields() {
ssid.replace('+', ' ');
password.replace('+', ' ');
}