-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSub.ino
271 lines (224 loc) · 6.75 KB
/
Sub.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
#include <esp_now.h>
#include <WiFi.h>
// Keep as all zeroes so we do a broadcast
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t peerInfo;
//say how many macs we should keep in the buffer to compare for uniqueness
#define mac_history_len 512
struct mac_addr {
unsigned char bytes[6];
};
struct mac_addr mac_history[mac_history_len];
unsigned int mac_history_cursor = 0;
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
char bssid[64];
char ssid[32];
char encryptionType[16];
int32_t channel;
int32_t rssi;
int boardID;
} struct_message;
//**********
//**********
//change the boardID for every unit you flash, this also sets the channel that the board will scan on
int boardID = 1;
String AP;
String BSSIDchar;
String ENC;
String EncTy;
// Create a struct_message called myData
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 200; // send readings timer
// Callback when data is sent
void OnDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void save_mac(unsigned char* mac) {
//Save a MAC address into the recently seen array.
if (mac_history_cursor >= mac_history_len) {
mac_history_cursor = 0;
}
struct mac_addr tmp;
for (int x = 0; x < 6; x++) {
tmp.bytes[x] = mac[x];
}
mac_history[mac_history_cursor] = tmp;
mac_history_cursor++;
Serial.print("Mac len ");
Serial.println(mac_history_cursor);
}
boolean seen_mac(unsigned char* mac) {
//Return true if this MAC address is in the recently seen array.
struct mac_addr tmp;
for (int x = 0; x < 6; x++) {
tmp.bytes[x] = mac[x];
}
for (int x = 0; x < mac_history_len; x++) {
if (mac_cmp(tmp, mac_history[x])) {
return true;
}
}
return false;
}
void print_mac(struct mac_addr mac) {
//Print a mac_addr struct nicely.
for (int x = 0; x < 6; x++) {
Serial.print(mac.bytes[x], HEX);
Serial.print(":");
}
}
boolean mac_cmp(struct mac_addr addr1, struct mac_addr addr2) {
//Return true if 2 mac_addr structs are equal.
for (int y = 0; y < 6; y++) {
if (addr1.bytes[y] != addr2.bytes[y]) {
return false;
}
}
return true;
}
String security_int_to_string(int security_type) {
//Provide a security type int from WiFi.encryptionType(i) to convert it to a String which Wigle CSV expects.
String authtype = "";
switch (security_type) {
case WIFI_AUTH_OPEN:
authtype = "[OPEN]";
break;
case WIFI_AUTH_WEP:
authtype = "[WEP]";
break;
case WIFI_AUTH_WPA_PSK:
authtype = "[WPA_PSK]";
break;
case WIFI_AUTH_WPA2_PSK:
authtype = "[WPA2_PSK]";
break;
case WIFI_AUTH_WPA_WPA2_PSK:
authtype = "[WPA_WPA2_PSK]";
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
authtype = "[WPA2]";
break;
//Requires at least v2.0.0 of https://github.com/espressif/arduino-esp32/
case WIFI_AUTH_WPA3_PSK:
authtype = "[WPA3_PSK]";
break;
case WIFI_AUTH_WPA2_WPA3_PSK:
authtype = "[WPA2_WPA3_PSK]";
default:
authtype = "";
}
return authtype;
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
pinMode(2, OUTPUT); //setup built in led
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
//esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
//Serial.println("Starting");
char Buf[50];
char bufBSSID[64];
char BufEnc[50];
if ((millis() - lastTime) > timerDelay) {
// Set values to send
//myData.b = random(1,20);
//myData.c = 1.2;
int n = WiFi.scanNetworks(false, true, false, 500, boardID);
if (n == 0) {
Serial.println("No networks found");
Serial.println("No networks found");
} else {
for (int8_t i = 0; i < n; i++) {
//delay(10);
if (seen_mac(WiFi.BSSID(i))) {
Serial.println("We've already seen it");
//BSSIDchar = WiFi.BSSID(i);
//BSSIDchar.toCharArray(bufBSSID, 64);
//strcpy(myData.bssid, Buf);
//Serial.println(myData.bssid);
Serial.println(myData.boardID);
continue;
}
Serial.println("We havent seen it");
String MacString = WiFi.BSSIDstr(i).c_str();
//myData.bssid = MacString;
MacString.toCharArray(bufBSSID, 64);
strcpy(myData.bssid, bufBSSID);
Serial.println(myData.bssid);
//myData.bssid = WiFi.BSSID(i);
//Serial.print("MyData.bssid: ");
//Serial.println(myData.bssid);
String AP = WiFi.SSID(i);
AP.toCharArray(Buf, 50);
strcpy(myData.ssid, Buf);
Serial.print("SSID: ");
Serial.println(myData.ssid);
//String ENC = security_int_to_string(WiFi.encryptionType(i));
//ENC.toCharArray(BufEnc, 32);
//strcpy(myData.encryptionType, BufEnc);
//myData.encryptionType = authtype;
switch (WiFi.encryptionType(i)) {
case WIFI_AUTH_OPEN:
EncTy = "Open";
break;
case WIFI_AUTH_WEP:
EncTy = "WEP";
break;
case WIFI_AUTH_WPA_PSK:
EncTy = "WPA PSK";
break;
case WIFI_AUTH_WPA2_PSK:
EncTy = "WPA2 PSK";
break;
case WIFI_AUTH_WPA_WPA2_PSK:
EncTy = "WPA/WPA2 PSK";
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
EncTy = "WPA2 Enterprise";
break;
default:
EncTy = "Unknown";
break;
}
EncTy.toCharArray(BufEnc, 16);
strcpy(myData.encryptionType, BufEnc);
Serial.print("Encryption: ");
Serial.println(myData.encryptionType);
myData.channel = WiFi.channel(i);
myData.rssi = WiFi.RSSI(i);
myData.boardID = boardID; //YOU NEED TO CHANGE THE BOARDID TO BE UNIQUE FOR EVERY SUB BVEFORE YOU FLASH IT. DONT DO IT HERE THOUGH
Serial.println(myData.boardID);
save_mac(WiFi.BSSID(i));
esp_now_send(broadcastAddress, (uint8_t*)&myData, sizeof(myData));
//digitalWrite(2, LOW);
delay(200);
//digitalWrite(2, HIGH);
}
lastTime = millis();
}
}
}