-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFiScan.ino
123 lines (108 loc) · 3.32 KB
/
WiFiScan.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
/* AP datalogger
The circuit:
ESP8266
SD card attached to SPI bus as follows:
** MOSI - pin 13
** MISO - pin 12
** CLK - pin 14
** CS - pin 15
*/
#include "ESP8266WiFi.h"
#include <SPI.h>
#include <SD.h>
#define LED 2
const short chipSelect = 15;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
//WiFi.disconnect();
delay(100);
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.println("Setup done");
}
void loop() {
// make a string for assembling the data to log:
short count = 10;
//Serial.println("READY");
//delay (1000);
while (Serial.available()) {
String xy = Serial.readString();
while (count > 0) {
count --;
String dataString = "";
dataString += xy;
dataString.remove(dataString.length() - 1);
dataString += ", 2.39,";
// Serial.println("scan start");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
// Serial.println("scan done");
if (n == 0)
Serial.println("no networks found");
else
{
// sort by RSSI
int indices[n];
for (int i = 0; i < n; i++) {
indices[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
// Serial.print("WiFi.RSSI(indices[j] = ");
// Serial.print(WiFi.RSSI(indices[j]));
// Serial.print("WiFi.RSSI(indices[i] = ");
// Serial.println(WiFi.RSSI(indices[i]));
if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
// int temp = indices[j];
// indices[j] = indices[i];
// indices[i] = temp;
std::swap(indices[i], indices[j]);
}
}
}
// Serial.print(n);
// Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
// Serial.print(WiFi.RSSI(i));
// Serial.print(" ");
dataString += String(WiFi.RSSI(indices[i]));
dataString += String(" ");
delay(10);
// Serial.print(WiFi.BSSIDstr(i));
dataString += String(WiFi.BSSIDstr(indices[i]));
dataString += String(",");
// Serial.print(",");
delay(10);
}
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataString.remove(dataString.length() - 1);
dataString += String(n);
// print to the serial port too:
Serial.println(dataString);
digitalWrite(LED, HIGH);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
// Serial.println("");
// Wait a bit before scanning again
// delay(200);
if (count == 0) // Serial.println("READY");
digitalWrite(LED, LOW);
}
}
}