-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFeatherScanLogger
477 lines (344 loc) · 9.35 KB
/
FeatherScanLogger
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
#include "ESP8266WiFi.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SD.h>
#include "RTClib.h"
//rtc related setup
RTC_PCF8523 rtc;
//oled feather related stuff.
#define OLED_RESET 3
Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);
#define BUTTON_A 0
#define BUTTON_B 16
#define BUTTON_C 2
// Button state counter
int stateA = 0;
int stateB = 0;
int stateC = 0;
//wifi scan related stuff
int netNum = 0;
int totalNet = 0;
typedef struct Single_Network {
String SSID;
uint8_t encryptionType;
int32_t RSSI;
uint8_t* BSSID;
String BSSIDstr;
int32_t channel;
bool isHidden;
};
// Holds up to 50 networks... figured it should be enough for 1 scan.
Single_Network Network[50];
//datalogger related stuff
//Setup chip set for logger feather.
const int chipSelect = 15;
void setup() {
//RTC related setup :
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.initialized()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
DateTime now = rtc.now();
Serial.println(now.unixtime());
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.begin(9600); // Display to serial monitor
Serial.println("Setup begins....");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
pinMode(BUTTON_A, INPUT_PULLUP);
pinMode(BUTTON_B, INPUT_PULLUP);
pinMode(BUTTON_C, INPUT_PULLUP);
delay(100);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer.5
display.clearDisplay();
delay(500);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println("1:Scan");
display.println("2:Continuous Scan ");
display.println("3:Top Dog SSID");
display.print("EPOCH:");
display.println(now.unixtime());
display.display();
delay(1000);
}
//Main Scan for networks function
void scanNets(void) {
netNum = 0;
totalNet = 0;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Scanning");
display.display();
WiFi.scanNetworksAsync(prinScanResult, 1);
while (WiFi.scanComplete () < 0 ) {
display.print(".");
display.display();
delay(500);
}
display.clearDisplay();
display.setCursor(0, 0);
display.println("Scan done");
Serial.println("scan done");
display.println();
display.println("Press C for result");
display.display();
}
//Function to write the results to the files after a scan.
void prinScanResult(int networksFound)
{
DateTime now = rtc.now();
String uTime = String(now.unixtime());
Serial.printf("%d network(s) found\n", networksFound);
totalNet = networksFound;
for (int i = 0; i < networksFound; i++)
{
File openFile = SD.open("open.txt", FILE_WRITE);
File allFile = SD.open("all.txt", FILE_WRITE);
Serial.printf("%d: %s, Ch:%d (%ddBm) %s %s\n", i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "", WiFi.isHidden(i) ? "hidden" : "");
if (WiFi.isHidden(i)) {
Network[i].SSID = "* Hidden";
} else {
Network[i].SSID = WiFi.SSID(i).c_str();
}
Network[i].channel = WiFi.channel(i);
Network[i].RSSI = WiFi.RSSI(i);
Network[i].encryptionType = WiFi.encryptionType(i);
Network[i].BSSID = WiFi.BSSID(i);
Network[i].BSSIDstr = WiFi.BSSIDstr(i);
// Write found SSIDs to the files on the root of the sd card.
if (openFile) {
// Log OPEN files.
if (WiFi.encryptionType(i) == ENC_TYPE_NONE)
{
openFile.print(uTime);
openFile.print(",");
openFile.print(Network[i].SSID);
openFile.print(",");
openFile.print(Network[i].BSSIDstr);
openFile.print(",");
openFile.print( Network[i].channel);
openFile.print("ch");
openFile.print(",");
openFile.print("OPEN");
openFile.println();
openFile.close();
}
}
// Log EVERYTHING files.
if (allFile) {
allFile.print(uTime);
allFile.print(",");
allFile.print(Network[i].SSID);
allFile.print(",");
allFile.print(Network[i].BSSIDstr);
allFile.print(",");
allFile.print( Network[i].channel);
allFile.print("ch");
allFile.print(",");
allFile.print(EncryptionType(Network[i].encryptionType));
allFile.println();
allFile.close();
}
}
}
//Convert EncryptionType Byte to human readable form.
String EncryptionType(uint8_t encType)
{
String encryptType;
switch (encType)
{
case 4:
{
encryptType = "CCMP(WPA)";
break;
}
case 5:
{
encryptType = "WEP";
break;
}
case 2:
{
encryptType = "TKIP(WPA)";
break;
}
case 7:
{
encryptType = "None";
break;
}
case 8:
{
encryptType = "Auto";
break;
}
default:
{
encryptType = "Other";
break;
}
}
return encryptType;
}
// Display the details of 1 network.
void showNetwork(void)
{
Serial.print("display network:");
Serial.print(netNum);
Serial.print("/");
Serial.println(totalNet);
display.clearDisplay();
display.setCursor(0, 0);
display.print(netNum + 1);
display.print("/");
display.print(totalNet);
display.print(":");
display.println(Network[netNum].SSID);
display.println();
display.print("Ch:");
display.print(Network[netNum].channel);
display.println(" (" + String(Network[netNum].RSSI) + " dBm)");
display.print("Auth:" + EncryptionType(Network[netNum].encryptionType));
display.display();
//Increment or round robin the counter to that the scroll is round robined
if (netNum == totalNet - 1) {
netNum = 0;
} else {
netNum++;
}
}
// Perform continuous scan
void contScan(void) {
while (true) {
int sN = 0;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Scanning cont");
boolean writing = false;
if (writing == false) {
//Psuedo progress bar-ish.
if (sN == 77) {
display.clearDisplay();
display.display();
display.setCursor(0, 0);
display.print("Scanning");
sN = 0;
}
WiFi.scanNetworksAsync(prinScanResult, true);
while (WiFi.scanComplete() < 0 ) {
display.print(".");
display.display();
delay(500);
}
}
if (WiFi.scanComplete() >= 0) {
writing = true;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Writing results");
delay (5000);
}
display.print(".");
sN++;
// Serial.println(sN);
display.display();
delay(500);
}
WiFi.scanDelete();
}
void topDog(void) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Finding Top Dog SSID");
display.display();
int n = WiFi.scanNetworks(false, true);
String ssid;
uint8_t encryptionType;
int32_t RSSI;
uint8_t* BSSID;
int32_t channel;
bool isHidden;
int topDog_rssi = -100;
int topDog;
for (int i = 0; i < n; i++)
{
WiFi.getNetworkInfo(i, ssid, encryptionType, RSSI, BSSID, channel, isHidden);
Serial.printf("%d: %s, Ch:%d (%ddBm) %s %s\n", i + 1, ssid.c_str(), channel, RSSI, encryptionType == ENC_TYPE_NONE ? "open" : "", isHidden ? "hidden" : "");
if (WiFi.RSSI(i) > topDog_rssi) {
topDog = i ;
topDog_rssi = WiFi.RSSI(i);
if (WiFi.isHidden(i)) {
Network[topDog].SSID = "* Hidden";
} else {
Network[topDog].SSID = WiFi.SSID(i).c_str();
}
Network[topDog].channel = WiFi.channel(i);
Network[topDog].RSSI = WiFi.RSSI(i);
Network[topDog].encryptionType = WiFi.encryptionType(i);
Network[topDog].BSSID = WiFi.BSSID(i);
}
}
display.clearDisplay();
delay(500);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(Network[topDog].SSID);
display.println();
display.println("Ch:" + String(Network[topDog].channel) + " " + "(" + String(Network[topDog].RSSI) + " dBM)" );
display.println("Auth:" + EncryptionType(Network[topDog].encryptionType));
display.print("MAC:");
display.print((char*)(Network[topDog].BSSID));
display.display();
}
void loop() {
if (!digitalRead(BUTTON_A)) {
stateA = 1;
stateB = 1;
stateC = 1;
scanNets();
}
if (!digitalRead(BUTTON_B)) {
if (stateB == 0) {
contScan();
}
}
if (!digitalRead(BUTTON_C)) {
Serial.println("State C" + stateC);
if (stateC == 1) {
showNetwork();
}
if (stateC == 0) {
topDog();
Serial.println("top dogging");
}
}
delay(200);
yield();
display.display();
}