Merge branch 'feature/esp_event_loop_examples_wifi' into 'master'

examples/wifi,ethernet,system: use esp_event library for event handling

See merge request idf/esp-idf!3765
This commit is contained in:
Ivan Grokhotkov
2019-04-12 13:55:15 +08:00
13 changed files with 241 additions and 274 deletions

View File

@@ -12,6 +12,7 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_event_loop.h" #include "esp_event_loop.h"
#include "esp_err.h"
#include "esp_event.h" #include "esp_event.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_eth.h" #include "esp_eth.h"
@@ -92,51 +93,48 @@ static void eth_gpio_config_rmii(void)
phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO); phy_rmii_smi_configure_pins(PIN_SMI_MDC, PIN_SMI_MDIO);
} }
/** /** Event handler for Ethernet events */
* @brief event handler for ethernet static void eth_event_handler(void* arg, esp_event_base_t event_base,
* int32_t event_id, void* event_data)
* @param ctx
* @param event
* @return esp_err_t
*/
static esp_err_t eth_event_handler(void *ctx, system_event_t *event)
{ {
tcpip_adapter_ip_info_t ip; switch (event_id) {
case ETHERNET_EVENT_CONNECTED:
switch (event->event_id) {
case SYSTEM_EVENT_ETH_CONNECTED:
ESP_LOGI(TAG, "Ethernet Link Up"); ESP_LOGI(TAG, "Ethernet Link Up");
break; break;
case SYSTEM_EVENT_ETH_DISCONNECTED: case ETHERNET_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "Ethernet Link Down"); ESP_LOGI(TAG, "Ethernet Link Down");
break; break;
case SYSTEM_EVENT_ETH_START: case ETHERNET_EVENT_START:
ESP_LOGI(TAG, "Ethernet Started"); ESP_LOGI(TAG, "Ethernet Started");
break; break;
case SYSTEM_EVENT_ETH_GOT_IP: case ETHERNET_EVENT_STOP:
memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip));
ESP_LOGI(TAG, "Ethernet Got IP Addr");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip.ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip.netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip.gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
break;
case SYSTEM_EVENT_ETH_STOP:
ESP_LOGI(TAG, "Ethernet Stopped"); ESP_LOGI(TAG, "Ethernet Stopped");
break; break;
default: default:
break; break;
} }
return ESP_OK; }
/** Event handler for IP_EVENT_ETH_GOT_IP */
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;
const tcpip_adapter_ip_info_t* ip_info = &event->ip_info;
ESP_LOGI(TAG, "Ethernet Got IP Address");
ESP_LOGI(TAG, "~~~~~~~~~~~");
ESP_LOGI(TAG, "ETHIP:" IPSTR, IP2STR(&ip_info->ip));
ESP_LOGI(TAG, "ETHMASK:" IPSTR, IP2STR(&ip_info->netmask));
ESP_LOGI(TAG, "ETHGW:" IPSTR, IP2STR(&ip_info->gw));
ESP_LOGI(TAG, "~~~~~~~~~~~");
} }
void app_main() void app_main()
{ {
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
config.phy_addr = CONFIG_PHY_ADDRESS; config.phy_addr = CONFIG_PHY_ADDRESS;
@@ -150,5 +148,7 @@ void app_main()
#endif #endif
ESP_ERROR_CHECK(esp_eth_init(&config)); ESP_ERROR_CHECK(esp_eth_init(&config));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &eth_event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &got_ip_event_handler, NULL));
ESP_ERROR_CHECK(esp_eth_enable()) ; ESP_ERROR_CHECK(esp_eth_enable()) ;
} }

View File

