YourESP32 Sketch WIFI Scan

From ArduinoInfo
Jump to: navigation, search

/* YourESP32 Example: Sketch WIFI_SCAN
    SEE: https://ESP32Info.Info
  - WHAT IT DOES :demonstrates how to scan WiFi networks.
  - SEE the comments after "//" on each line below
  - CONNECTIONS:

  - V1.00 10/02/2018
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include "WiFi.h"
/*-----( Declare Constants and Pin Numbers )-----*/
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/
{
  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");

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Serial.println("scan start");

  int n = WiFi.scanNetworks();  // return the number of networks found
  Serial.println("scan done");
  if (n == 0)
  {
    Serial.println("no networks found");
  } else
  {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i)
    {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/
//NONE yet

//*********( THE END )***********