You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过ESP32编程检测目标网络是否启用WPS?

检测ESP32目标WiFi网络的WPS状态

是的,完全可以通过ESP32的内置WiFi库(无论是ESP-IDF原生库还是Arduino ESP32核心)直接编程检测目标AP是否开启WPS,无需外部工具。

核心原理

ESP32在扫描周边WiFi网络时,会获取每个AP的详细信息,其中就包含WPS启用状态。在ESP-IDF的wifi_ap_record_t结构体中,wps_enable字段(布尔类型)直接标记了该AP是否开启WPS;Arduino ESP32核心也封装了这个字段,可通过WiFiScanClass的相关方法获取。

实现代码示例

ESP-IDF 原生实现

#include "esp_wifi.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

static const char *TAG = "WPS_DETECT";

void app_main(void)
{
    // 初始化NVS(WiFi操作必需)
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    // 配置WiFi为STA模式
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
    ESP_ERROR_CHECK(esp_wifi_start());

    // 扫描WiFi网络
    uint16_t ap_count = 0;
    uint8_t scan_buf[sizeof(wifi_ap_record_t) * 10]; // 最多存储10个AP信息
    ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
    ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, (wifi_ap_record_t *)scan_buf));

    ESP_LOGI(TAG, "扫描到 %d 个WiFi网络", ap_count);

    // 遍历扫描结果,检测目标AP的WPS状态(替换成你的目标SSID)
    const char *target_ssid = "YourTargetSSID";
    bool target_wps_enabled = false;

    for (int i = 0; i < ap_count; i++) {
        wifi_ap_record_t *ap = (wifi_ap_record_t *)&scan_buf[i * sizeof(wifi_ap_record_t)];
        ESP_LOGI(TAG, "SSID: %s, WPS: %s", ap->ssid, ap->wps_enable ? "Enabled" : "Disabled");
        
        if (strcmp((char *)ap->ssid, target_ssid) == 0) {
            target_wps_enabled = ap->wps_enable;
            break;
        }
    }

    if (target_wps_enabled) {
        ESP_LOGI(TAG, "目标网络 %s 已启用WPS", target_ssid);
    } else {
        ESP_LOGI(TAG, "目标网络 %s 未启用WPS或未扫描到", target_ssid);
    }

    // 停止WiFi
    esp_wifi_stop();
    esp_wifi_deinit();
}

Arduino ESP32 实现

#include <WiFi.h>

void setup() {
    Serial.begin(115200);
    WiFi.mode(WIFI_STA);

    // 扫描WiFi网络
    int ap_count = WiFi.scanNetworks();
    Serial.printf("扫描到 %d 个WiFi网络\n", ap_count);

    // 检测目标AP的WPS状态(替换成你的目标SSID)
    const char* target_ssid = "YourTargetSSID";
    bool target_wps_enabled = false;

    for (int i = 0; i < ap_count; i++) {
        // 获取AP的WPS状态,Arduino核心中通过WiFi.isWPSAvailable(i)获取
        bool wps_enabled = WiFi.isWPSAvailable(i);
        Serial.printf("SSID: %s, WPS: %s\n", WiFi.SSID(i).c_str(), wps_enabled ? "Enabled" : "Disabled");
        
        if (WiFi.SSID(i) == target_ssid) {
            target_wps_enabled = wps_enabled;
            break;
        }
    }

    if (target_wps_enabled) {
        Serial.printf("目标网络 %s 已启用WPS\n", target_ssid);
    } else {
        Serial.printf("目标网络 %s 未启用WPS或未扫描到\n", target_ssid);
    }

    WiFi.disconnect();
}

void loop() {
    // 无需循环操作
}

注意事项

  • 确保ESP32的WiFi模式设置为WIFI_MODE_STA(Station模式),扫描操作仅在STA模式下有效。
  • 扫描结果可能受环境干扰,建议多次扫描提高准确性。
  • ESP-IDF版本不同可能存在细微API差异,若wps_enable字段不存在,可检查wifi_ap_record_t结构体定义,通常该字段在v4.0及以上版本中稳定存在。
  • Arduino ESP32核心中,WiFi.isWPSAvailable(index)方法直接封装了底层的wps_enable字段,使用更便捷。

内容的提问来源于stack exchange,提问作者Apocryphon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 06:15:26