examples/wifi: use esp_event library to handle events

This commit is contained in:
Ivan Grokhotkov
2018-11-20 18:26:53 +08:00
parent 11355c4162
commit b4056560c0
10 changed files with 192 additions and 220 deletions

View File

@@ -20,7 +20,7 @@
#include "freertos/semphr.h" #include "freertos/semphr.h"
#include "freertos/timers.h" #include "freertos/timers.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "tcpip_adapter.h" #include "tcpip_adapter.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
@@ -39,23 +39,11 @@ static uint16_t s_example_espnow_seq[EXAMPLE_ESPNOW_DATA_MAX] = { 0, 0 };
static void example_espnow_deinit(example_espnow_send_param_t *send_param); static void example_espnow_deinit(example_espnow_send_param_t *send_param);
static esp_err_t example_event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
ESP_LOGI(TAG, "WiFi started");
break;
default:
break;
}
return ESP_OK;
}
/* WiFi should start before using ESPNOW */ /* WiFi should start before using ESPNOW */
static void example_wifi_init(void) static void example_wifi_init(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(example_event_handler, NULL) ); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );

View File

@@ -12,7 +12,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@@ -33,23 +33,18 @@ static EventGroupHandle_t s_wifi_event_group;
static const char *TAG = "wifi softAP"; static const char *TAG = "wifi softAP";
static esp_err_t event_handler(void *ctx, system_event_t *event) static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) { if (event_id == WIFI_EVENT_AP_STACONNECTED) {
case SYSTEM_EVENT_AP_STACONNECTED: wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d", ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac), MAC2STR(event->mac), event->aid);
event->event_info.sta_connected.aid); } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
break; wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
case SYSTEM_EVENT_AP_STADISCONNECTED: ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d", MAC2STR(event->mac), event->aid);
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
default:
break;
} }
return ESP_OK;
} }
void wifi_init_softap() void wifi_init_softap()
@@ -57,10 +52,13 @@ void wifi_init_softap()
s_wifi_event_group = xEventGroupCreate(); s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.ap = { .ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID, .ssid = EXAMPLE_ESP_WIFI_SSID,
@@ -78,7 +76,7 @@ void wifi_init_softap()
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s", ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} }

View File

@@ -12,7 +12,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@@ -39,33 +39,26 @@ static const char *TAG = "wifi station";
static int s_retry_num = 0; static int s_retry_num = 0;
static esp_err_t event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect(); esp_wifi_connect();
break; } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "got ip:%s",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) { if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect(); esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++; s_retry_num++;
ESP_LOGI(TAG,"retry to connect to the AP"); ESP_LOGI(TAG, "retry to connect to the AP");
} }
ESP_LOGI(TAG,"connect to the AP fail\n"); ESP_LOGI(TAG,"connect to the AP fail");
break; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
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));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} }
default:
break;
}
return ESP_OK;
} }
void wifi_init_sta() void wifi_init_sta()
@@ -73,17 +66,21 @@ void wifi_init_sta()
s_wifi_event_group = xEventGroupCreate(); s_wifi_event_group = xEventGroupCreate();
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID, .ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS .password = EXAMPLE_ESP_WIFI_PASS
}, },
}; };
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) ); ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() ); ESP_ERROR_CHECK(esp_wifi_start() );

View File

