title: ESP8266-RTOS-SDK - cannot connect to WPA2 network depending on esp_wifi_init() location
body: <p>I encounter a strange issue while working on my project with ESP8266 and using FreeRTOS SDK (version <em>release/v3.4</em>).</p>
  <p>In my code, I'd like to have 5 functions for controlling WiFi state:</p>
  <ul>
  <li>wifi_initialize()</li>
  <li>wifi_station_connect()</li>
  <li>wifi_station_disconnect()</li>
  <li>wifi_ap_start()</li>
  <li>wifi_ap_stop()</li>
  </ul>
  <p>I believe the names are self-descriptive. The (simplified) code for first 2 (only ones relevant to the problem) is as follows:</p>
  <pre><code>void wifi_initialize()
  {
      tcpip_adapter_init();
  
      esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &amp;event_handler, NULL);
      esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &amp;event_handler, NULL);
  
      wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT(); // Problematic when called here
      ESP_ERROR_CHECK(esp_wifi_init(&amp;init_cfg));                // Problematic when called here
      ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));      // Problematic when called here
      ESP_ERROR_CHECK(esp_wifi_start());                        // Problematic when called here
  }
  
  void wifi_station_connect()
  {
      // wifi_init_config_t init_cfg = WIFI_INIT_CONFIG_DEFAULT(); // OK when called here
      // ESP_ERROR_CHECK(esp_wifi_init(&amp;init_cfg));                // OK when called here
      // ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));      // OK when called here
      // ESP_ERROR_CHECK(esp_wifi_start());                        // OK when called here
  
      wifi_config_t wifi_config;
      if (xSemaphoreTake(common_settings_mutex, pdMS_TO_TICKS(20)))
      {
          memcpy(wifi_config.sta.ssid, common_settings.wifi_ssid, 32);
          ESP_LOGI(TAG, &quot;Using SSID: %s&quot;, wifi_config.sta.ssid);
          memcpy(wifi_config.sta.password, common_settings.wifi_password, 64);
          ESP_LOGI(TAG, &quot;Using password: %s&quot;, wifi_config.sta.password);
          xSemaphoreGive(common_settings_mutex);
      }
      if (strlen((char*) wifi_config.sta.password))
      {
          wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
      }
  
      ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &amp;wifi_config));
      ESP_ERROR_CHECK(esp_wifi_connect());
  }
  
  </code></pre>
  <p>The problem: with the code as above, the ESP is unable to connect to password-protected networks. The SSID and password copied to <code>wifi_config</code> are correct as checked using logging. Connecting to open networks (using empty password) works fine.
  The bizzare part: moving call to <code>esp_wifi_init()</code> from <code>wifi_initialize()</code> to <code>wifi_station_connect()</code> solves the problem, even though in any case <code>wifi_station_connect()</code> is called directly after <code>wifi_initialize()</code> (<code>esp_wifi_set_mode()</code> and <code>esp_wifi_start()</code> are also moved, since they need to be called after <code>esp_wifi_init()</code>).</p>
  <p>I will be grateful for any suggestions what might be wrong here, or how to debug this issue/</p>