ESP32 WPS在开机时重新连接

Chr*_*ger 8 arduino wlan esp32

我正在尝试开发一个应该使用HTTP/REST API提供某些功能的物联网设备.我决定使用ESP32芯片(在"ESP32开发板"上).

现在我想实现一个易于使用的WLAN配置.我不想像许多其他样本那样在我的源代码中存储凭据; 所以我决定使用WPS.

我尝试使用以下源代码实现基本Web服务器:https: //randomnerdtutorials.com/esp32-web-server-arduino-ide/ - 然后我添加了EPS32扩展附带的Wifi/WPS样本的WPS功能对于Arduino IDE.

现在WPS已经工作,即当开发板上电时,它处于WPS连接模式并等待路由器接受WPS连接.它成功获取SSID并连接到WLAN.

但是当我关闭ESP32并重新上电时,我必须再次进行WPS重新连接程序.我期望重新连接,存储凭据,并且能够在以后随时打开ESP32设备时再次连接到同一WLAN.我想我必须存储一些凭据并使用它们重新建立连接 - 但我在哪里获取凭据,以及如何重新连接?

我在网上搜索"ESP32 WLAN WPS重新连接"和类似的术语,但确实找到了非wps(SSID +密码)连接的重新连接策略.我还检查了WiFi库文档和esp_wps库文档,但没有找到合适的东西.

这是WLAN WPS连接源:

#include <WiFi.h>
#include "esp_wps.h"

#define ESP_WPS_MODE WPS_TYPE_PBC

esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(ESP_WPS_MODE);

String wpspin2string(uint8_t a[]){
  //...
}

void WiFiEvent(WiFiEvent_t event, system_event_info_t info){
  switch(event){
  case SYSTEM_EVENT_STA_START:
    Serial.println("Station Mode Started");
    break;
  case SYSTEM_EVENT_STA_GOT_IP:
    Serial.println("Connected to :" + String(WiFi.SSID()));
    Serial.print("Got IP: ");
    Serial.println(WiFi.localIP());
    break;
  case SYSTEM_EVENT_STA_DISCONNECTED:
    Serial.println("Disconnected from station, attempting reconnection");
    WiFi.reconnect();
    break;
  case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
    Serial.println("WPS Successfull, stopping WPS and connecting to: " +     String(WiFi.SSID()));
    esp_wifi_wps_disable();
    delay(10);
    WiFi.begin();
    break;
  case SYSTEM_EVENT_STA_WPS_ER_FAILED:
    Serial.println("WPS Failed, retrying");
    esp_wifi_wps_disable();
    esp_wifi_wps_enable(&config);
    esp_wifi_wps_start(0);
    break;
  case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
    Serial.println("WPS Timedout, retrying");
    esp_wifi_wps_disable();
    esp_wifi_wps_enable(&config);
    esp_wifi_wps_start(0);
    break;
    case SYSTEM_EVENT_STA_WPS_ER_PIN:
    Serial.println("WPS_PIN = " + wpspin2string(info.sta_er_pin.pin_code));
    break;
    default:
    break;
  }
}

// some GPIO stuff, removed for SO question

void setup() {
  // initialize some GPIO for status etc. - removed for SO

  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while(!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // We start by connecting to a WiFi network

  WiFi.onEvent(WiFiEvent);
  WiFi.mode(WIFI_MODE_STA);

  Serial.println("Starting WPS");

  esp_wifi_wps_enable(&config);
  esp_wifi_wps_start(0);

  // attempt to connect to Wifi network:
  while(WiFi.status() != WL_CONNECTED) {
    // Connect to WPA/WPA2 network. Change this line if using open or WEP     network:
    delay(700);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  //irrelevant for SO question
}
Run Code Online (Sandbox Code Playgroud)

Xer*_*XeX 5

ESP,无论是 32 还是 8266,都会记住它连接的最后一个 AP。WiFi.begin();没有任何凭据的简单调用将使其连接到最后一个 AP。然后在你的while(WiFi.status() != WL_CONNECTED)循环中,你可以让它超时,然后esp_wifi_wps_start(0);如果它没有连接就调用它。