@@ -17,7 +17,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "tcpip_adapter.h" #include "tcpip_adapter.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "iperf.h" #include "iperf.h"
typedef struct { typedef struct {
@@ -53,7 +53,8 @@ static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0; const int CONNECTED_BIT = BIT0;
const int DISCONNECTED_BIT = BIT1; const int DISCONNECTED_BIT = BIT1;
static void scan_done_handler(void) static void scan_done_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
uint16_t sta_number = 0; uint16_t sta_number = 0;
uint8_t i; uint8_t i;
@@ -72,20 +73,19 @@ static void scan_done_handler(void)
} }
} }
free(ap_list_buffer); free(ap_list_buffer);
ESP_LOGI(TAG, "sta scan done");
} }
static esp_err_t event_handler(void *ctx, system_event_t *event) static void got_ip_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) {
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT); xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break; }
case SYSTEM_EVENT_SCAN_DONE:
scan_done_handler(); static void disconnect_handler(void* arg, esp_event_base_t event_base,
ESP_LOGI(TAG, "sta scan done"); int32_t event_id, void* event_data)
break; {
case SYSTEM_EVENT_STA_DISCONNECTED:
if (reconnect) { if (reconnect) {
ESP_LOGI(TAG, "sta disconnect, reconnect..."); ESP_LOGI(TAG, "sta disconnect, reconnect...");
esp_wifi_connect(); esp_wifi_connect();
@@ -94,13 +94,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
} }
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT); xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
} }
void initialise_wifi(void) void initialise_wifi(void)
{ {
esp_log_level_set("wifi", ESP_LOG_WARN); esp_log_level_set("wifi", ESP_LOG_WARN);
@@ -112,9 +108,12 @@ void initialise_wifi(void)
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate(); wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); ESP_ERROR_CHECK( esp_event_loop_create_default() );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_SCAN_DONE, &scan_done_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &got_ip_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
ESP_ERROR_CHECK( esp_wifi_start() ); ESP_ERROR_CHECK( esp_wifi_start() );

View File

@@ -16,7 +16,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_pm.h" #include "esp_pm.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@@ -39,37 +39,31 @@
static const char *TAG = "power_save"; static const char *TAG = "power_save";
static void event_handler(void* arg, esp_event_base_t event_base,
static esp_err_t event_handler(void *ctx, system_event_t *event) int32_t event_id, void* event_data)
{ {
switch(event->event_id) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
case SYSTEM_EVENT_STA_START: esp_wifi_connect();
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_ERROR_CHECK(esp_wifi_connect()); esp_wifi_connect();
break; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
case SYSTEM_EVENT_STA_GOT_IP: ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.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;
default:
break;
} }
return ESP_OK;
} }
/*init wifi as sta and set power save mode*/ /*init wifi as sta and set power save mode*/
static void wifi_power_save(void) static void wifi_power_save(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = DEFAULT_SSID, .ssid = DEFAULT_SSID,

View File

@@ -25,7 +25,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "nvs_flash.h" #include "nvs_flash.h"
/*Set the SSID and Password via "make menuconfig"*/ /*Set the SSID and Password via "make menuconfig"*/
@@ -68,36 +68,32 @@
static const char *TAG = "scan"; static const char *TAG = "scan";
static esp_err_t event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch (event->event_id) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
case SYSTEM_EVENT_STA_START: esp_wifi_connect();
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
ESP_ERROR_CHECK(esp_wifi_connect()); esp_wifi_connect();
break; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
case SYSTEM_EVENT_STA_GOT_IP: ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); ESP_LOGI(TAG, "got ip: %s", ip4addr_ntoa(&event->ip_info.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;
default:
break;
} }
return ESP_OK;
} }
/* Initialize Wi-Fi as sta and set scan method */ /* Initialize Wi-Fi as sta and set scan method */
static void wifi_scan(void) static void wifi_scan(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {
.ssid = DEFAULT_SSID, .ssid = DEFAULT_SSID,

View File

@@ -13,7 +13,7 @@
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "tcpip_adapter.h" #include "tcpip_adapter.h"
#include "esp_console.h" #include "esp_console.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_vfs_dev.h" #include "esp_vfs_dev.h"
#include "esp_vfs_fat.h" #include "esp_vfs_fat.h"
#include "esp_wifi.h" #include "esp_wifi.h"
@@ -65,7 +65,7 @@ static void initialize_nvs()
static void initialize_wifi() static void initialize_wifi()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(NULL, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));

View File

@@ -14,7 +14,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_wpa2.h" #include "esp_wpa2.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@@ -31,36 +31,33 @@ static const int CONNECTED_BIT = BIT0;
static const int ESPTOUCH_DONE_BIT = BIT1; static const int ESPTOUCH_DONE_BIT = BIT1;
static const char *TAG = "sc"; static const char *TAG = "sc";
void smartconfig_example_task(void * parm); static void smartconfig_example_task(void * parm);
static esp_err_t event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
case SYSTEM_EVENT_STA_START:
xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL); xTaskCreate(smartconfig_example_task, "smartconfig_example_task", 4096, NULL, 3, NULL);
break; } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect(); esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT); xEventGroupClearBits(s_wifi_event_group, CONNECTED_BIT);
break; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
default: xEventGroupSetBits(s_wifi_event_group, CONNECTED_BIT);
break;
} }
return ESP_OK;
} }
static void initialise_wifi(void) static void initialise_wifi(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
s_wifi_event_group = xEventGroupCreate(); s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) ); ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() ); ESP_ERROR_CHECK( esp_wifi_start() );
} }
@@ -100,7 +97,7 @@ static void sc_callback(smartconfig_status_t status, void *pdata)
} }
} }
void smartconfig_example_task(void * parm) static void smartconfig_example_task(void * parm)
{ {
EventBits_t uxBits; EventBits_t uxBits;
ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) ); ESP_ERROR_CHECK( esp_smartconfig_set_type(SC_TYPE_ESPTOUCH) );

View File

