Header Ads

ESP8266 scan and print nearby Wi-Fi Device

ESP8266 scan nearby Wi-Fi and print order by wifi signal strength.

This sketch demonstrates how to scan WiFi networks and print signal strength order.Click the download link to download the code Download here.




/*
    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);

  // 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("Setup done");
}

void loop() {
  Serial.println("scan start");

  // WiFi.scanNetworks will return the number of networks found
  //int networksFound = WiFi.scanNetworks();
  int n = WiFi.scanNetworks();;
  Serial.println("scan done");
   int indices[n];
    int skip[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++) {
            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]);
                std::swap(skip[i], skip[j]);
            }
        }
    }

    for (int i = 0; i < 5 && i < n ; ++i) {
        Serial.print("SSID : ");
        Serial.print(WiFi.SSID(indices[i]));
        Serial.print(", MAC : ");
        Serial.print(WiFi.BSSIDstr(indices[i]));
        Serial.print(", RSSI : ");
        Serial.print(WiFi.RSSI(indices[i]));
        Serial.print(", Channel : ");
        Serial.print(WiFi.channel(indices[i]));
        Serial.print(", Encryption : ");
        Serial.print(WiFi.encryptionType(indices[i]));
        Serial.print(", Hidden? : ");
        Serial.println(WiFi.isHidden(indices[i]) ? true : false);
    }
      WiFi.scanDelete();

}

No comments

Powered by Blogger.