Page 1 of 1

Triggering WPS_ER_PIN Somehow

Posted: Mon May 17, 2021 7:36 am
by acp_21
Hello,

After changing my project to support transitioning from AP mode to STA mode I've noticed some strange behavior with the wifi event loop. For unknown reasons, the event loop keeps receiving the SYSTEM_EVENT_STA_WPS_ER_PIN event.

Code: Select all

void initWifiController(){
    // * NVS must be initialized before wifi work can be done
    // Handle when connected to the network
    connectionSemaphore = xSemaphoreCreateBinary();
    // Begin network stack
    ESP_ERROR_CHECK(esp_netif_init());
    // Create event loop for handling callbacks
    ESP_ERROR_CHECK(esp_event_loop_create_default());
}
void beginWifi(Creds creds){
    if(creds.status == ESP_OK){
        ESP_LOGI(TAG, "Connection credentials have been found, connecting to network");
        connectSTA(creds);
    }
    else if(creds.status == ESP_ERR_NVS_NOT_FOUND){
        ESP_LOGW(TAG, "Missing credentials, starting as AP");
        connectAP();
    }
    else{
        ESP_LOGE(TAG, "ESP failed with error %s, not starting wifi", esp_err_to_name(creds.status));
    }
}
void connectSTA(Creds creds){
    
    ESP_LOGI(TAG, "Attempting to connect to wifi with following creds: %s | %s", creds.ssid, creds.pass);
    // ESP_LOGI("Attempting to connect to wifi");
    // Set netif to sta
    esp_netif_create_default_wifi_sta();

    // Prepare and initialize wifi_init_config_t
    wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));

    // Register tracked events for event_handler
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL));
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, event_handler, NULL));

    // TODO: Check if this can be used to avoid havng to use NVS manually
    esp_wifi_set_storage(WIFI_STORAGE_RAM);

    // Config struct for wifi details
    wifi_config_t wifi_config = {};

    // Copy casted info into wifi_config
    // * https://www.esp32.com/viewtopic.php?f=13&t=14611
    // * See above link for details on this
    strcpy((char *)wifi_config.sta.ssid, creds.ssid);
    strcpy((char *)wifi_config.sta.password, creds.pass);

    esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);

    ESP_ERROR_CHECK(esp_wifi_start());

    // ? Is this required to avoid a memory leak?
    free(creds.pass);
    free(creds.ssid);
}
My code essentially initializes the network stack and then checks if the credentials are stored in NVS. If they are, the firmware attempts to connect to the network. I can confirm that connectSTA() runs, it even logs the correct ssid and password, but whenever the event loop gets created, it receives a callback for SYSTEM_EVENT_STA_WPS_ER_PIN. I don't even know what that event id is, much less what could be causing it.

To make things stranger, even after implementing the code to support either STA or AP based on the credentials being present in NVS, the error didn't start until after I wiped the flash and tested the entire system.

If anyone has any idea what could be going on here, I would greatly appreciate any input you may have.