Provisioning : Manager example updated to use esp_event_loop feature

This commit is contained in:
Anurag Kar
2019-04-24 00:13:14 +05:30
committed by bot
parent 0e8bd1699d
commit a13cc7da7a

View File

@@ -16,7 +16,7 @@
#include <esp_log.h> #include <esp_log.h>
#include <esp_wifi.h> #include <esp_wifi.h>
#include <esp_event_loop.h> #include <esp_event.h>
#include <nvs_flash.h> #include <nvs_flash.h>
#include <wifi_provisioning/manager.h> #include <wifi_provisioning/manager.h>
@@ -30,73 +30,56 @@ const int WIFI_CONNECTED_EVENT = BIT0;
static EventGroupHandle_t wifi_event_group; static EventGroupHandle_t wifi_event_group;
/* Event handler for catching system events */ /* Event handler for catching system events */
static esp_err_t event_handler(void *ctx, system_event_t *event) static void event_handler(void* arg, esp_event_base_t event_base,
int event_id, void* event_data)
{ {
/* Pass event information to provisioning manager so that it can if (event_base == WIFI_PROV_EVENT) {
* maintain its internal state depending upon the system event */ switch (event_id) {
wifi_prov_mgr_event_handler(ctx, event); case WIFI_PROV_START:
ESP_LOGI(TAG, "Provisioning started");
/* Global event handling */ break;
switch (event->event_id) { case WIFI_PROV_CRED_RECV: {
case SYSTEM_EVENT_STA_START: wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
esp_wifi_connect(); ESP_LOGI(TAG, "Received Wi-Fi credentials"
break; "\n\tSSID : %s\n\tPassword : %s",
case SYSTEM_EVENT_STA_GOT_IP: (const char *) wifi_sta_cfg->ssid,
ESP_LOGI(TAG, "Connected with IP Address:%s", (const char *) wifi_sta_cfg->password);
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip)); break;
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); }
break; case WIFI_PROV_CRED_FAIL: {
case SYSTEM_EVENT_STA_DISCONNECTED: wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
esp_wifi_connect(); "\n\tPlease reset to factory and retry provisioning",
break; (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
default: "Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
break; break;
} }
return ESP_OK; case WIFI_PROV_CRED_SUCCESS:
} ESP_LOGI(TAG, "Provisioning successful");
break;
/* Event handler for catching provisioning manager events */ case WIFI_PROV_END:
static void prov_event_handler(void *user_data, /* De-initialize manager once provisioning is finished */
wifi_prov_cb_event_t event, void *event_data) wifi_prov_mgr_deinit();
{ break;
switch (event) { default:
case WIFI_PROV_START: break;
ESP_LOGI(TAG, "Provisioning started");
break;
case WIFI_PROV_CRED_RECV: {
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
/* If SSID length is exactly 32 bytes, null termination
* will not be present, so explicitly obtain the length */
size_t ssid_len = strnlen((const char *)wifi_sta_cfg->ssid, sizeof(wifi_sta_cfg->ssid));
ESP_LOGI(TAG, "Received Wi-Fi credentials"
"\n\tSSID : %.*s\n\tPassword : %s",
ssid_len, (const char *) wifi_sta_cfg->ssid,
(const char *) wifi_sta_cfg->password);
break;
} }
case WIFI_PROV_CRED_FAIL: { } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data; esp_wifi_connect();
ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s" } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
"\n\tPlease reset to factory and retry provisioning", ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
(*reason == WIFI_PROV_STA_AUTH_ERROR) ? ESP_LOGI(TAG, "Connected with IP Address:%s", ip4addr_ntoa(&event->ip_info.ip));
"Wi-Fi AP password incorrect" : "Wi-Fi AP not found"); /* Signal main application to continue execution */
break; xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT);
} } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
case WIFI_PROV_CRED_SUCCESS: ESP_LOGI(TAG, "Disconnected. Connecting to the AP again...");
ESP_LOGI(TAG, "Provisioning successful"); esp_wifi_connect();
break;
case WIFI_PROV_END:
/* De-initialize manager once provisioning is finished */
wifi_prov_mgr_deinit();
break;
default:
break;
} }
} }
static void wifi_init_sta() static void wifi_init_sta()
{ {
/* Start Wi-Fi in station mode */
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());
} }
@@ -123,11 +106,18 @@ void app_main()
ESP_ERROR_CHECK(nvs_flash_init()); ESP_ERROR_CHECK(nvs_flash_init());
} }
/* Initialize TCP/IP and the event loop */ /* Initialize TCP/IP */
tcpip_adapter_init(); tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
/* Initialize the event loop */
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_event_group = xEventGroupCreate(); wifi_event_group = xEventGroupCreate();
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
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));
/* Initialize Wi-Fi */ /* Initialize Wi-Fi */
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));
@@ -146,14 +136,7 @@ void app_main()
* appropriate scheme specific event handler allows the manager * appropriate scheme specific event handler allows the manager
* to take care of this automatically. This can be set to * to take care of this automatically. This can be set to
* WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/ * WIFI_PROV_EVENT_HANDLER_NONE when using wifi_prov_scheme_softap*/
.scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM, .scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM
/* Do we want an application specific handler be executed on
* various provisioning related events */
.app_event_handler = {
.event_cb = prov_event_handler,
.user_data = NULL
}
}; };
/* Initialize provisioning manager with the /* Initialize provisioning manager with the
@@ -219,7 +202,7 @@ void app_main()
/* Uncomment the following to wait for the provisioning to finish and then release /* Uncomment the following to wait for the provisioning to finish and then release
* the resources of the manager. Since in this case de-initialization is triggered * the resources of the manager. Since in this case de-initialization is triggered
* by the configured prov_event_handler(), we don't need to call the following */ * by the default event loop handler, we don't need to call the following */
// wifi_prov_mgr_wait(); // wifi_prov_mgr_wait();
// wifi_prov_mgr_deinit(); // wifi_prov_mgr_deinit();
} else { } else {