使用ESP32的WiFi模式


ESP32微控制器可以連線到網際網路並使用WiFi。它可以用來檢視可用網路的網路詳細資訊。它還可以用於更高階的功能,例如更改ESP32的MAC地址。本文透過三個不同的示例,給出了使用ESP32的WiFi的程式。示例1中,使用C程式掃描可用的WiFi網路並顯示詳細資訊。示例2中,透過提供SSID詳細資訊來建立WiFi連線,示例3中,檢視ESP32當前的MAC地址,然後使用ESP32的WiFi模組更改它。

示例1 − 使用ESP32 WiFi模組查詢可用的WiFi網路。

示例2 − 使用ESP32 WiFi模組與無線網路建立網際網路連線。

示例3 − 使用ESP32 WiFi模組設定ESP32的新MAC地址。

電路設計步驟和編碼

步驟1 − 將ESP32連線到麵包板,然後使用USB資料線將其連線到電腦。

步驟2 − 如果電腦上未安裝Arduino IDE,請下載並安裝。

步驟3 − 啟動Arduino IDE。編寫C程式並使用勾號進行編譯。

步驟4 − 按下勾號旁邊的右箭頭,將程式上傳到ESP32。

步驟5 − 在序列埠監視器上檢查結果。

示例1:使用ESP32 WiFi模組查詢可用的WiFi網路

ESP32可以使用其WiFi模組連線到網際網路。這裡首先使用它掃描可用的WiFi網路,然後顯示SSID、RSSI、訊號強度以及加密型別。為此,使用wifi.h庫。

程式碼

//library needed for wifi connectivity Using the WiFi Mode with ESP32
#include "WiFi.h"
void setup() {
   //set the baud rate
   Serial.begin(115200);
   //set wifi to the station mode
   WiFi.mode(WIFI_STA);
   //disconnect to start again
   WiFi.disconnect();
   delay(100);
}
void loop() {
   Serial.println("starting to scan now");
   // finding the networks available nearby
   int netCount = WiFi.scanNetworks();
   Serial.println("scan completed");
   if (netCount == 0) {
      Serial.println("no network detected");
   } else {
      Serial.print(netCount);
      Serial.println(" networks found");
      for (int nn = 0; nn < netCount; ++nn) {
         //The number should start from 1 and not 0
         Serial.print(nn + 1);
         Serial.print(" ---- ");
         // Print SSID(Service Set Identifier)
         Serial.print(WiFi.SSID(nn));
         Serial.print(" ---- ");
         //Print RSSI(Received Signal Strength Indicator)
         Serial.print(WiFi.RSSI(nn));
         // print the unit
         Serial.print(" dB ---- ");
         String enType="";
         wifi_auth_mode_t encryp_type=WiFi.encryptionType(nn);
         if (encryp_type == WIFI_AUTH_WPA_WPA2_PSK){
            enType="WPA_WPA2_PSK";
         }
         else if (encryp_type == WIFI_AUTH_WPA2_ENTERPRISE){
            enType="WPA2_ENTERPRISE";
         }
         else if (encryp_type == WIFI_AUTH_OPEN){
            enType="Open";
         }
         else if (encryp_type == WIFI_AUTH_WPA_PSK){
            enType="WPA_PSK";
         }
         else if (encryp_type == WIFI_AUTH_WPA2_PSK){
            enType="WPA2_PSK";
         }
         else if (encryp_type == WIFI_AUTH_WEP){
            enType="WEP";
         }
         //display encryption type
         Serial.println(enType);
         delay(50);
      }
   }
   Serial.println("");
   // Wait and scan again
   delay(5000);
}

檢視結果

程式編譯後,上傳到ESP32,結果可以在序列埠監視器上看到。

圖1:顯示序列埠監視器上的結果

示例2:使用ESP32 WiFi模組與無線網路建立網際網路連線。

ESP32可以使用其WiFi模組連線到網際網路。在此示例中,透過提供無線網路的SSID和WiFi密碼來建立與無線網路的連線。為此,使用wifi.h庫。

程式碼

//library required for wifi connection
#include <WiFi.h>
//specify your wifi ssid and its password
#define WIFI_SSID "ABCD"
#define WIFI_PASSWORD "EFGH"
void setup(){
   Serial.begin(115200);
   delay(500);
   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
   while(WiFi.status() != WL_CONNECTED){
      Serial.print(".");
      delay(200);
   }
   if(WiFi.status() == WL_CONNECTED) {
      Serial.println("
Now Connected to the WiFi "); //print SSID of wifi Serial.print("Wireless Network: "); Serial.println(WIFI_SSID); //print the MAC address of wireless router or access point (Basic Service Set Identifier) Serial.print("The Current MAC address is : " ); Serial.println(WiFi.BSSIDstr()); //print the private address of wireless router(Gateway IP) Serial.print("The router IP is : "); Serial.println(WiFi.gatewayIP()); //print a 32-bit number by setting host bits to 0s and network bits to 1s.(Subnet Mask) Serial.print("This is the Subnet Mask : "); Serial.println(WiFi.subnetMask()); //print received signal strength Serial.print("This is the signal strength : "); Serial.println(WiFi.RSSI() ); //print ESP32 IP address Serial.print("This is the IP address of ESP32 : "); Serial.println(WiFi.localIP()); } }

檢視結果

程式編譯後,上傳到ESP32,結果可以在序列埠監視器上看到。

圖2:序列埠監視器上顯示的結果

示例3:使用ESP32 WiFi模組設定ESP32的新MAC地址。

ESP32可以使用其WiFi模組連線到網際網路。MAC(媒體訪問控制)地址是一個12個字元的物理地址,用於在網路上識別裝置。這裡使用WiFi庫函式首先顯示ESP32當前的MAC地址。其次,它用於指定一個新的MAC地址,然後設定新的MAC地址並顯示它。這裡使用的庫是WiFi.h和esp_wifi.h

程式碼

//library required for wifi connection
//first library
#include <esp_wifi.h>
//second libray
#include <WiFi.h>
//specify the new mac address of ESP32
uint8_t new_mac_addr[] = {0x67C, 0x9D, 0xC2, 0x08, 0x5B, 0x33};
void setup(){
   Serial.begin(115200);
   WiFi.mode(WIFI_STA);
   //display the new MAC address
   Serial.print("This is the current MAC Address of ESP32: ");
   Serial.println(WiFi.macAddress());
   //setting the new MAC address
   esp_wifi_set_mac(ESP_IF_WIFI_STA, new_mac_addr);
   //display the new MAC address
   Serial.print("The new MAC Address is set now: ");
   Serial.println(WiFi.macAddress());
   delay(1500);
}
void loop(){}

檢視結果

程式編譯後,上傳到ESP32,結果可以在序列埠監視器上看到。

圖3:顯示序列埠監視器上的結果

本文介紹瞭如何使用ESP32的WiFi模組。示例1中,指定了顯示所有可用WiFi網路的方法。示例2中,解釋了與特定無線網路建立連線的方法。此外,示例3中介紹了更改MAC地址的更高階方法。

更新於:2023年4月18日

2K+ 次瀏覽

開啟你的職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.