AutoConnect
AutoConnect copied to clipboard
Attempt to initialize WPS before AutoConnect initialization
Hello. I am working on ESP32 project involving ability to connect via WPS to WiFi router (ESP32 works in station mode) as one of desired functions. Unfortunately, WPS is not implemented in AutoConnect so I am attempting to use it like this:
- Do stuff related to WPS: initialize, wait for connection.
- Initialize AutoConnect after WIFI_EVENT_STA_WPS_ER_SUCCESS. But as I run this code:
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_wps.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include <string.h>
#include <Arduino.h>
#include <WiFi.h>
#include <AutoConnect.h>
#define CONFIG_EXAMPLE_WPS_TYPE_PBC 1
/*set wps mode via project configuration */
#if CONFIG_EXAMPLE_WPS_TYPE_PBC
#define WPS_MODE WPS_TYPE_PBC
#elif CONFIG_EXAMPLE_WPS_TYPE_PIN
#define WPS_MODE WPS_TYPE_PIN
#else
#define WPS_MODE WPS_TYPE_DISABLE
#endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
#define MAX_RETRY_ATTEMPTS 1
#ifndef PIN2STR
#define PIN2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
#define PINSTR "%c%c%c%c%c%c%c%c"
#endif
static const char *TAG = "example_wps";
static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_MODE);
static wifi_config_t wps_ap_creds[MAX_WPS_AP_CRED];
static int s_ap_creds_num = 0;
static int s_retry_num = 0;
// Autoconnect
bool acn_ini = false, wl_cn = false;
WebServer Server;
AutoConnect Portal;
void onConnect(IPAddress& ipaddr) {
Serial.println("Autoconnect 'onConnect'");
Serial.print("WiFi connected with ");
Serial.print(WiFi.SSID());
Serial.print(", IP:");
Serial.println(ipaddr.toString());
}
void rootPage() {
char content[] = "Hello, world";
Server.send(200, "text/plain", content);
}
void Start_ACN() {
Portal.onConnect(onConnect); // Register the ConnectExit function
Server.on("/", rootPage);
Portal.begin();
}
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
static int ap_idx = 1;
switch (event_id) {
case WIFI_EVENT_STA_START:
Serial.println("WIFI_EVENT_STA_START");
ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
ESP_LOGI(TAG, "\n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
Serial.println("WIFI_EVENT_STA_DISCONNECTED");
acn_ini = wl_cn = false;
ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
if (s_retry_num < MAX_RETRY_ATTEMPTS) {
esp_wifi_connect();
s_retry_num++;
} else if (ap_idx < s_ap_creds_num) {
/* Try the next AP credential if first one fails */
if (ap_idx < s_ap_creds_num) {
Serial.print("Connecting to SSID: ");
Serial.print((char*) wps_ap_creds[ap_idx].sta.ssid);
Serial.print(", Password: ");
Serial.println((char*) wps_ap_creds[ap_idx].sta.password);
ESP_LOGI(TAG, "Connecting to SSID: %s, Passphrase: %s",
wps_ap_creds[ap_idx].sta.ssid, wps_ap_creds[ap_idx].sta.password);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wps_ap_creds[ap_idx++]) );
esp_wifi_connect();
}
s_retry_num = 0;
} else {
Serial.println("Failed to connect!");
ESP_LOGI(TAG, "Failed to connect!");
ESP_LOGI(TAG, "\n");
}
break;
case WIFI_EVENT_STA_WPS_ER_SUCCESS:
Serial.println("WIFI_EVENT_STA_WPS_ER_SUCCESS");
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
{
wifi_event_sta_wps_er_success_t *evt =
(wifi_event_sta_wps_er_success_t *)event_data;
int i;
if (evt) {
s_ap_creds_num = evt->ap_cred_cnt;
for (i = 0; i < s_ap_creds_num; i++) {
memcpy(wps_ap_creds[i].sta.ssid, evt->ap_cred[i].ssid,
sizeof(evt->ap_cred[i].ssid));
memcpy(wps_ap_creds[i].sta.password, evt->ap_cred[i].passphrase,
sizeof(evt->ap_cred[i].passphrase));
}
/* If multiple AP credentials are received from WPS, connect with first one */
Serial.print("Connecting to SSID: ");
Serial.print((char*) wps_ap_creds[0].sta.ssid);
Serial.print(", Password: ");
Serial.println((char*) wps_ap_creds[0].sta.password);
ESP_LOGI(TAG, "Connecting to SSID: %s, Passphrase: %s",
wps_ap_creds[0].sta.ssid, wps_ap_creds[0].sta.password);
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wps_ap_creds[0]) );
}
/*
* If only one AP credential is received from WPS, there will be no event data and
* esp_wifi_set_config() is already called by WPS modules for backward compatibility
* with legacy apps. So directly attempt connection here.
*/
ESP_ERROR_CHECK(esp_wifi_wps_disable());
esp_wifi_connect();
wl_cn = true;
}
break;
case WIFI_EVENT_STA_WPS_ER_FAILED:
Serial.println("WIFI_EVENT_STA_WPS_ER_FAILED");
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED");
ESP_ERROR_CHECK(esp_wifi_wps_disable());
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
ESP_LOGI(TAG, "\n");
break;
case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
Serial.println("WIFI_EVENT_STA_WPS_ER_TIMEOUT");
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT");
ESP_ERROR_CHECK(esp_wifi_wps_disable());
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
break;
case WIFI_EVENT_STA_WPS_ER_PIN:
Serial.println("WIFI_EVENT_STA_WPS_ER_PIN");
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_PIN");
/* display the PIN code */
wifi_event_sta_wps_er_pin_t* event = (wifi_event_sta_wps_er_pin_t*) event_data;
ESP_LOGI(TAG, "WPS_PIN = " PINSTR, PIN2STR(event->pin_code));
break;
}
}
static void got_ip_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
Serial.printf("got ip: %i:%i:%i:%i\n", IP2STR(&event->ip_info.ip));
ESP_LOGI(TAG, "got ip: " 1IPSTR, IP2STR(&event->ip_info.ip));
}
/*init wifi as sta and start wps*/
static void start_wps(void) {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_event_handler, NULL));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start());
Serial.println("start wps");
ESP_LOGI(TAG, "start wps...");
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Start");
ESP_LOGI(TAG, "ESP_LOGI test!!!\n");
/* Initialize NVS — it is used to store PHY calibration data */
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 );
start_wps();
}
void loop() {
// put your main code here, to run repeatedly:
delay(6000);
Serial.println("LP");
if (wl_cn) {
if (!acn_ini) {
Serial.println("Autoconnect initialization");
Start_ACN();
acn_ini = true;
}
Serial.println("WL_CN");
Portal.handleClient();
}
}
the error occures at the moment of AutoConnect initialization. The code is a bit altered example from: https://github.com/espressif/esp-idf/tree/master/examples/wifi/wps