2017-03-27 09:52:46 +00:00
|
|
|
/* WiFi Connection Example using WPS
|
2017-04-13 03:43:01 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2018-11-20 10:26:53 +00:00
|
|
|
This example demonstrates how to use WPS.
|
|
|
|
It supports two modes, which can be selected in menuconfig.
|
2017-04-13 03:43:01 +00:00
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
WPS_TYPE_PBC:
|
|
|
|
Start ESP32 and it will enter WPS PBC mode. Then push WPS button on the router.
|
|
|
|
ESP32 will receive SSID and password, and connect to the router.
|
|
|
|
|
|
|
|
WPS_TYPE_PIN:
|
|
|
|
Start ESP32, you'll see an eight-digit PIN number in log output.
|
|
|
|
Enter the PIN code on the router and then the ESP32 will get connected to router.
|
2017-04-13 03:43:01 +00:00
|
|
|
*/
|
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_wps.h"
|
2018-11-20 10:26:53 +00:00
|
|
|
#include "esp_event.h"
|
2017-07-13 15:46:19 +00:00
|
|
|
#include "nvs_flash.h"
|
2020-06-10 08:51:57 +00:00
|
|
|
#include <string.h>
|
2017-03-27 09:52:46 +00:00
|
|
|
|
|
|
|
|
2019-06-23 01:54:31 +00:00
|
|
|
/*set wps mode via project configuration */
|
2017-03-29 07:33:05 +00:00
|
|
|
#if CONFIG_EXAMPLE_WPS_TYPE_PBC
|
2018-11-20 10:26:53 +00:00
|
|
|
#define WPS_MODE WPS_TYPE_PBC
|
2017-03-29 07:33:05 +00:00
|
|
|
#elif CONFIG_EXAMPLE_WPS_TYPE_PIN
|
2018-11-20 10:26:53 +00:00
|
|
|
#define WPS_MODE WPS_TYPE_PIN
|
2017-03-29 07:33:05 +00:00
|
|
|
#else
|
2018-11-20 10:26:53 +00:00
|
|
|
#define WPS_MODE WPS_TYPE_DISABLE
|
2017-04-13 03:43:01 +00:00
|
|
|
#endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
|
|
|
|
|
2020-06-10 08:51:57 +00:00
|
|
|
#define MAX_RETRY_ATTEMPTS 2
|
2017-03-27 09:52:46 +00:00
|
|
|
|
|
|
|
#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";
|
2018-11-20 10:26:53 +00:00
|
|
|
static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_MODE);
|
2020-06-10 08:51:57 +00:00
|
|
|
static wifi_config_t wps_ap_creds[MAX_WPS_AP_CRED];
|
|
|
|
static int s_ap_creds_num = 0;
|
|
|
|
static int s_retry_num = 0;
|
2017-03-27 09:52:46 +00:00
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2017-03-27 09:52:46 +00:00
|
|
|
{
|
2020-06-10 08:51:57 +00:00
|
|
|
static int ap_idx = 1;
|
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
switch (event_id) {
|
|
|
|
case WIFI_EVENT_STA_START:
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
|
|
|
|
break;
|
|
|
|
case WIFI_EVENT_STA_DISCONNECTED:
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
|
2020-06-10 08:51:57 +00:00
|
|
|
if (s_retry_num < MAX_RETRY_ATTEMPTS) {
|
|
|
|
ESP_ERROR_CHECK(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) {
|
|
|
|
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(ESP_IF_WIFI_STA, &wps_ap_creds[ap_idx++]) );
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
|
|
}
|
|
|
|
s_retry_num = 0;
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Failed to connect!");
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
break;
|
|
|
|
case WIFI_EVENT_STA_WPS_ER_SUCCESS:
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
|
2020-06-10 08:51:57 +00:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
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(ESP_IF_WIFI_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_ERROR_CHECK(esp_wifi_connect());
|
|
|
|
}
|
2018-11-20 10:26:53 +00:00
|
|
|
break;
|
|
|
|
case 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));
|
|
|
|
break;
|
|
|
|
case 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:
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
break;
|
2017-03-27 09:52:46 +00:00
|
|
|
}
|
2018-11-20 10:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-08-20 12:40:34 +00:00
|
|
|
ESP_LOGI(TAG, "got ip: " IPSTR, IP2STR(&event->ip_info.ip));
|
2017-03-27 09:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*init wifi as sta and start wps*/
|
|
|
|
static void start_wps(void)
|
|
|
|
{
|
2019-11-29 09:54:02 +00:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 10:26:53 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-10-17 09:36:34 +00:00
|
|
|
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
|
|
|
|
assert(sta_netif);
|
2017-03-27 09:52:46 +00:00
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 10:26:53 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2018-11-20 10:26:53 +00:00
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
ESP_LOGI(TAG, "start wps...");
|
2018-11-20 10:26:53 +00:00
|
|
|
|
2017-08-01 14:29:16 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
|
2017-03-27 09:52:46 +00:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_wps_start(0));
|
|
|
|
}
|
|
|
|
|
2019-07-16 09:33:30 +00:00
|
|
|
void app_main(void)
|
2017-03-27 09:52:46 +00:00
|
|
|
{
|
2017-07-13 15:46:19 +00:00
|
|
|
/* Initialize NVS — it is used to store PHY calibration data */
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2017-07-13 15:46:19 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2017-03-27 09:52:46 +00:00
|
|
|
start_wps();
|
|
|
|
}
|