text: /* esp-azure example
  
     This example code is in the Public Domain (or CC0 licensed, at your option.)
  
     Unless required by applicable law or agreed to in writing, this
     software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     CONDITIONS OF ANY KIND, either express or implied.
  */
  #include <stdio.h>
  #include "freertos/FreeRTOS.h"
  #include "freertos/task.h"
  #include "freertos/event_groups.h"
  
  #include "esp_system.h"
  #include "esp_system.h"
  #include "esp_wifi.h"
  #include "esp_event_loop.h"
  #include "esp_log.h"
  
  #include "nvs_flash.h"
  
  #define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
  #define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
  
  /* FreeRTOS event group to signal when we are connected & ready to make a request */
  static EventGroupHandle_t wifi_event_group;
  
  #ifndef BIT0
  #define BIT0 (0x1 << 0)
  #endif
  /* The event group allows multiple bits for each event,
     but we only care about one event - are we connected
     to the AP with an IP? */
  const int CONNECTED_BIT = BIT0;
  
  static const char *TAG = "azure";
  
  static esp_err_t event_handler(void *ctx, system_event_t *event)
  {
      switch(event->event_id) {
      case SYSTEM_EVENT_STA_START:
          esp_wifi_connect();
          break;
      case SYSTEM_EVENT_STA_GOT_IP:
          xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
          break;
      case SYSTEM_EVENT_STA_DISCONNECTED:
          /* This is a workaround as ESP platform WiFi libs don't currently
             auto-reassociate. */
          esp_wifi_connect();
          xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
          break;
      default:
          break;
      }
      return ESP_OK;
  }
  
  static void initialise_wifi(void)
  {
      tcpip_adapter_init();
      wifi_event_group = xEventGroupCreate();
      ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
      wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
      ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
      ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
      wifi_config_t wifi_config = {
          .sta = {
              .ssid = EXAMPLE_WIFI_SSID,
              .password = EXAMPLE_WIFI_PASS,
          },
      };
      ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
      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_start() );
  }
  
  extern int prov_dev_client_ll_sample_run();
  void azure_task(void *pvParameter)
  {
      xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
                          false, true, portMAX_DELAY);
      ESP_LOGI(TAG, "Connected to AP success!");
  
      prov_dev_client_ll_sample_run();
  
      vTaskDelete(NULL);
  }
  
  void app_main()
  {
      // Initialize NVS
      esp_err_t ret = nvs_flash_init();
      if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
          ESP_ERROR_CHECK(nvs_flash_erase());
          ret = nvs_flash_init();
      }
      ESP_ERROR_CHECK( ret );
  
      initialise_wifi();
  
      if ( xTaskCreate(&azure_task, "azure_task", 1024 * 5, NULL, 5, NULL) != pdPASS ) {
          printf("create azure task failed\r\n");
      }
  }
  