1 /* WiFi Connection Example using WPS
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
11 Showing how to use WPS.
13 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.
14 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.
17 #include "freertos/FreeRTOS.h"
18 #include "freertos/event_groups.h"
22 #include "esp_event_loop.h"
23 #include "nvs_flash.h"
26 /*set wps mode via "make menuconfig"*/
27 #if CONFIG_EXAMPLE_WPS_TYPE_PBC
28 #define WPS_TEST_MODE WPS_TYPE_PBC
29 #elif CONFIG_EXAMPLE_WPS_TYPE_PIN
30 #define WPS_TEST_MODE WPS_TYPE_PIN
32 #define WPS_TEST_MODE WPS_TYPE_DISABLE
33 #endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
37 #define PIN2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5], (a)[6], (a)[7]
38 #define PINSTR "%c%c%c%c%c%c%c%c"
42 static const char *TAG = "example_wps";
43 static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE);
45 static esp_err_t event_handler(void *ctx, system_event_t *event)
47 switch(event->event_id) {
48 case SYSTEM_EVENT_STA_START:
49 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
51 case SYSTEM_EVENT_STA_GOT_IP:
52 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
53 ESP_LOGI(TAG, "got ip:%s\n",
54 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
56 case SYSTEM_EVENT_STA_DISCONNECTED:
57 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
58 ESP_ERROR_CHECK(esp_wifi_connect());
60 case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
61 /*point: the function esp_wifi_wps_start() only get ssid & password
62 * so call the function esp_wifi_connect() here
64 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS");
65 ESP_ERROR_CHECK(esp_wifi_wps_disable());
66 ESP_ERROR_CHECK(esp_wifi_connect());
68 case SYSTEM_EVENT_STA_WPS_ER_FAILED:
69 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_FAILED");
70 ESP_ERROR_CHECK(esp_wifi_wps_disable());
71 ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
72 ESP_ERROR_CHECK(esp_wifi_wps_start(0));
74 case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
75 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT");
76 ESP_ERROR_CHECK(esp_wifi_wps_disable());
77 ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
78 ESP_ERROR_CHECK(esp_wifi_wps_start(0));
80 case SYSTEM_EVENT_STA_WPS_ER_PIN:
81 ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN");
82 /*show the PIN code here*/
83 ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.pin_code));
91 /*init wifi as sta and start wps*/
92 static void start_wps(void)
95 ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
97 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
99 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
101 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
102 ESP_ERROR_CHECK(esp_wifi_start());
104 ESP_LOGI(TAG, "start wps...");
107 ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
108 ESP_ERROR_CHECK(esp_wifi_wps_start(0));
113 /* Initialize NVS — it is used to store PHY calibration data */
114 esp_err_t ret = nvs_flash_init();
115 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
116 ESP_ERROR_CHECK(nvs_flash_erase());
117 ret = nvs_flash_init();
119 ESP_ERROR_CHECK( ret );