@@ -13,7 +13,7 @@
#include "tcpip_adapter.h" #include "tcpip_adapter.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_console.h" #include "esp_console.h"
#include "esp_event_loop.h" #include "esp_event.h"
#include "esp_eth.h" #include "esp_eth.h"
#include "argtable3/argtable3.h" #include "argtable3/argtable3.h"
#include "iperf.h" #include "iperf.h"
@@ -216,31 +216,26 @@ static int eth_cmd_iperf(int argc, char **argv)
return 0; return 0;
} }
static esp_err_t eth_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 == ETH_EVENT && event_id == ETHERNET_EVENT_START) {
case SYSTEM_EVENT_ETH_START:
started = true; started = true;
break; } else if (event_base == ETH_EVENT && event_id == ETHERNET_EVENT_STOP) {
case SYSTEM_EVENT_ETH_GOT_IP:
memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(ESP_IF_ETH, &ip));
xEventGroupSetBits(eth_event_group, GOTIP_BIT);
break;
case SYSTEM_EVENT_ETH_STOP:
xEventGroupClearBits(eth_event_group, GOTIP_BIT); xEventGroupClearBits(eth_event_group, GOTIP_BIT);
started = false; started = false;
default: } else if (event_base == IP_EVENT && event_id == IP_EVENT_ETH_GOT_IP) {
break; ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
memcpy(&ip, &event->ip_info, sizeof(ip));
xEventGroupSetBits(eth_event_group, GOTIP_BIT);
} }
return ESP_OK;
} }
void register_ethernet() void register_ethernet()
{ {
eth_event_group = xEventGroupCreate(); eth_event_group = xEventGroupCreate();
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(eth_event_handler, NULL)); ESP_ERROR_CHECK(esp_event_loop_create_default());
eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG; eth_config_t config = DEFAULT_ETHERNET_PHY_CONFIG;
config.phy_addr = CONFIG_PHY_ADDRESS; config.phy_addr = CONFIG_PHY_ADDRESS;
@@ -254,6 +249,8 @@ void register_ethernet()
#endif #endif
ESP_ERROR_CHECK(esp_eth_init(&config)); ESP_ERROR_CHECK(esp_eth_init(&config));
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &event_handler, NULL));
eth_control_args.control = arg_str1(NULL, NULL, "<start|stop|info>", "Start/Stop Ethernet or Get info of Ethernet"); eth_control_args.control = arg_str1(NULL, NULL, "<start|stop|info>", "Start/Stop Ethernet or Get info of Ethernet");
eth_control_args.end = arg_end(1); eth_control_args.end = arg_end(1);

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 "cmd_wifi.h" #include "cmd_wifi.h"
#define JOIN_TIMEOUT_MS (10000) #define JOIN_TIMEOUT_MS (10000)
@@ -25,20 +25,16 @@
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0; const int CONNECTED_BIT = BIT0;
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_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)
@@ -50,9 +46,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, WIFI_EVENT_STA_DISCONNECTED, &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) );
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

@@ -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: if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail");
} 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", ESP_LOGI(TAG, "got ip:%s",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); ip4addr_ntoa(&event->ip_info.ip));
s_retry_num = 0; s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
{
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG,"retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail\n");
break;
}
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,35 +73,30 @@ 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) { xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT);
case SYSTEM_EVENT_STA_GOT_IP: xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
xEventGroupClearBits(wifi_event_group, DISCONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_SCAN_DONE:
scan_done_handler();
ESP_LOGI(TAG, "sta scan done");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
if (reconnect) {
ESP_LOGI(TAG, "sta disconnect, reconnect...");
esp_wifi_connect();
} else {
ESP_LOGI(TAG, "sta disconnect");
}
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
} }
static void disconnect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (reconnect) {
ESP_LOGI(TAG, "sta disconnect, reconnect...");
esp_wifi_connect();
} else {
ESP_LOGI(TAG, "sta disconnect");
}
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
xEventGroupSetBits(wifi_event_group, DISCONNECTED_BIT);
}
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,43 +39,37 @@
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,
.password = DEFAULT_PWD, .password = DEFAULT_PWD,
.listen_interval = DEFAULT_LISTEN_INTERVAL, .listen_interval = DEFAULT_LISTEN_INTERVAL,
}, },
}; };
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));

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", ESP_ERROR_CHECK(esp_wifi_connect());
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); break;
break; case WIFI_EVENT_STA_WPS_ER_SUCCESS:
case SYSTEM_EVENT_STA_DISCONNECTED: ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_SUCCESS");
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED"); /* esp_wifi_wps_start() only gets ssid & password, so call esp_wifi_connect() here. */
ESP_ERROR_CHECK(esp_wifi_connect()); ESP_ERROR_CHECK(esp_wifi_wps_disable());
break; ESP_ERROR_CHECK(esp_wifi_connect());
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS: break;
/*point: the function esp_wifi_wps_start() only get ssid & password case WIFI_EVENT_STA_WPS_ER_FAILED:
* so call the function esp_wifi_connect() here ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_FAILED");
* */ ESP_ERROR_CHECK(esp_wifi_wps_disable());
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_SUCCESS"); ESP_ERROR_CHECK(esp_wifi_wps_enable(&config));
ESP_ERROR_CHECK(esp_wifi_wps_disable()); ESP_ERROR_CHECK(esp_wifi_wps_start(0));
ESP_ERROR_CHECK(esp_wifi_connect()); break;
break; case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
case SYSTEM_EVENT_STA_WPS_ER_FAILED: ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_TIMEOUT");
ESP_LOGI(TAG, "SYSTEM_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 WIFI_EVENT_STA_WPS_ER_PIN:
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT: ESP_LOGI(TAG, "WIFI_EVENT_STA_WPS_ER_PIN");
ESP_LOGI(TAG, "SYSTEM_EVENT_STA_WPS_ER_TIMEOUT"); /* display the PIN code */
ESP_ERROR_CHECK(esp_wifi_wps_disable()); wifi_event_sta_wps_er_pin_t* event = (wifi_event_sta_wps_er_pin_t*) event_data;
ESP_ERROR_CHECK(esp_wifi_wps_enable(&config)); ESP_LOGI(TAG, "WPS_PIN = " PINSTR, PIN2STR(event->pin_code));
ESP_ERROR_CHECK(esp_wifi_wps_start(0)); break;
break; default:
case SYSTEM_EVENT_STA_WPS_ER_PIN: break;
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; }
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));
} }