mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-05-28 05:08:23 +08:00
124 lines
4.5 KiB
C
124 lines
4.5 KiB
C
/* 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 void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
|
int32_t event_id, void* event_data)
|
|
{
|
|
switch (event_id) {
|
|
case WIFI_EVENT_STA_START:
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
|
|
break;
|
|
case WIFI_EVENT_STA_DISCONNECTED: {
|
|
system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data;
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
|
|
if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
|
|
/*Switch to 802.11 bgn mode */
|
|
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCOL_11B | WIFI_PROTOCOL_11G | WIFI_PROTOCOL_11N);
|
|
}
|
|
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
}
|
|
break;
|
|
case WIFI_EVENT_STA_WPS_ER_SUCCESS:
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
|
|
/* esp_wifi_wps_start() only gets ssid & password, so call esp_wifi_connect() here. */
|
|
ESP_ERROR_CHECK(esp_wifi_wps_disable());
|
|
ESP_ERROR_CHECK(esp_wifi_connect());
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.ip));
|
|
}
|
|
|
|
/*init wifi as sta and start wps*/
|
|
static void start_wps(void)
|
|
{
|
|
tcpip_adapter_init();
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
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));
|
|
|
|
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();
|
|
}
|