SHOW:
|
|
- or go back to the newest paste.
| 1 | - | /* |
| 1 | + | /* |
| 2 | - | * This sketch demonstrates how to scan WiFi networks. |
| 2 | + | * This sketch demonstrates how to scan WiFi networks. |
| 3 | - | * The API is almost the same as with the WiFi Shield library, |
| 3 | + | * The API is almost the same as with the WiFi Shield library, |
| 4 | - | * the most obvious difference being the different file you need to include: |
| 4 | + | * the most obvious difference being the different file you need to include: |
| 5 | - | */ |
| 5 | + | */ |
| 6 | - | #include "ESP8266WiFi.h" |
| 6 | + | #include "ESP8266WiFi.h" |
| 7 | - | |
| 7 | + | |
| 8 | - | void setup() { |
| 8 | + | void setup() {
|
| 9 | - | Serial.begin(115200); |
| 9 | + | Serial.begin(115200); |
| 10 | - | // Serial.setDebugOutput(true); |
| 10 | + | // Serial.setDebugOutput(true); |
| 11 | - | |
| 11 | + | |
| 12 | - | // Set WiFi to station mode and disconnect from an AP if it was previously connected |
| 12 | + | // Set WiFi to station mode and disconnect from an AP if it was previously connected |
| 13 | - | WiFi.mode(WIFI_STA); |
| 13 | + | WiFi.mode(WIFI_STA); |
| 14 | - | WiFi.disconnect(); |
| 14 | + | WiFi.disconnect(); |
| 15 | - | |
| 15 | + | |
| 16 | - | delay(100); |
| 16 | + | delay(100); |
| 17 | - | |
| 17 | + | |
| 18 | - | Serial.println(""); |
| 18 | + | Serial.println("");
|
| 19 | - | Serial.println("Setup done"); |
| 19 | + | Serial.println("Setup done");
|
| 20 | - | Serial.println(""); |
| 20 | + | Serial.println("");
|
| 21 | - | Serial.println("MAC: " + WiFi.macAddress()); |
| 21 | + | Serial.println("MAC: " + WiFi.macAddress());
|
| 22 | } | |
| 23 | - | |
| 23 | + | |
| 24 | - | String encryptionTypeStr(uint8_t authmode) { |
| 24 | + | String encryptionTypeStr(uint8_t authmode) {
|
| 25 | - | switch(authmode) { |
| 25 | + | switch(authmode) {
|
| 26 | - | case ENC_TYPE_NONE: |
| 26 | + | case ENC_TYPE_NONE: |
| 27 | - | return "OPEN"; |
| 27 | + | return "OPEN"; |
| 28 | - | case ENC_TYPE_WEP: |
| 28 | + | case ENC_TYPE_WEP: |
| 29 | - | return "WEP"; |
| 29 | + | return "WEP"; |
| 30 | - | case ENC_TYPE_TKIP: |
| 30 | + | case ENC_TYPE_TKIP: |
| 31 | - | return "WPA_PSK"; |
| 31 | + | return "WPA_PSK"; |
| 32 | - | case ENC_TYPE_CCMP: |
| 32 | + | case ENC_TYPE_CCMP: |
| 33 | - | return "WPA2_PSK"; |
| 33 | + | return "WPA2_PSK"; |
| 34 | - | case ENC_TYPE_AUTO: |
| 34 | + | case ENC_TYPE_AUTO: |
| 35 | - | return "WPA_WPA2_PSK"; |
| 35 | + | return "WPA_WPA2_PSK"; |
| 36 | - | default: |
| 36 | + | default: |
| 37 | - | return "UNKOWN"; |
| 37 | + | return "UNKOWN"; |
| 38 | - | } |
| 38 | + | } |
| 39 | } | |
| 40 | - | |
| 40 | + | |
| 41 | - | void loop() { |
| 41 | + | void loop() {
|
| 42 | - | // Serial.println("scan start"); |
| 42 | + | // Serial.println("scan start");
|
| 43 | - | |
| 43 | + | |
| 44 | - | // WiFi.scanNetworks will return the number of networks found |
| 44 | + | // WiFi.scanNetworks will return the number of networks found |
| 45 | - | int n = WiFi.scanNetworks(false,false); |
| 45 | + | int n = WiFi.scanNetworks(false,false); |
| 46 | - | // Serial.println("scan done"); |
| 46 | + | // Serial.println("scan done");
|
| 47 | - | int o = n; |
| 47 | + | int o = n; |
| 48 | - | int loops = 0; |
| 48 | + | int loops = 0; |
| 49 | - | |
| 49 | + | |
| 50 | - | if (n == 0) |
| 50 | + | if (n == 0) |
| 51 | - | Serial.println("no networks found"); |
| 51 | + | Serial.println("no networks found");
|
| 52 | - | else |
| 52 | + | else |
| 53 | - | { |
| 53 | + | {
|
| 54 | - | // sort by RSSI |
| 54 | + | // sort by RSSI |
| 55 | - | int indices[n]; |
| 55 | + | int indices[n]; |
| 56 | - | int skip[n]; |
| 56 | + | int skip[n]; |
| 57 | - | |
| 57 | + | |
| 58 | - | String ssid; |
| 58 | + | String ssid; |
| 59 | - | |
| 59 | + | |
| 60 | - | for (int i = 0; i < n; i++) { |
| 60 | + | for (int i = 0; i < n; i++) {
|
| 61 | - | indices[i] = i; |
| 61 | + | indices[i] = i; |
| 62 | - | } |
| 62 | + | } |
| 63 | - | |
| 63 | + | |
| 64 | - | // CONFIG |
| 64 | + | // CONFIG |
| 65 | - | bool sortRSSI = true; // sort aps by RSSI |
| 65 | + | bool sortRSSI = true; // sort aps by RSSI |
| 66 | - | bool removeDups = false; // remove dup aps ( forces sort ) |
| 66 | + | bool removeDups = false; // remove dup aps ( forces sort ) |
| 67 | - | bool printAPs = true; // print found aps |
| 67 | + | bool printAPs = true; // print found aps |
| 68 | - | |
| 68 | + | |
| 69 | - | bool printAPFound = false; // do home ap check |
| 69 | + | bool printAPFound = false; // do home ap check |
| 70 | - | const char* homeAP = "MYAP"; // check for this ap on each scan |
| 70 | + | const char* homeAP = "MYAP"; // check for this ap on each scan |
| 71 | - | // -------- |
| 71 | + | // -------- |
| 72 | - | |
| 72 | + | |
| 73 | - | bool homeAPFound = false; |
| 73 | + | bool homeAPFound = false; |
| 74 | - | |
| 74 | + | |
| 75 | - | if(removeDups || sortRSSI){ |
| 75 | + | if(removeDups || sortRSSI){
|
| 76 | - | for (int i = 0; i < n; i++) { |
| 76 | + | for (int i = 0; i < n; i++) {
|
| 77 | - | for (int j = i + 1; j < n; j++) { |
| 77 | + | for (int j = i + 1; j < n; j++) {
|
| 78 | - | if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) { |
| 78 | + | if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
|
| 79 | - | loops++; |
| 79 | + | loops++; |
| 80 | - | //int temp = indices[j]; |
| 80 | + | //int temp = indices[j]; |
| 81 | - | //indices[j] = indices[i]; |
| 81 | + | //indices[j] = indices[i]; |
| 82 | - | //indices[i] = temp; |
| 82 | + | //indices[i] = temp; |
| 83 | - | std::swap(indices[i], indices[j]); |
| 83 | + | std::swap(indices[i], indices[j]); |
| 84 | - | std::swap(skip[i], skip[j]); |
| 84 | + | std::swap(skip[i], skip[j]); |
| 85 | - | } |
| 85 | + | } |
| 86 | - | } |
| 86 | + | } |
| 87 | - | } |
| 87 | + | } |
| 88 | - | } |
| 88 | + | } |
| 89 | - | |
| 89 | + | |
| 90 | - | if(removeDups){ |
| 90 | + | if(removeDups){
|
| 91 | - | for (int i = 0; i < n; i++) { |
| 91 | + | for (int i = 0; i < n; i++) {
|
| 92 | - | if(indices[i] == -1){ |
| 92 | + | if(indices[i] == -1){
|
| 93 | - | --o; |
| 93 | + | --o; |
| 94 | - | continue; |
| 94 | + | continue; |
| 95 | - | } |
| 95 | + | } |
| 96 | - | ssid = WiFi.SSID(indices[i]); |
| 96 | + | ssid = WiFi.SSID(indices[i]); |
| 97 | - | for (int j = i + 1; j < n; j++) { |
| 97 | + | for (int j = i + 1; j < n; j++) {
|
| 98 | - | loops++; |
| 98 | + | loops++; |
| 99 | - | if (ssid == WiFi.SSID(indices[j])) { |
| 99 | + | if (ssid == WiFi.SSID(indices[j])) {
|
| 100 | - | indices[j] = -1; |
| 100 | + | indices[j] = -1; |
| 101 | - | } |
| 101 | + | } |
| 102 | - | } |
| 102 | + | } |
| 103 | - | } |
| 103 | + | } |
| 104 | - | } |
| 104 | + | } |
| 105 | - | |
| 105 | + | |
| 106 | - | // Serial.println((String)loops); |
| 106 | + | // Serial.println((String)loops); |
| 107 | - | Serial.print(o); |
| 107 | + | Serial.print(o); |
| 108 | - | Serial.println(" networks found of " + (String)n); |
| 108 | + | Serial.println(" networks found of " + (String)n);
|
| 109 | - | |
| 109 | + | |
| 110 | - | Serial.println("00: (RSSI)[BSSID][hidden] SSID [channel] [encryption]"); |
| 110 | + | Serial.println("00: (RSSI)[BSSID][hidden] SSID [channel] [encryption]");
|
| 111 | - | for (int i = 0; i < n; ++i) |
| 111 | + | for (int i = 0; i < n; ++i) |
| 112 | - | { |
| 112 | + | {
|
| 113 | - | if(printAPFound && (WiFi.SSID(indices[i]) == homeAP)) homeAPFound = true; |
| 113 | + | if(printAPFound && (WiFi.SSID(indices[i]) == homeAP)) homeAPFound = true; |
| 114 | - | |
| 114 | + | |
| 115 | - | if(printAPs && indices[i] != -1){ |
| 115 | + | if(printAPs && indices[i] != -1){
|
| 116 | - | // Print SSID and RSSI for each network found |
| 116 | + | // Print SSID and RSSI for each network found |
| 117 | - | Serial.printf("%02d", i + 1); |
| 117 | + | Serial.printf("%02d", i + 1);
|
| 118 | - | Serial.print(":"); |
| 118 | + | Serial.print(":");
|
| 119 | - | |
| 119 | + | |
| 120 | - | Serial.print(" ("); |
| 120 | + | Serial.print(" (");
|
| 121 | - | Serial.print(WiFi.RSSI(indices[i])); |
| 121 | + | Serial.print(WiFi.RSSI(indices[i])); |
| 122 | - | Serial.print(")"); |
| 122 | + | Serial.print(")");
|
| 123 | - | |
| 123 | + | |
| 124 | - | Serial.print(" ["); |
| 124 | + | Serial.print(" [");
|
| 125 | - | Serial.print(WiFi.BSSIDstr(indices[i])); |
| 125 | + | Serial.print(WiFi.BSSIDstr(indices[i])); |
| 126 | - | Serial.print("]"); |
| 126 | + | Serial.print("]");
|
| 127 | - | |
| 127 | + | |
| 128 | - | Serial.print(" ["); |
| 128 | + | Serial.print(" [");
|
| 129 | - | Serial.print((String) WiFi.isHidden(indices[i])); |
| 129 | + | Serial.print((String) WiFi.isHidden(indices[i])); |
| 130 | - | Serial.print("]"); |
| 130 | + | Serial.print("]");
|
| 131 | - | |
| 131 | + | |
| 132 | - | Serial.print(" " + WiFi.SSID(indices[i])); |
| 132 | + | Serial.print(" " + WiFi.SSID(indices[i]));
|
| 133 | - | // Serial.print((WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE)?" ":"*"); |
| 133 | + | // Serial.print((WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE)?" ":"*"); |
| 134 | - | |
| 134 | + | |
| 135 | - | Serial.print(" ["); |
| 135 | + | Serial.print(" [");
|
| 136 | - | Serial.printf("%02d",(int)WiFi.channel(indices[i])); |
| 136 | + | Serial.printf("%02d",(int)WiFi.channel(indices[i]));
|
| 137 | - | Serial.print("]"); |
| 137 | + | Serial.print("]");
|
| 138 | - | |
| 138 | + | |
| 139 | - | Serial.print(" ["); |
| 139 | + | Serial.print(" [");
|
| 140 | - | Serial.print((String) encryptionTypeStr(WiFi.encryptionType(indices[i]))); |
| 140 | + | Serial.print((String) encryptionTypeStr(WiFi.encryptionType(indices[i]))); |
| 141 | - | Serial.print("]"); |
| 141 | + | Serial.print("]");
|
| 142 | - | |
| 142 | + | |
| 143 | - | // Serial.print(" WiFi index: " + (String)indices[i]); |
| 143 | + | // Serial.print(" WiFi index: " + (String)indices[i]);
|
| 144 | - | |
| 144 | + | |
| 145 | - | Serial.println(); |
| 145 | + | Serial.println(); |
| 146 | - | } |
| 146 | + | } |
| 147 | - | delay(10); |
| 147 | + | delay(10); |
| 148 | - | } |
| 148 | + | } |
| 149 | - | if(printAPFound && !homeAPFound) Serial.println("HOME AP NOT FOUND"); |
| 149 | + | if(printAPFound && !homeAPFound) Serial.println("HOME AP NOT FOUND");
|
| 150 | - | Serial.println(""); |
| 150 | + | Serial.println("");
|
| 151 | - | } |
| 151 | + | } |
| 152 | - | |
| 152 | + | |
| 153 | - | // Wait a bit before scanning again |
| 153 | + | // Wait a bit before scanning again |
| 154 | - | delay(500); |
| 154 | + | delay(500); |
| 155 | } |