mirror of
https://github.com/espressif/esp-idf
synced 2025-03-10 09:39:10 -04:00
examples/provisioning : Call esp_wifi_init() only in main function before starting provisioning
Removed all other instances of call to esp_wifi_init(), because every time this is called it will override its previously set default event handler and hence cause numerous warnings. Also, call nvs_flash_init() only once, that is before calling esp_wifi_init() in main function
This commit is contained in:
parent
3608f9bde4
commit
d784fbb314
@ -82,9 +82,7 @@ static void wifi_init_sta()
|
||||
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));
|
||||
|
||||
/* Start wifi in station mode with credentials set during provisioning */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
/* Start Wi-Fi in station mode with credentials set during provisioning */
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
@ -121,6 +119,13 @@ void app_main()
|
||||
* main app and the provisioning service */
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
/* Initialize NVS needed by Wi-Fi */
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
/* Initialize Wi-Fi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
/* Check if device is provisioned */
|
||||
bool provisioned;
|
||||
if (app_prov_is_provisioned(&provisioned) != ESP_OK) {
|
||||
@ -135,6 +140,6 @@ void app_main()
|
||||
} else {
|
||||
/* Else start as station with credentials set during provisioning */
|
||||
ESP_LOGI(TAG, "Starting WiFi station");
|
||||
wifi_init_sta(NULL);
|
||||
wifi_init_sta();
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ static void app_prov_stop_service(void)
|
||||
|
||||
/* Remove event handler */
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler);
|
||||
|
||||
/* Release memory used by BT stack */
|
||||
esp_bt_mem_release(ESP_BT_MODE_BTDM);
|
||||
@ -257,17 +257,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
nvs_flash_erase();
|
||||
#endif
|
||||
|
||||
if (nvs_flash_init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init NVS");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init wifi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Get WiFi Station configuration */
|
||||
wifi_config_t wifi_cfg;
|
||||
if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) {
|
||||
@ -284,12 +273,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
|
||||
esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg)
|
||||
{
|
||||
/* Initialize WiFi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init WiFi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/* Configure WiFi as station */
|
||||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set WiFi mode");
|
||||
@ -358,7 +341,7 @@ esp_err_t app_prov_start_ble_provisioning(int security, const protocomm_security
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL);
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register IP event handler");
|
||||
return err;
|
||||
|
@ -53,9 +53,7 @@ static void wifi_init_sta()
|
||||
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));
|
||||
|
||||
/* Start wifi in station mode with credentials set during provisioning */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
/* Start Wi-Fi in station mode with credentials set during provisioning */
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
@ -92,6 +90,13 @@ void app_main()
|
||||
* main app and the provisioning service */
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
/* Initialize NVS needed by Wi-Fi */
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
/* Initialize Wi-Fi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
/* Check if device is provisioned */
|
||||
bool provisioned;
|
||||
if (app_prov_is_provisioned(&provisioned) != ESP_OK) {
|
||||
@ -106,6 +111,6 @@ void app_main()
|
||||
} else {
|
||||
/* Else start as station with credentials set during provisioning */
|
||||
ESP_LOGI(TAG, "Starting WiFi station");
|
||||
wifi_init_sta(NULL);
|
||||
wifi_init_sta();
|
||||
}
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ static void app_prov_stop_service(void)
|
||||
|
||||
/* Remove event handler */
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler);
|
||||
}
|
||||
|
||||
/* Task spawned by timer callback */
|
||||
@ -223,17 +223,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
nvs_flash_erase();
|
||||
#endif
|
||||
|
||||
if (nvs_flash_init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init NVS");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init wifi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Get WiFi Station configuration */
|
||||
wifi_config_t wifi_cfg;
|
||||
if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) {
|
||||
@ -250,12 +239,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
|
||||
esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg)
|
||||
{
|
||||
/* Initialize WiFi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init WiFi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
/* Configure WiFi as station */
|
||||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set WiFi mode");
|
||||
@ -324,7 +307,7 @@ esp_err_t app_prov_start_console_provisioning(int security, const protocomm_secu
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL);
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register IP event handler");
|
||||
return err;
|
||||
|
@ -53,9 +53,7 @@ static void wifi_init_sta()
|
||||
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));
|
||||
|
||||
/* Start wifi in station mode with credentials set during provisioning */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
/* Start Wi-Fi in station mode with credentials set during provisioning */
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
@ -93,6 +91,13 @@ void app_main()
|
||||
* main app and the provisioning service */
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
/* Initialize NVS needed by Wi-Fi */
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
/* Initialize Wi-Fi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
/* Check if device is provisioned */
|
||||
bool provisioned;
|
||||
if (app_prov_is_provisioned(&provisioned) != ESP_OK) {
|
||||
@ -107,6 +112,6 @@ void app_main()
|
||||
} else {
|
||||
/* Start WiFi station with credentials set during provisioning */
|
||||
ESP_LOGI(TAG, "Starting WiFi station");
|
||||
wifi_init_sta(NULL);
|
||||
wifi_init_sta();
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ static void app_prov_stop_service(void)
|
||||
|
||||
/* Remove event handler */
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler);
|
||||
}
|
||||
|
||||
/* Task spawned by timer callback */
|
||||
@ -248,17 +248,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
nvs_flash_erase();
|
||||
#endif
|
||||
|
||||
if (nvs_flash_init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init NVS");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init wifi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Get WiFi Station configuration */
|
||||
wifi_config_t wifi_cfg;
|
||||
if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) {
|
||||
@ -306,14 +295,6 @@ esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg)
|
||||
|
||||
static esp_err_t start_wifi_ap(const char *ssid, const char *pass)
|
||||
{
|
||||
/* Initialize WiFi with default configuration */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t err = esp_wifi_init(&cfg);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init WiFi : %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Build WiFi configuration for AP mode */
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
@ -333,7 +314,7 @@ static esp_err_t start_wifi_ap(const char *ssid, const char *pass)
|
||||
}
|
||||
|
||||
/* Start WiFi in AP mode with configuration built above */
|
||||
err = esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
esp_err_t err = esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set WiFi mode : %d", err);
|
||||
return err;
|
||||
@ -392,7 +373,7 @@ esp_err_t app_prov_start_softap_provisioning(const char *ssid, const char *pass,
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL);
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register IP event handler");
|
||||
return err;
|
||||
|
@ -53,9 +53,7 @@ static void wifi_init_sta()
|
||||
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));
|
||||
|
||||
/* Start wifi in station mode with credentials set during provisioning */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
/* Start Wi-Fi in station mode with credentials set during provisioning */
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
}
|
||||
@ -108,6 +106,13 @@ void app_main()
|
||||
* main app and the provisioning service */
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
/* Initialize NVS needed by Wi-Fi */
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
/* Initialize Wi-Fi with default config */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
/* Check if device is provisioned */
|
||||
bool provisioned;
|
||||
if (app_prov_is_provisioned(&provisioned) != ESP_OK) {
|
||||
@ -122,6 +127,6 @@ void app_main()
|
||||
} else {
|
||||
/* Start WiFi station with credentials set during provisioning */
|
||||
ESP_LOGI(TAG, "Starting WiFi station");
|
||||
wifi_init_sta(NULL);
|
||||
wifi_init_sta();
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ static void app_prov_stop_service(void)
|
||||
|
||||
/* Remove event handler */
|
||||
esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler);
|
||||
esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler);
|
||||
}
|
||||
|
||||
/* Task spawned by timer callback */
|
||||
@ -234,17 +234,6 @@ esp_err_t app_prov_is_provisioned(bool *provisioned)
|
||||
nvs_flash_erase();
|
||||
#endif
|
||||
|
||||
if (nvs_flash_init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init NVS");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
if (esp_wifi_init(&cfg) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init wifi");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Get WiFi Station configuration */
|
||||
wifi_config_t wifi_cfg;
|
||||
if (esp_wifi_get_config(ESP_IF_WIFI_STA, &wifi_cfg) != ESP_OK) {
|
||||
@ -292,14 +281,6 @@ esp_err_t app_prov_configure_sta(wifi_config_t *wifi_cfg)
|
||||
|
||||
static esp_err_t start_wifi_ap(const char *ssid, const char *pass)
|
||||
{
|
||||
/* Initialize WiFi with default configuration */
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_err_t err = esp_wifi_init(&cfg);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to init WiFi : %d", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
/* Build WiFi configuration for AP mode */
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
@ -319,7 +300,7 @@ static esp_err_t start_wifi_ap(const char *ssid, const char *pass)
|
||||
}
|
||||
|
||||
/* Start WiFi in AP mode with configuration built above */
|
||||
err = esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
esp_err_t err = esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set WiFi mode : %d", err);
|
||||
return err;
|
||||
@ -378,7 +359,7 @@ esp_err_t app_prov_start_softap_provisioning(const char *ssid, const char *pass,
|
||||
return err;
|
||||
}
|
||||
|
||||
err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, app_prov_event_handler, NULL);
|
||||
err = esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, app_prov_event_handler, NULL);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to register IP event handler");
|
||||
return err;
|
||||
|
Loading…
x
Reference in New Issue
Block a user