Created
April 8, 2016 18:08
-
-
Save tablatronix/497f3b299e0f212fc171ac5662fa7b42 to your computer and use it in GitHub Desktop.
arduino esp8266 wifiscanner with detail, rssi sorting and duplicate filtering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This sketch demonstrates how to scan WiFi networks. | |
* The API is almost the same as with the WiFi Shield library, | |
* the most obvious difference being the different file you need to include: | |
*/ | |
#include "ESP8266WiFi.h" | |
void setup() { | |
Serial.begin(115200); | |
// Serial.setDebugOutput(true); | |
// Set WiFi to station mode and disconnect from an AP if it was previously connected | |
WiFi.mode(WIFI_STA); | |
WiFi.disconnect(); | |
delay(100); | |
Serial.println(""); | |
Serial.println("Setup done"); | |
Serial.println(""); | |
Serial.println("MAC: " + WiFi.macAddress()); | |
} | |
String encryptionTypeStr(uint8_t authmode) { | |
switch(authmode) { | |
case ENC_TYPE_NONE: | |
return "OPEN"; | |
case ENC_TYPE_WEP: | |
return "WEP"; | |
case ENC_TYPE_TKIP: | |
return "WPA_PSK"; | |
case ENC_TYPE_CCMP: | |
return "WPA2_PSK"; | |
case ENC_TYPE_AUTO: | |
return "WPA_WPA2_PSK"; | |
default: | |
return "UNKOWN"; | |
} | |
} | |
void loop() { | |
// Serial.println("scan start"); | |
// WiFi.scanNetworks will return the number of networks found | |
int n = WiFi.scanNetworks(false,false); | |
// Serial.println("scan done"); | |
int o = n; | |
int loops = 0; | |
if (n == 0) | |
Serial.println("no networks found"); | |
else | |
{ | |
// sort by RSSI | |
int indices[n]; | |
int skip[n]; | |
String ssid; | |
for (int i = 0; i < n; i++) { | |
indices[i] = i; | |
} | |
// CONFIG | |
bool sortRSSI = true; // sort aps by RSSI | |
bool removeDups = false; // remove dup aps ( forces sort ) | |
bool printAPs = true; // print found aps | |
bool printAPFound = false; // do home ap check | |
const char* homeAP = "MYAP"; // check for this ap on each scan | |
// -------- | |
bool homeAPFound = false; | |
if(removeDups || sortRSSI){ | |
for (int i = 0; i < n; i++) { | |
for (int j = i + 1; j < n; j++) { | |
if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) { | |
loops++; | |
//int temp = indices[j]; | |
//indices[j] = indices[i]; | |
//indices[i] = temp; | |
std::swap(indices[i], indices[j]); | |
std::swap(skip[i], skip[j]); | |
} | |
} | |
} | |
} | |
if(removeDups){ | |
for (int i = 0; i < n; i++) { | |
if(indices[i] == -1){ | |
--o; | |
continue; | |
} | |
ssid = WiFi.SSID(indices[i]); | |
for (int j = i + 1; j < n; j++) { | |
loops++; | |
if (ssid == WiFi.SSID(indices[j])) { | |
indices[j] = -1; | |
} | |
} | |
} | |
} | |
// Serial.println((String)loops); | |
Serial.print(o); | |
Serial.println(" networks found of " + (String)n); | |
Serial.println("00: (RSSI)[BSSID][hidden] SSID [channel] [encryption]"); | |
for (int i = 0; i < n; ++i) | |
{ | |
if(printAPFound && (WiFi.SSID(indices[i]) == homeAP)) homeAPFound = true; | |
if(printAPs && indices[i] != -1){ | |
// Print SSID and RSSI for each network found | |
Serial.printf("%02d", i + 1); | |
Serial.print(":"); | |
Serial.print(" ("); | |
Serial.print(WiFi.RSSI(indices[i])); | |
Serial.print(")"); | |
Serial.print(" ["); | |
Serial.print(WiFi.BSSIDstr(indices[i])); | |
Serial.print("]"); | |
Serial.print(" ["); | |
Serial.print((String) WiFi.isHidden(indices[i])); | |
Serial.print("]"); | |
Serial.print(" " + WiFi.SSID(indices[i])); | |
// Serial.print((WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE)?" ":"*"); | |
Serial.print(" ["); | |
Serial.printf("%02d",(int)WiFi.channel(indices[i])); | |
Serial.print("]"); | |
Serial.print(" ["); | |
Serial.print((String) encryptionTypeStr(WiFi.encryptionType(indices[i]))); | |
Serial.print("]"); | |
// Serial.print(" WiFi index: " + (String)indices[i]); | |
Serial.println(); | |
} | |
delay(10); | |
} | |
if(printAPFound && !homeAPFound) Serial.println("HOME AP NOT FOUND"); | |
Serial.println(""); | |
} | |
// Wait a bit before scanning again | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Consider using
qsort
for the sorting: https://arduino.stackexchange.com/questions/38177/how-to-sort-elements-of-array-in-arduino-code