example: use example common componments in esp_local_ctrl

This commit is contained in:
Chen Yudong 2022-07-04 22:13:16 +08:00
parent 9575f03b8d
commit 4d9393fc53
6 changed files with 41 additions and 126 deletions

View File

@ -39,6 +39,13 @@ menu "Example Connection Configuration"
WiFi password (WPA or WPA2) for the example to use. WiFi password (WPA or WPA2) for the example to use.
Can be left blank if the network has no security set. Can be left blank if the network has no security set.
config EXAMPLE_WIFI_CONN_MAX_RETRY
int "Maximum retry"
default 6
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited,
in case the AP is really inexistent.
choice EXAMPLE_WIFI_SCAN_METHOD choice EXAMPLE_WIFI_SCAN_METHOD
prompt "WiFi Scan Method" prompt "WiFi Scan Method"
default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL default EXAMPLE_WIFI_SCAN_METHOD_ALL_CHANNEL

View File

@ -82,17 +82,22 @@ void example_print_all_netif_ips(const char *prefix)
esp_err_t example_connect(void) esp_err_t example_connect(void)
{ {
#if CONFIG_EXAMPLE_CONNECT_ETHERNET #if CONFIG_EXAMPLE_CONNECT_ETHERNET
example_ethernet_connect(); if (example_ethernet_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown)); ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_ethernet_shutdown));
#endif #endif
#if CONFIG_EXAMPLE_CONNECT_WIFI #if CONFIG_EXAMPLE_CONNECT_WIFI
example_wifi_connect(); if (example_wifi_connect() != ESP_OK) {
return ESP_FAIL;
}
ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown)); ESP_ERROR_CHECK(esp_register_shutdown_handler(&example_wifi_shutdown));
#endif #endif
#if CONFIG_EXAMPLE_CONNECT_ETHERNET #if CONFIG_EXAMPLE_CONNECT_ETHERNET
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH); example_print_all_netif_ips(EXAMPLE_NETIF_DESC_ETH);
#endif #endif
#if CONFIG_EXAMPLE_CONNECT_WIFI #if CONFIG_EXAMPLE_CONNECT_WIFI
example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA); example_print_all_netif_ips(EXAMPLE_NETIF_DESC_STA);
#endif #endif
@ -101,7 +106,6 @@ esp_err_t example_connect(void)
} }
esp_err_t example_disconnect(void) esp_err_t example_disconnect(void)
{ {
#if CONFIG_EXAMPLE_CONNECT_ETHERNET #if CONFIG_EXAMPLE_CONNECT_ETHERNET

View File

@ -55,10 +55,23 @@ static SemaphoreHandle_t s_semph_get_ip_addrs = NULL;
#define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK #define EXAMPLE_WIFI_SCAN_AUTH_MODE_THRESHOLD WIFI_AUTH_WAPI_PSK
#endif #endif
static int s_retry_num = 0;
static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base, static void example_handler_on_wifi_disconnect(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) int32_t event_id, void *event_data)
{ {
s_retry_num++;
if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
ESP_LOGI(TAG, "WiFi Connect failed %d times, stop reconnect.", s_retry_num);
if (s_semph_get_ip_addrs) {
/* let example_wifi_sta_do_connect() return */
xSemaphoreGive(s_semph_get_ip_addrs);
#if CONFIG_EXAMPLE_CONNECT_IPV6
xSemaphoreGive(s_semph_get_ip_addrs);
#endif
}
return;
}
ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect..."); ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
esp_err_t err = esp_wifi_connect(); esp_err_t err = esp_wifi_connect();
if (err == ESP_ERR_WIFI_NOT_STARTED) { if (err == ESP_ERR_WIFI_NOT_STARTED) {
@ -78,6 +91,7 @@ static void example_handler_on_wifi_connect(void *esp_netif, esp_event_base_t ev
static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base, static void example_handler_on_sta_got_ip(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) int32_t event_id, void *event_data)
{ {
s_retry_num = 0;
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) { if (!example_is_our_netif(EXAMPLE_NETIF_DESC_STA, event->esp_netif)) {
return; return;
@ -150,7 +164,7 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
if (wait) { if (wait) {
s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0); s_semph_get_ip_addrs = xSemaphoreCreateCounting(NR_OF_IP_ADDRESSES_TO_WAIT_FOR, 0);
} }
s_retry_num = 0;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &example_handler_on_wifi_disconnect, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &example_handler_on_sta_got_ip, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &example_handler_on_wifi_connect, s_example_sta_netif));
@ -170,6 +184,9 @@ esp_err_t example_wifi_sta_do_connect(wifi_config_t wifi_config, bool wait)
for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) { for (int i = 0; i < NR_OF_IP_ADDRESSES_TO_WAIT_FOR; ++i) {
xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY); xSemaphoreTake(s_semph_get_ip_addrs, portMAX_DELAY);
} }
if (s_retry_num > CONFIG_EXAMPLE_WIFI_CONN_MAX_RETRY) {
return ESP_FAIL;
}
} }
return ESP_OK; return ESP_OK;
} }
@ -228,8 +245,7 @@ esp_err_t example_wifi_connect(void)
wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN; wifi_config.sta.threshold.authmode = WIFI_AUTH_OPEN;
} }
#endif #endif
example_wifi_sta_do_connect(wifi_config, true); return example_wifi_sta_do_connect(wifi_config, true);
return ESP_OK;
} }

View File

@ -2,5 +2,9 @@
# in this exact order for cmake to work correctly # in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
include($ENV{IDF_PATH}/tools/cmake/project.cmake) include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(esp_local_ctrl) project(esp_local_ctrl)

View File

@ -1,20 +0,0 @@
menu "Example Configuration"
config EXAMPLE_WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config EXAMPLE_WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
config EXAMPLE_MAXIMUM_RETRY
int "Maximum retry"
default 5
help
Set the Maximum retry to avoid station reconnecting to the AP unlimited when the AP is really inexistent.
endmenu

View File

@ -17,110 +17,13 @@
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "protocol_examples_common.h"
#include "lwip/err.h" #include "lwip/err.h"
#include "lwip/sys.h" #include "lwip/sys.h"
/* The examples use WiFi configuration that you can set via 'idf.py menuconfig'.
If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS CONFIG_EXAMPLE_WIFI_PASSWORD
#define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_EXAMPLE_MAXIMUM_RETRY
/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;
/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
static const char *TAG = "local_ctrl_example"; static const char *TAG = "local_ctrl_example";
static int s_retry_num = 0;
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
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:" IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
esp_err_t wifi_init_sta(void)
{
esp_err_t ret_value = ESP_OK;
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
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, &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 = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
ret_value = ESP_FAIL;
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
ret_value = ESP_ERR_INVALID_STATE;
}
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
vEventGroupDelete(s_wifi_event_group);
return ret_value;
}
/* Function responsible for configuring and starting the esp_local_ctrl service. /* Function responsible for configuring and starting the esp_local_ctrl service.
* See local_ctrl_service.c for implementation */ * See local_ctrl_service.c for implementation */
@ -135,9 +38,10 @@ void app_main(void)
ret = nvs_flash_init(); ret = nvs_flash_init();
} }
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); if (example_connect() == ESP_OK) {
if (wifi_init_sta() == ESP_OK) {
start_esp_local_ctrl_service(); start_esp_local_ctrl_service();
} else { } else {
ESP_LOGI(TAG, "Connection failed, not starting esp_local_ctrl service"); ESP_LOGI(TAG, "Connection failed, not starting esp_local_ctrl service");