1

这是扫描WiFi网络并连接到WiFi的代码。

#include<ESP8266WiFi.h>
#include<Arduino.h>
#define USER_SERIAL Serial 
const char* ssid = "*****";
const char* pass = "*****";

void setup() {
  USER_SERIAL.begin(115200);
  searchWifi();
  WiFi.mode(WIFI_STA);
  WiFi.hostname("ESP-host");
  WiFi.setPhyMode(WIFI_PHY_MODE_11G);
  WiFi.enableInsecureWEP(true);
  WiFi.begin(ssid,pass);
  while(WiFi.status() != WL_CONNECTED){
    USER_SERIAL.print(".");
    delay(1000);
  }
  USER_SERIAL.print("");
  USER_SERIAL.println("WiFi connected");
  USER_SERIAL.print("IP Address: ");
  USER_SERIAL.println(WiFi.localIP());
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

 void searchWifi(){
  int numberOfNetwork = WiFi.scanNetworks();
  USER_SERIAL.println("-----");
  for ( int i = 0; i< numberOfNetwork;i++){
    USER_SERIAL.print("Network name: ");
    USER_SERIAL.println(WiFi.SSID(i));
    USER_SERIAL.print("Signal Strength: ");
    USER_SERIAL.println(WiFi.RSSI(i));
    USER_SERIAL.println("-------------");
  }
}

我的 WiFi 处于 11bgn 混合模式,这意味着我的连接允许连接所有设备。此外,我的 WiFi 路由器也有 2.4GHz 频率,这是 ESP8266 允许连接的要求。

4

0 回答 0