]> granicus.if.org Git - esp-idf/blob - examples/wifi/wps/main/wps.c
6fb8ad24683bafed472fd0be472a63e9ae41edd9
[esp-idf] / examples / wifi / wps / main / wps.c
1 /* WiFi Connection Example using WPS
2
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4
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.
8 */
9
10 /*
11    Showing how to use WPS.
12
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.
15 */
16
17 #include "freertos/FreeRTOS.h"
18 #include "freertos/event_groups.h"
19 #include "esp_wifi.h"
20 #include "esp_log.h"
21 #include "esp_wps.h"
22 #include "esp_event_loop.h"
23 #include "nvs_flash.h"
24
25
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
31 #else
32 #define WPS_TEST_MODE WPS_TYPE_DISABLE
33 #endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
34
35
36 #ifndef PIN2STR
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"
39 #endif
40
41
42 static const char *TAG = "example_wps";
43 static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE);
44
45 static esp_err_t event_handler(void *ctx, system_event_t *event)
46 {
47     switch(event->event_id) {
48     case SYSTEM_EVENT_STA_START:
49         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
50         break;
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));
55         break;
56     case SYSTEM_EVENT_STA_DISCONNECTED:
57         ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
58         ESP_ERROR_CHECK(esp_wifi_connect());
59         break;
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
63          * */
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());
67         break;
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));
73         break;
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));
79         break;
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));
84         break;
85     default:
86         break;
87     }
88     return ESP_OK;
89 }
90
91 /*init wifi as sta and start wps*/
92 static void start_wps(void)
93 {
94     tcpip_adapter_init();
95     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
96
97     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
98     
99     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
100     
101     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
102     ESP_ERROR_CHECK(esp_wifi_start());
103     
104     ESP_LOGI(TAG, "start wps...");
105     
106        
107     ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
108     ESP_ERROR_CHECK(esp_wifi_wps_start(0));
109 }
110
111 void app_main()
112 {
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();
118     }
119     ESP_ERROR_CHECK( ret );
120
121     start_wps();
122 }