@@ -23,7 +23,7 @@
#include "freertos/event_groups.h" #include "freertos/event_groups.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_wpa2.h" #include "esp_wpa2.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h" #include "esp_system.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@@ -76,23 +76,17 @@ extern uint8_t client_crt_end[] asm("_binary_wpa2_client_crt_end");
extern uint8_t client_key_start[] asm("_binary_wpa2_client_key_start"); extern uint8_t client_key_start[] asm("_binary_wpa2_client_key_start");
extern uint8_t client_key_end[] asm("_binary_wpa2_client_key_end"); extern uint8_t client_key_end[] asm("_binary_wpa2_client_key_end");
static esp_err_t event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect(); esp_wifi_connect();
break; } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect(); esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break; } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
default: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
} }
return ESP_OK;
} }
static void initialise_wifi(void) static void initialise_wifi(void)
@@ -104,9 +98,11 @@ static void initialise_wifi(void)
tcpip_adapter_init(); tcpip_adapter_init();
wifi_event_group = xEventGroupCreate(); wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) ); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) ); ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = { wifi_config_t wifi_config = {
.sta = { .sta = {

View File

@@ -8,10 +8,16 @@
*/ */
/* /*
Showing how to use WPS. This example demonstrates how to use WPS.
It supports two modes, which can be selected in menuconfig.
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. WPS_TYPE_PBC:
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. 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.
*/ */
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
@@ -19,17 +25,17 @@
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_wps.h" #include "esp_wps.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "nvs_flash.h" #include "nvs_flash.h"
/*set wps mode via "make menuconfig"*/ /*set wps mode via "make menuconfig"*/
#if CONFIG_EXAMPLE_WPS_TYPE_PBC #if CONFIG_EXAMPLE_WPS_TYPE_PBC
#define WPS_TEST_MODE WPS_TYPE_PBC #define WPS_MODE WPS_TYPE_PBC
#elif CONFIG_EXAMPLE_WPS_TYPE_PIN #elif CONFIG_EXAMPLE_WPS_TYPE_PIN
#define WPS_TEST_MODE WPS_TYPE_PIN #define WPS_MODE WPS_TYPE_PIN
#else #else
#define WPS_TEST_MODE WPS_TYPE_DISABLE #define WPS_MODE WPS_TYPE_DISABLE
#endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/ #endif /*CONFIG_EXAMPLE_WPS_TYPE_PBC*/
@@ -38,72 +44,73 @@
#define PINSTR "%c%c%c%c%c%c%c%c" #define PINSTR "%c%c%c%c%c%c%c%c"
#endif #endif
static const char *TAG = "example_wps"; static const char *TAG = "example_wps";
static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_TEST_MODE); static esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(WPS_MODE);
static esp_err_t event_handler(void *ctx, system_event_t *event) static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{ {
switch(event->event_id) { switch (event_id) {
case SYSTEM_EVENT_STA_START: case WIFI_EVENT_STA_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START"); ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
break; break;
case SYSTEM_EVENT_STA_GOT_IP: case WIFI_EVENT_STA_DISCONNECTED:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP"); ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
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()); ESP_ERROR_CHECK(esp_wifi_connect());
break; break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: case WIFI_EVENT_STA_WPS_ER_SUCCESS:
/*point: the function esp_wifi_wps_start() only get ssid & password ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
* so call the function esp_wifi_connect() here /* esp_wifi_wps_start() only gets ssid & password, so call 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_wps_disable());
ESP_ERROR_CHECK(esp_wifi_connect()); ESP_ERROR_CHECK(esp_wifi_connect());
break; break;
case SYSTEM_EVENT_STA_WPS_ER_FAILED: case WIFI_EVENT_STA_WPS_ER_FAILED:
ESP_LOGI(TAG, "SYSTEM_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_disable());
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0)); ESP_ERROR_CHECK(esp_wifi_wps_start(0));
break; break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
ESP_LOGI(TAG, "SYSTEM_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_disable());
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0)); ESP_ERROR_CHECK(esp_wifi_wps_start(0));
break; break;
case SYSTEM_EVENT_STA_WPS_ER_PIN: case WIFI_EVENT_STA_WPS_ER_PIN:
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_PIN"); ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_PIN");
/*show the PIN code here*/ /* display the PIN code */
ESP_LOGI(TAG, "WPS_PIN = "PINSTR, PIN2STR(event->event_info.sta_er_pin.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; break;
default: default:
break; break;
} }
return ESP_OK; }
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*/ /*init wifi as sta and start wps*/
static void start_wps(void) static void start_wps(void)
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg)); 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_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_start()); ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "start wps..."); ESP_LOGI(TAG, "start wps...");
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_start(0)); ESP_ERROR_CHECK(esp_wifi_wps_start(0));
} }