使用Arduino开发ESP32(四):扫描wifi,串口输出其ssid,信号强度,加密方式,mac地址;输出esp32自身的ip与mac地址,含官方例程

话不多说,先上代码:

#include

const char* id="wifi名称";              //常量定义
const char* psw="wifi密码";

String transEncryptionType(wifi_auth_mode_t encryptionType){
     		//对比出该wifi网络加密类型并返回相应的String值   
  switch(encryptionType){
     
      case (WIFI_AUTH_OPEN):
        return "Open";
      case (WIFI_AUTH_WEP):
        return "WEP";
      case (WIFI_AUTH_WPA_PSK):
        return "WPA_PSK";
      case (WIFI_AUTH_WPA2_PSK):
        return "WPA2_PSK";
      case (WIFI_AUTH_WPA_WPA2_PSK):
        return "WPA_WPA2_PSK";
      case (WIFI_AUTH_WPA2_ENTERPRISE):
        return "WPA2_ENTERPRISE";
      default:
        return("Unkonwn EncryptionType");
  }
}

void scanNetworks(){
     						//扫描周边wifi网络,并显示wifi数量,打印它们的属性(ssid,信号强度,加密方式,mac地址)
  int numberOfNetworks= WiFi.scanNetworks();
  Serial.print("The number of networks found is:");
  Serial.println(numberOfNetworks);
  for(int i=0;i<numberOfNetworks;i++){
     
    Serial.print("Networkname: ");
    Serial.println(WiFi.SSID(i));
    Serial.print("Signalstrength: ");
    Serial.println(WiFi.RSSI(i));
    Serial.print("MACaddress: ");
    Serial.println(WiFi.BSSIDstr(i));
    Serial.print("Encryptiontype: ");
    String encryptionTypeDescription = transEncryptionType(WiFi.encryptionType(i));
    Serial.println(encryptionTypeDescription);
    Serial.println("-----------------------");
  }
}

void connect(){
     							//连接到指定wifi
  WiFi.begin(id,psw);
   while (WiFi.status()!= WL_CONNECTED) {
     
    delay(1000);
    Serial.println("正在尝试连接该wifi...");
  }
  Serial.println("连接成功!");
}

void setup() {
     
  Serial.begin(115200);                //初始化配置,波特率115200
  scanNetworks();						//扫描wifi并打印信息
  connect();							//连接到指定wifi
  Serial.println(WiFi.macAddress());		//打印esp32的mac地址
  Serial.println(WiFi.localIP());			//esp32此刻的本地ip地址
  WiFi.disconnect(true);					//断开当前wifi网络的连接
  Serial.println(WiFi.localIP());  			//打印esp32此刻的本地ip地址
}

void loop(){
     
  
}

输出效果:
使用Arduino开发ESP32(四):扫描wifi,串口输出其ssid,信号强度,加密方式,mac地址;输出esp32自身的ip与mac地址,含官方例程_第1张图片
使用Arduino开发ESP32(四):扫描wifi,串口输出其ssid,信号强度,加密方式,mac地址;输出esp32自身的ip与mac地址,含官方例程_第2张图片
代码解释:
2020.7.15:
正在恶补c++中…
本来想深入库函数讲解的,发现是c++写的

官方arduino core for esp库函数下载地址
libraries文件夹下自行查找,也包含例程
。。。。。。。。。。。。。。。。。。。。。。。。。。。
2020.7.16:
已经找到了相关库函数的本体
不过量有点大,所以决定新开一篇博客
.。。。。。。。。。。。。。。。。。。。。。。。。。。。
2020.7.17
官方例程(修改部分文本):

/*
 *  This sketch demonstrates how to scan WiFi networks.
 * 这个梗概示例演示了如何扫描wifi网络
 *  The API is almost the same as with the WiFi Shield library,
 *  the most obvious difference being the different file you 
 */
#include "WiFi.h"

void setup()
{
     
    Serial.begin(115200);

    // Set WiFi to station mode and disconnect from an AP if it was previously connected
    //将wifi置于station工作模式,如果设备处于AP工作模式下,且正被其他设备连接,将会断开连接
    WiFi.mode(WIFI_STA);   //设置为Station模式
    WiFi.disconnect();	   //断开当前连接
    delay(100);

    Serial.println("初始化完成");		
}

void loop()
{
     
    Serial.println("扫描开始");

    // WiFi.scanNetworks will return the number of networks found
    //WiFi.scanNetworks()函数会返回已发现网络数量
    int n = WiFi.scanNetworks();
    Serial.println("扫描完成");
    if (n == 0) {
     
        Serial.println("没有发现网络");
    } else {
     
        Serial.print(n);
        Serial.println(" 发现网络");
        for (int i = 0; i < n; ++i) {
     
            // Print SSID and RSSI for each network found
            //为每个网络打印其SSID(Service Set Identifier 服务集标识)
            //RSSI(Received Signal Strength Indication 信号强度)
            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)?" ":"*");
            //AUTH:Authentication  认证
            //WIFI_AUTH_OPEN 没有认证(不需要wifi密码)
            //这段语句意思:如果需要密码才能连接就打印"*",否则空格
            delay(10);
        }
    }
    Serial.println("");

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

wifi_auth_mode_t 内容包含:

• WIFI_AUTH_OPEN - 没有安全性。

• WIFI_AUTH_WEP - WEP安全性。

• WIFI_AUTH_WPA_PSK - WPA安全性。

• WIFI_AUTH_WPA2_PSK - WPA2安全。

• WIFI_AUTH_WPA_WPA2_PSK - WPA或WPA2安全性。

参考文章:
通过wifi_scan学习esp32wifi程序编写

你可能感兴趣的:(ESP32学习之路,wifi,ESP32,蓝牙,wifi)