I am trying to implement WPS on ESP32. A WT32-SC01 Plus to be exact. It seems to working so far that I'll get the WPS connection but then it takes around 5 minutes [!!!] to get IP address from the router. Also when the WPS connection was successful, after a reboot it do not reconnect but expects a new WPS initialization.
Can you take a look and tell me what am I doing wrong.
Here is my code:
#include "WiFi.h"
#include "esp_wps.h"
#define ESP_MANUFACTURER "ESPRESSIF"
#define ESP_MODEL_NUMBER "ESP32"
#define ESP_MODEL_NAME "ESPRESSIF IOT"
#define ESP_DEVICE_NAME "ESP STATION"
struct WiFiHandler
{
bool connected = false;
bool connecting = true;
bool wpsDone = false;
WiFiHandler()
{
WiFi.mode( WIFI_STA );
// WiFi.setAutoReconnect( false );
WiFi.onEvent( [ = ] ( WiFiEvent_t event, arduino_event_info_t info ) -> void
{
switch( event ) {
case ARDUINO_EVENT_WIFI_STA_START:
Output::Message( "Station Mode Started" );
break;
case ARDUINO_EVENT_WPS_ER_PIN:
Output::Message( "PIN: %d", info.wps_er_pin.pin_code );
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Output::StartUpMsg( "Connected" );
connected = true;
connecting = false;
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Output::Message( "Reconnecting" );
WiFi.reconnect();
break;
case ARDUINO_EVENT_WPS_ER_SUCCESS:
Output::StartUpMsg( "WPS Successfull." );
WiFi.disconnect();
wpsStop();
WiFi.begin();
wpsDone = true;
break;
case ARDUINO_EVENT_WPS_ER_FAILED:
Output::StartUpMsg( "WPS Failed!" );
connecting = false;
break;
case ARDUINO_EVENT_WPS_ER_TIMEOUT:
Output::StartUpMsg( "WPS Timedout!" );
connecting = false;
break;
}
} );
}
bool initialise()
{
int cnt = 0;
TaskHandle_t taskHandle = NULL;
xTaskCreate( this->blink, "WiFiConnect", 1000, this, 1, &taskHandle );
Output::StartUpMsg( "Establishing network connection ..." );
if( String( WiFi.SSID().c_str() ).length() ) {
WiFi.begin( WiFi.SSID().c_str(), WiFi.psk().c_str() );
while( ( WiFi.reconnect() != ESP_OK && !this->connected ) && ( ++cnt < 20 ) ) {
lv_bar_set_value( ui_BootProgress, lv_bar_get_value( ui_BootProgress ) + 1, LV_ANIM_ON );
delay( 100 );
}
}
if( !this->connected ) {
WiFi.begin();
Output::StartUpMsg( "Please initialise WPS connection on the router." );
cnt = 0;
if( wpsInit() ) {
if( esp_wifi_wps_start( 1000 ) != ESP_OK ) {
Output::StartUpMsg( "Can't start WPS" );
}
while( WiFi.status() != WL_CONNECTED )
{
delay( 1000 );
if( WiFi.status() == WL_IDLE_STATUS && wpsDone ) {
WiFi.reconnect();
}
// if( WiFi.status() == WL_CONNECTION_LOST && wpsDone ) {
// WiFi.disconnect();
// WiFi.begin( WiFi.SSID().c_str(), WiFi.psk().c_str() );
// }
Output::Message( WiFi.SSID().c_str() );
Output::Message( WiFi.localIP().toString().c_str() );
Output::Message( String( WiFi.status() ).c_str() );
}
delay( 2000 );
this->connected = true;
}
}
if( this->connected ) {
while ( !WiFi.SSID().length() )
{
lv_bar_set_value( ui_BootProgress, lv_bar_get_value( ui_BootProgress ) + 1, LV_ANIM_ON );
delay( 100 );
}
Output::StartUpMsg( "Connected to: " + WiFi.SSID() );
while ( !String( WiFi.localIP() ).length() || WiFi.localIP().toString() == "0.0.0.0" )
{
lv_bar_set_value( ui_BootProgress, lv_bar_get_value( ui_BootProgress ) + 1, LV_ANIM_ON );
delay( 100 );
}
Output::StartUpMsg( "IP: " + String( WiFi.localIP().toString() ) );
delay( 500 );
this->connecting = false;
this->connected = true;
}
return this->connected;
}
String getIP()
{
return String( WiFi.localIP().toString() );
}
String getPass()
{
return String( WiFi.psk().c_str() );
}
String getNetworkName()
{
return String( WiFi.SSID() );
}
String getMAC()
{
return String( WiFi.macAddress() );
}
static void blink( void *pvParameter )
{
WiFiHandler *l_pThis = ( WiFiHandler * ) pvParameter;
for( int i = 0; l_pThis->connecting; i++ ) {
i%2 ? lv_obj_add_flag( ui_WiFiStatus1, LV_OBJ_FLAG_HIDDEN ) : lv_obj_clear_flag( ui_WiFiStatus1, LV_OBJ_FLAG_HIDDEN );
delay( 250 );
}
l_pThis->connected ? lv_obj_clear_flag( ui_WiFiStatus1, LV_OBJ_FLAG_HIDDEN ) : lv_obj_add_flag( ui_WiFiStatus1, LV_OBJ_FLAG_HIDDEN );
vTaskDelete( NULL );
}
static bool wpsInit()
{
int cnt = 0;
esp_wps_config_t config;
config.wps_type = WPS_TYPE_PBC;
strcpy( config.factory_info.manufacturer, ESP_MANUFACTURER );
strcpy( config.factory_info.model_number, ESP_MODEL_NUMBER );
strcpy( config.factory_info.model_name, ESP_MODEL_NAME );
strcpy( config.factory_info.device_name, ESP_DEVICE_NAME );
esp_err_t status = esp_wifi_wps_enable( &config );
if( status != ESP_OK ) {
Output::StartUpMsg( "WPS config failed! " + String( esp_err_to_name( status ) ) );
return false;
}
return true;
}
static void wpsStop()
{
if( esp_wifi_wps_disable() ) {
Output::StartUpMsg( "WPS Disable Failed" );
}
}
};