Advertisement
maujogador

arduino wifi scan

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