-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_probe.ino
476 lines (358 loc) · 10.7 KB
/
wifi_probe.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "ESP8266WiFi.h"
#include "ArrayList.h"
#include "Mappings.h"
#include "RXSniffer.h"
#include "Util.h"
#include "WebPageStore.h"
#include <LittleFS.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#define LED_PIN 2
#define RESET_SWITCH 0
#define CHANNEL_HOP_INTERVAL_MS 2000
static os_timer_t channelHop_timer;
#define DISABLE 0
#define ENABLE 1
long rebootmillis = 0;
ESP8266WebServer server(80);
boolean setupMode = false;
#define DATA_LENGTH 112
#define TYPE_MANAGEMENT 0x00
#define TYPE_CONTROL 0x01
#define TYPE_DATA 0x02
#define SUBTYPE_PROBE_REQUEST 0x04
String cdevid = "?????";
String wifiname = "";
String wifipass = "";
String room = "";
IPAddress ip;
int port = 29000;
int minrssi = 80;
int checktime = 10;
long lastdata = 0;
ArrayList<String> devices;
HashMap<String, int> rssimap;
void initLittleFS()
{
bool initok = false;
initok = LittleFS.begin();
if (!(initok))
{
Serial.println("Format LittleFS");
LittleFS.format();
initok = LittleFS.begin();
}
if (!(initok))
{
LittleFS.format();
initok = LittleFS.begin();
}
if (!initok) {
Serial.println("Error while reading LittleFS! Halting System...");
while (true) {
delay(10000);
}
} else {
if (!(LittleFS.exists ("/devid"))) {
Serial.print("Setting new DevID... ");
File devidfile = LittleFS.open("/devid", "w");
if (!devidfile) {
Serial.println("Error while reading LittleFS! Halting System...");
while (true) {
delay(10000);
}
}
char devid[7];
getRandomStrF(devid, 7);
devidfile.println(devid);
devidfile.close();
Serial.println(((String) "[OK] DevID is ") + devid);
cdevid = devid;
} else {
Serial.print("Loading DevID... ");
File devidfile = LittleFS.open("/devid", "r");
String devid = devidfile.readStringUntil('\n');
devid.trim();
devidfile.close();
Serial.println("[OK] DevID is " + devid);
cdevid = devid;
}
}
}
static void showMetadata(SnifferPacket *snifferPacket) {
unsigned int frameControl = ((unsigned int)snifferPacket->data[1] << 8) + snifferPacket->data[0];
uint8_t version = (frameControl & 0b0000000000000011) >> 0;
uint8_t frameType = (frameControl & 0b0000000000001100) >> 2;
uint8_t frameSubType = (frameControl & 0b0000000011110000) >> 4;
uint8_t toDS = (frameControl & 0b0000000100000000) >> 8;
uint8_t fromDS = (frameControl & 0b0000001000000000) >> 9;
if (frameType != TYPE_MANAGEMENT ||
frameSubType != SUBTYPE_PROBE_REQUEST) {
return;
}
Serial.print("RSSI: ");
Serial.print(snifferPacket->rx_ctrl.rssi, DEC);
Serial.print(" Channel: ");
Serial.print(wifi_get_channel());
char addr[] = "00:00:00:00:00:00";
getMAC(addr, snifferPacket->data, 10);
Serial.print(" MAC: ");
Serial.print(addr);
uint8_t SSID_length = snifferPacket->data[25];
Serial.print(" SSID: ");
printDataSpan(26, SSID_length, snifferPacket->data);
Serial.println();
int rssi = snifferPacket->rx_ctrl.rssi;
if (rssi < 0) {
rssi = -rssi;
}
if (rssi <= minrssi) {
if (!devices.contains((String) addr)) {
devices.add((String) addr);
}
rssimap.put((String) addr, rssi);
}
}
static void ICACHE_FLASH_ATTR sniffer_callback(uint8_t *buffer, uint16_t length) {
struct SnifferPacket *snifferPacket = (struct SnifferPacket*) buffer;
showMetadata(snifferPacket);
}
static void printDataSpan(uint16_t start, uint16_t size, uint8_t* data) {
for (uint16_t i = start; i < DATA_LENGTH && i < start + size; i++) {
Serial.write(data[i]);
}
}
static void getMAC(char *addr, uint8_t* data, uint16_t offset) {
sprintf(addr, "%02x:%02x:%02x:%02x:%02x:%02x", data[offset + 0], data[offset + 1], data[offset + 2], data[offset + 3], data[offset + 4], data[offset + 5]);
}
void channelHop()
{
uint8 new_channel = wifi_get_channel() + 1;
if (new_channel > 12) {
new_channel = 1;
}
wifi_set_channel(new_channel);
}
void enableLED() {
digitalWrite(LED_PIN, 1);
}
void disableLED() {
digitalWrite(LED_PIN, 0);
}
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(RESET_SWITCH, INPUT);
pinMode(LED_PIN, OUTPUT);
disableLED();
initLittleFS();
if (checkFirstStart()) {
setupMode = true;
return;
}
WiFi.mode(WIFI_STA);
delay(10);
wifi_set_opmode(STATION_MODE);
wifi_set_channel(1);
wifi_promiscuous_enable(DISABLE);
delay(10);
wifi_set_promiscuous_rx_cb(sniffer_callback);
delay(10);
wifi_promiscuous_enable(ENABLE);
os_timer_disarm(&channelHop_timer);
os_timer_setfn(&channelHop_timer, (os_timer_func_t *) channelHop, NULL);
os_timer_arm(&channelHop_timer, CHANNEL_HOP_INTERVAL_MS, 1);
WiFi.hostname("SchemilWifiProbe-" + cdevid);
}
void loop() {
if (rebootmillis != 0) {
if (millis() > rebootmillis) {
ESP.restart();
return;
}
}
if (setupMode) {
server.handleClient();
return;
}
if (lastdata == 0) {
lastdata = millis();
}
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(wifiname, wifipass);
}
// if ((lastdata + (20000)) < millis()) {
if ((lastdata + (checktime * 60000)) < millis()) {
lastdata = millis();
int devs = devices.size();
WiFi.persistent(false);
delay(20);
// WiFi.setAutoReconnect(false);
delay(20);
WiFi.mode(WIFI_STA);
delay(100);
WiFi.disconnect();
delay(100);
WiFi.scanDelete();
delay(50);
WiFi.scanNetworks();
wifi_promiscuous_enable(DISABLE);
os_timer_disarm(&channelHop_timer);
delay(200);
Serial.println();
Serial.print("Connecting to WiFi ...");
int i = 0;
WiFi.hostname("SchemilWifiProbe-" + cdevid);
WiFi.begin(wifiname, wifipass);
// while (WiFi.status() != WL_CONNECTED)
while (WiFi.status() != WL_CONNECTED)
{
delay(500); //100
Serial.print(".");
yield();
i++;
if (i > 500) {
Serial.println();
Serial.println("Connection lost.");
// WiFi.printDiag(Serial);
Serial.println(WiFi.status());
Serial.println("0 WiFi is in process of changing between statuses");
Serial.println("1 SSID cannot be reached");
Serial.println("2 Scan Completed");
Serial.println("3 Successful connection is established");
Serial.println("4 Password is incorrect");
Serial.println("5 Connection Lost");
Serial.println("6 Module is disconnected or not configured in station mode");
recheckWiFi();
return;
}
}
delay(100);
Serial.println();
Serial.println("WiFi connected.");
Serial.println(WiFi.localIP());
delay(500);
WiFiClient client;
client.connect(ip, port);
client.println("WiFiProbe V0.1.1");
client.println(room);
client.println(String(devs));
String currentdev;
for (int i = 0; i < devices.size(); i++) {
currentdev = devices[i];
client.println(currentdev);
client.println(String(rssimap.get(currentdev)));
}
client.stop();
delay(200);
WiFi.disconnect();
delay(500);
recheckWiFi();
}
if (digitalRead(RESET_SWITCH) == 0) {
int i = 0;
int maxtime = 100;
while (digitalRead(RESET_SWITCH) == 0 && i < maxtime) {
enableLED();
delay(50);
disableLED();
delay(50);
i++;
Serial.println(((String) "Resetbutton pressed. ") + i + ((String) "/") + maxtime);
}
if (i < maxtime) {
return;
}
Serial.println("Resetting... ");
LittleFS.format();
ESP.eraseConfig();
File devidfile = LittleFS.open("/devid", "w");
if (!devidfile) {
Serial.println("Error while reading LittleFS! Halting System...");
while (true) {
delay(10000);
}
}
devidfile.println(cdevid);
devidfile.close();
ESP.restart();
while (true) {
}
}
}
boolean checkFirstStart() {
if ((LittleFS.exists ("/wifidata"))) {
File wififile = LittleFS.open("/wifidata", "r");
String wifinamer = wififile.readStringUntil('\n');
String wifipassr = wififile.readStringUntil('\n');
String ipr = wififile.readStringUntil('\n');
String portr = wififile.readStringUntil('\n');
String roomr = wififile.readStringUntil('\n');
String checktimer = wififile.readStringUntil('\n');
String minrssir = wififile.readStringUntil('\n');
wifinamer.trim();
wifipassr.trim();
ipr.trim();
portr.trim();
roomr.trim();
checktimer.trim();
minrssir.trim();
wifiname = wifinamer;
wifipass = wifipassr;
ip.fromString(ipr);
port = portr.toInt();
room = roomr;
checktime = checktimer.toInt();
minrssi = minrssir.toInt();
wififile.close();
return false;
}
WiFi.mode(WIFI_AP);
WiFi.persistent(false);
WiFi.softAP("WifiProberSetup-" + cdevid);
delay(500);
server.on("/", []() {
server.send(200, "text/html", setuppage);
});
server.on("/wifisave", []() {
if (!server.hasArg("checktime") || !server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("room") || !server.hasArg("ip") || !server.hasArg("port") || !server.hasArg("rssi")) {
server.send(200, "text/plain", "Es fehlen Argumente!");
return;
}
String ssid = server.arg("ssid");
String checktime = server.arg("checktime");
String room = server.arg("room");
String password = server.arg("password");
String ip = server.arg("ip");
String port = server.arg("port");
String rssi = server.arg("rssi");
File wififile = LittleFS.open("/wifidata", "w");
wififile.println(ssid);
wififile.println(password);
wififile.println(ip);
wififile.println(port);
wififile.println(room);
wififile.println(checktime);
wififile.println(rssi);
wififile.close();
server.send(200, "text/html", successfullywritten);
rebootmillis = millis() + 5000;
});
server.begin();
return true;
}
void recheckWiFi() {
delay(10);
wifi_set_opmode(STATION_MODE);
wifi_set_channel(1);
wifi_promiscuous_enable(DISABLE);
delay(10);
wifi_set_promiscuous_rx_cb(sniffer_callback);
delay(10);
wifi_promiscuous_enable(ENABLE);
os_timer_disarm(&channelHop_timer);
os_timer_setfn(&channelHop_timer, (os_timer_func_t *) channelHop, NULL);
os_timer_arm(&channelHop_timer, CHANNEL_HOP_INTERVAL_MS, 1);
devices.clear();
rssimap.clear();
}