From be4f25691f0ad77593f2270e8faac36d976fdfaf Mon Sep 17 00:00:00 2001 From: Zhang Jun Hao Date: Fri, 7 Sep 2018 19:25:02 +0800 Subject: [PATCH] feat(examples): add wps example --- examples/wifi/wps/Makefile | 9 ++ examples/wifi/wps/README.md | 11 +++ examples/wifi/wps/main/Kconfig.projbuild | 17 ++++ examples/wifi/wps/main/component.mk | 4 + examples/wifi/wps/main/wps.c | 120 +++++++++++++++++++++++ 5 files changed, 161 insertions(+) create mode 100644 examples/wifi/wps/Makefile create mode 100644 examples/wifi/wps/README.md create mode 100644 examples/wifi/wps/main/Kconfig.projbuild create mode 100644 examples/wifi/wps/main/component.mk create mode 100644 examples/wifi/wps/main/wps.c diff --git a/examples/wifi/wps/Makefile b/examples/wifi/wps/Makefile new file mode 100644 index 00000000..0fbaf221 --- /dev/null +++ b/examples/wifi/wps/Makefile @@ -0,0 +1,9 @@ +# +# This is a project Makefile. It is assumed the directory this Makefile resides in is a +# project subdirectory. +# + +PROJECT_NAME := wps_example + +include $(IDF_PATH)/make/project.mk + diff --git a/examples/wifi/wps/README.md b/examples/wifi/wps/README.md new file mode 100644 index 00000000..33add287 --- /dev/null +++ b/examples/wifi/wps/README.md @@ -0,0 +1,11 @@ +# Wifi WPS Example + +This example shows how to use wps enrollee in esp8266. + +Now we support to use enrollee feature in STA and APSTA mode. + +* PBC_MODE: Start esp8266 and press the wps button on router, Then esp8266 will get the ssid&password by wps PBC mode. + +* PIN_MODE: Start esp8266, It will enter wps mode and you'll see a pin code showing by COM. Enter this pin code in router and the esp8266 can get ssid&password by wps PIN mode. + +More info in the code [wps.c](./main/wps.c). diff --git a/examples/wifi/wps/main/Kconfig.projbuild b/examples/wifi/wps/main/Kconfig.projbuild new file mode 100644 index 00000000..d632af27 --- /dev/null +++ b/examples/wifi/wps/main/Kconfig.projbuild @@ -0,0 +1,17 @@ +menu "Example Configuration" + +choice EXAMPLE_WPS_TYPE + prompt "WPS mode" + default EXAMPLE_WPS_TYPE_PBC + help + WPS type for the esp8266 to use. + +config EXAMPLE_WPS_TYPE_PBC + bool "PBC" +config EXAMPLE_WPS_TYPE_PIN + bool "PIN" +config EXAMPLE_WPS_TYPE_DISABLE + bool "disable" +endchoice + +endmenu diff --git a/examples/wifi/wps/main/component.mk b/examples/wifi/wps/main/component.mk new file mode 100644 index 00000000..a98f634e --- /dev/null +++ b/examples/wifi/wps/main/component.mk @@ -0,0 +1,4 @@ +# +# "main" pseudo-component makefile. +# +# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) diff --git a/examples/wifi/wps/main/wps.c b/examples/wifi/wps/main/wps.c new file mode 100644 index 00000000..efbd9f27 --- /dev/null +++ b/examples/wifi/wps/main/wps.c @@ -0,0 +1,120 @@ +/* WiFi Connection Example using WPS + + 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 esp8266 and it will enter wps PBC mode. Then push the button of wps on router down. The esp8266 will connected to the router. + WPS_TYPE_PIN: Start esp8266, You'll see PIN code which is a eight-digit number showing on COM. Enter the PIN code in router and then the esp8266 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"*/ +#if CONFIG_EXAMPLE_WPS_TYPE_PBC +#define WPS_TEST_MODE WPS_TYPE_PBC +#elif CONFIG_EXAMPLE_WPS_TYPE_PIN +#define WPS_TEST_MODE WPS_TYPE_PIN +#else +#define WPS_TEST_MODE WPS_TYPE_DISABLE +#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] +#define PINSTR "%c%c%c%c%c%c%c%c" +#endif + +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) +{ + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); + break; + + case SYSTEM_EVENT_STA_GOT_IP: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); + ESP_LOGI(TAG, "got ip:%s\n", + ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); + break; + + case SYSTEM_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + + case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: + /*point: the function esp_wifi_wps_start() only get ssid & password + * so call the function esp_wifi_connect() here + * */ + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS"); + ESP_ERROR_CHECK(esp_wifi_wps_disable()); + ESP_ERROR_CHECK(esp_wifi_connect()); + break; + + 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(&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(&config)); + ESP_ERROR_CHECK(esp_wifi_wps_start(0)); + break; + + case SYSTEM_EVENT_STA_WPS_ER_PIN: + ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN"); + /*show the PIN code here*/ + ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.pin_code)); + break; + + default: + break; + } + + return ESP_OK; +} + +/*init wifi as sta and start wps*/ +static void start_wps(void) +{ + tcpip_adapter_init(); + 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(&config)); + ESP_ERROR_CHECK(esp_wifi_wps_start(0)); +} + +void app_main() +{ + /* Initialize NVS — it is used to store PHY calibration data */ + ESP_ERROR_CHECK(nvs_flash_init()); + start_wps(); +}