]> granicus.if.org Git - esp-idf/blobdiff - examples/wifi/wps/main/wps.c
Merge branch 'bugfix/spiram_malloc_reserve_internal_fragments' into 'master'
[esp-idf] / examples / wifi / wps / main / wps.c
index e68ac04556e7475f1770a786f1f592213ca24207..50ab8de9bc0bb71f05d5113403cee19711e50efb 100644 (file)
@@ -1,15 +1,26 @@
 /* WiFi Connection Example using WPS
- * 
- * WPS_TYPE_PBC: Start esp32 and it will enter wps PBC mode. Then push the button of wps on router down. The esp32 will connected to the router.
- *
- * WPS_TYPE_PIN: Start esp32, You'll see PIN code which is a eight-digit number showing on COM. Enter the PIN code in router and then the esp32 will connected to router.
- */
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+/*
+   Showing how to use WPS.
+
+   WPS_TYPE_PBC: Start esp32 and it will enter wps PBC mode. Then push the button of wps on router down. The esp32 will connected to the router.
+   WPS_TYPE_PIN: Start esp32, You'll see PIN code which is a eight-digit number showing on COM. Enter the PIN code in router and then the esp32 will connected to router.
+*/
+
 #include "freertos/FreeRTOS.h"
 #include "freertos/event_groups.h"
 #include "esp_wifi.h"
 #include "esp_log.h"
 #include "esp_wps.h"
 #include "esp_event_loop.h"
+#include "nvs_flash.h"
 
 
 /*set wps mode via "make menuconfig"*/
@@ -19,7 +30,8 @@
 #define WPS_TEST_MODE WPS_TYPE_PIN
 #else
 #define WPS_TEST_MODE WPS_TYPE_DISABLE
-#endif
+#endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
+
 
 #ifndef PIN2STR
 #define PIN2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
@@ -28,7 +40,7 @@
 
 
 static const char *TAG = "example_wps";
-
+static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE);
 
 static esp_err_t event_handler(void *ctx, system_event_t *event)
 {
@@ -47,7 +59,7 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
         break;
     case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
        /*point: the function esp_wifi_wps_start() only get ssid & password
-        * and we suggest you call the function esp_wifi_connect() here
+        * so call the function esp_wifi_connect() here
         * */
        ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS");
        ESP_ERROR_CHECK(esp_wifi_wps_disable());
@@ -56,13 +68,13 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
     case SYSTEM_EVENT_STA_WPS_ER_FAILED:
        ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED");
        ESP_ERROR_CHECK(esp_wifi_wps_disable());
-       ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
+       ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
        ESP_ERROR_CHECK(esp_wifi_wps_start(0));
        break;
     case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
        ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT");
        ESP_ERROR_CHECK(esp_wifi_wps_disable());
-       ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
+       ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
        ESP_ERROR_CHECK(esp_wifi_wps_start(0));
        break;
     case SYSTEM_EVENT_STA_WPS_ER_PIN:
@@ -83,18 +95,28 @@ static void start_wps(void)
     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
 
     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    
     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
     
     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
     ESP_ERROR_CHECK(esp_wifi_start());
     
     ESP_LOGI(TAG, "start wps...");
-    //ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TYPE_PBC));
-    ESP_ERROR_CHECK(esp_wifi_wps_enable(WPS_TEST_MODE));
+    
+       
+    ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
     ESP_ERROR_CHECK(esp_wifi_wps_start(0));
 }
 
 void app_main()
 {
+    /* 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();
 }