简体   繁体   中英

ESP32 WPS reconnect on power-on

I am trying to develop an IoT device that should provide some functionality using a HTTP/REST API. I decided to use the ESP32 chip (on "ESP32 dev board").

Now I want to implement an easy-to-use WLAN configuration. I don't want to store credentials in my source code like many other samples do; so I decided to use WPS.

I tried to implement a basic web server using the sources here: https://randomnerdtutorials.com/esp32-web-server-arduino-ide/ - and then I added the WPS functionality from the Wifi/WPS samples shipped with the EPS32 extensions for Arduino IDE.

Now the WPS already works, ie when the dev-board gets powered it is in WPS connection mode and waits for the router to accept the WPS connection. It successfully gets the SSID and connects to the WLAN.

But when I power-off the ESP32, and power-on again, I have to do the WPS reconnection procedure again. I'd expect a reconnection, that stores the credentials and is able to connect to the same WLAN again when the ESP32 device is powered-on at any time later. I guess I have to store some credentials and use them to re-establish the connection - but where do I get the credentials, and how do I reconnect?

I did search the web for "ESP32 WLAN WPS reconnect" and similar terms, but did find only reconnect strategies for non-wps (SSID + password) connections. I did also check the WiFi library documentation and the esp_wps library documentation, but didn't find anything suitable.

That's the WLAN WPS connection source:

#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
}

The ESP, both 32 and 8266, remember the last AP it was connected to. So simple call WiFi.begin(); without any credentials will make it connect to that last AP. Then in your while(WiFi.status() != WL_CONNECTED) loop you could make it timeout and then call the esp_wifi_wps_start(0); if it does not connect.

you can find the answer here https://www.esp32.com/viewtopic.php?f=19&t=27004

the SSID and password are stored in the config and given to esp_wifi_set_config, after WPS has finished, you can just use the getter function again by calling:

wifi_config_t config;
esp_err_t err = esp_wifi_get_config(WIFI_IF_STA, &config);
if (err == ESP_OK) {
  printf("SSID: %s, PW: %s\n", (char*) config.sta.ssid, (char*) config.sta.password);
} else {
  printf("Couldn't get config: %d\n", (int) err);
}

and you can then find the SSID and password in that struct again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM