-
Notifications
You must be signed in to change notification settings - Fork 6
/
wifi.ino
275 lines (257 loc) · 8.17 KB
/
wifi.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
boolean scanAndConnectToLocalSCADS() {
boolean foundLocalSCADS = false;
// WiFi.scanNetworks will return the number of networks found
Serial.println("scan start");
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
delay(10);
String networkSSID = WiFi.SSID(i);
if (networkSSID.length() <= SSID_MAX_LENGTH) {
scads_ssid = WiFi.SSID(i);
if (scads_ssid.indexOf("Yo-Yo-") > -1) {
Serial.println("Found YOYO");
foundLocalSCADS = true;
wifiMulti.addAP(scads_ssid.c_str(), scads_pass.c_str());
while ((wifiMulti.run() != WL_CONNECTED)) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
} else {
// SSID too long
Serial.println("SSID too long for use with current ESP-IDF");
}
}
}
return (foundLocalSCADS);
}
void createSCADSAP() {
//Creates Access Point for other device to connect to
scads_ssid = "Yo-Yo-" + generateID();
Serial.print("Wifi name:");
Serial.println(scads_ssid);
WiFi.mode(WIFI_AP);
delay(2000);
WiFi.persistent(false);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(scads_ssid.c_str(), scads_pass.c_str());
IPAddress myIP = WiFi.softAPIP();
Serial.println(myIP);
}
void connectToWifi(String credentials) {
WiFi.persistent(false);
String _wifiCredentials = credentials;
const size_t capacity = 2 * JSON_ARRAY_SIZE(6) + JSON_OBJECT_SIZE(2) + 150;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, _wifiCredentials);
JsonArray ssid = doc["ssid"];
JsonArray pass = doc["password"];
if (ssid.size() > 0) {
for (int i = 0; i < ssid.size(); i++) {
wifiMulti.addAP(checkSsidForSpelling(ssid[i]).c_str(), pass[i]);
}
} else {
Serial.println("issue with wifi credentials, creating access point");
}
Serial.println("Connecting to Router");
long wifiMillis = millis();
bool connectSuccess = false;
while (!connectSuccess) {
uint8_t currentStatus = wifiMulti.run();
//#ifdef DEV
Serial.print("Status: ");
switch (currentStatus) {
case WL_CONNECTED:
Serial.println("WL_CONNECTED");
break;
case WL_IDLE_STATUS:
Serial.println("WL_IDLE_STATUS");
break;
case WL_NO_SSID_AVAIL:
Serial.println("WL_NO_SSID_AVAIL");
break;
case WL_SCAN_COMPLETED:
Serial.println("WL_SCAN_COMPLETED");
break;
case WL_CONNECT_FAILED:
Serial.println("WL_CONNECT_FAILED");
break;
case WL_CONNECTION_LOST:
Serial.println("WL_CONNECTION_LOST");
break;
case WL_DISCONNECTED:
Serial.println("WL_DISCONNECTED");
break;
}
//#endif
if (currentStatus == WL_CONNECTED) {
// Connected!
connectSuccess = true;
Serial.println("Setting last connected");
Serial.println(WiFi.SSID());
setLastConnected(WiFi.SSID());
break;
}
if (millis() - wifiMillis > WIFICONNECTTIMEOUT) {
//Wifi credentials are potentially incorrect as not connected to a network before.
if (getLastConnected() == "") {
// Timeout, check if we're out of range.
// Wipe credentials and reset
Serial.println("Wifi connect failed, Please try your details again in the captive portal");
preferences.begin("scads", false);
preferences.putString("wifi", "");
preferences.end();
ESP.restart();
} else if (lastConnectedInNetworkList()) {
//Last connected network in the wifi scan, so is potentially network interference or dropped signal
Serial.println("Retrying network connect as last connected network is visible. Please try and move closer to your router");
ESP.restart();
} else {
//Last connected network not in the wifiScan, so most likely in a new location
if (currentStatus == WL_DISCONNECTED) {
Serial.println("Removing wifi credentials as the last connected network is not visible");
preferences.begin("scads", false);
preferences.putString("wifi", "");
preferences.end();
ESP.restart();
} else {
Serial.println("Seem to be having WiFi connection issues, Please try and move closer to you router");
ESP.restart();
}
}
}
delay(100);
yield();
Serial.print(".");
buttonBuiltIn.check();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
disconnected = false;
}
String checkSsidForSpelling(String incomingSSID) {
int n = WiFi.scanNetworks();
int currMatch = 255;
int prevMatch = currMatch;
int matchID;
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
Serial.println("can't find any wifi in the area");
return incomingSSID;
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
Serial.println(WiFi.SSID(i));
String networkSSID = WiFi.SSID(i);
if (networkSSID.length() <= SSID_MAX_LENGTH) {
currMatch = levenshteinIgnoreCase(incomingSSID.c_str(), WiFi.SSID(i).c_str()) < 2;
if (levenshteinIgnoreCase(incomingSSID.c_str(), WiFi.SSID(i).c_str()) < 2) {
if (currMatch < prevMatch) {
prevMatch = currMatch;
matchID = i;
}
}
} else {
// SSID too long
Serial.println("SSID too long for use with current ESP-IDF");
}
}
if (prevMatch != 255) {
Serial.println("Found a match!");
return WiFi.SSID(matchID);
} else {
Serial.println("can't find any wifi that are close enough matches in the area");
return incomingSSID;
}
}
}
void wifiCheck() {
if (millis() - wificheckMillis > wifiCheckTime) {
wificheckMillis = millis();
uint8_t currentStatusCheck = wifiMulti.run();
if (currentStatusCheck != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, 1);
disconnected = true;
} else {
disconnected = false;
}
}
}
bool isWifiValid(String incomingSSID) {
int n = WiFi.scanNetworks();
int currMatch = 255;
int prevMatch = currMatch;
int matchID;
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
Serial.println("can't find any wifi in the area");
return false;
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
Serial.println(WiFi.SSID(i));
String networkSSID = WiFi.SSID(i);
if (networkSSID.length() <= SSID_MAX_LENGTH) {
currMatch = levenshteinIgnoreCase(incomingSSID.c_str(), WiFi.SSID(i).c_str()) < 2;
if (levenshteinIgnoreCase(incomingSSID.c_str(), WiFi.SSID(i).c_str()) < 2) {
if (currMatch < prevMatch) {
prevMatch = currMatch;
matchID = i;
}
}
} else {
// SSID too long
Serial.println("SSID too long for use with current ESP-IDF");
}
}
if (prevMatch != 255) {
Serial.println("Found a match!");
return true;
} else {
Serial.println("can't find any wifi that are close enough matches in the area");
return false;
}
}
}
bool lastConnectedInNetworkList() {
int n = WiFi.scanNetworks();
if (n == 0) {
Serial.println("no networks found");
Serial.println("can't find any wifi in the area");
return false;
} else {
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i) {
Serial.println(WiFi.SSID(i));
if (getLastConnected() == WiFi.SSID(i)) {
return true;
}
}
return false;
}
}