title: How to correctly set the hostname for the TCP/IP adapter on the ESP32
body: <h3>Problem</h3>
  
  <p>Espressif's ESP-32 (specifically the ESP-WROOM-32 in this case) appears on a network with the default hostname "Espressif". I don't want to use this hostname, so I've opted to change it as follows: </p>
  
  <pre><code>    // Initialize the TCP/IP adapter (launches handler task)
      tcpip_adapter_init();
  
      // Set the hostname for the default TCP/IP station interface
      if ((err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, g_hostname))
              != ESP_OK) {
          return err;
      }
  
  </code></pre>
  
  <p>Of course, this isn't working. I get back the following error: <code>ESP_ERR_TCPIP_ADAPTER_IF_NOT_READY</code>. </p>
  
  <hr>
  
  <h3>Attempted Solutions</h3>
  
  <p>To solve this, I look to see if the TCP/IP adapter will post some kind of event when it is finished initializing. That way I can register a handler to set the hostname. The <a href="https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/wifi.html" rel="nofollow noreferrer">Espressif WiFi Driver Guide</a> here indicates a task is launched - so there is probably an event right:</p>
  
  <blockquote>
    <p>"The main task calls tcpip_adapter_init() to create an <strong>LwIP core task</strong> and initialize LwIP-related work."</p>
  </blockquote>
  
  <p>Well I cannot find any such events. Neither the <a href="https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/network/tcpip_adapter.html" rel="nofollow noreferrer">API documentation</a> nor the actual file itself (<code>tcpip_adapter.h</code>) have it. I checked the header file for events, and <strong>none seem to exist solely for the purpose of indicating that the TCP/IP adapter has finished starting</strong>:</p>
  
  <pre><code>/** IP event declarations */
  typedef enum {
      IP_EVENT_STA_GOT_IP,               /*!&lt; ESP32 station got IP from connected AP */
      IP_EVENT_STA_LOST_IP,              /*!&lt; ESP32 station lost IP and the IP is reset to 0 */
      IP_EVENT_AP_STAIPASSIGNED,         /*!&lt; ESP32 soft-AP assign an IP to a connected station */
      IP_EVENT_GOT_IP6,                  /*!&lt; ESP32 station or ap or ethernet interface v6IP addr is preferred */
      IP_EVENT_ETH_GOT_IP,               /*!&lt; ESP32 ethernet got IP from connected AP */
  } ip_event_t;
  </code></pre>
  
  <h3>Possible Lead</h3>
  
  <p>I have noticed that in <a href="https://docs.espressif.com/projects/esp-idf/en/release-v3.0/api-guides/wifi.html#system-event-sta-start" rel="nofollow noreferrer">Espressif's WiFi guide</a> they indicate that the event <code>SYSTEM_EVENT_STA_START</code> (which indicates that a station has started), will:</p>
  
  <blockquote>
    <p>Upon receiving this event, the event task will initialize the LwIP network interface (netif). </p>
  </blockquote>
  
  <p>If I place the call after a handler receives this event, I no longer get the error: </p>
  
  <pre><code>
      // After the event WIFI_EVENT_STA_START
      if (base == WIFI_EVENT &amp;&amp; id == WIFI_EVENT_STA_START) {
  
          // Set the hostname for the default TCP/IP station interface
          if ((err = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, g_hostname))
                  != ESP_OK) {
              fprintf(stderr, "Err: %s", esp_err_to_name(err));
          }
          ...
      }
  
  </code></pre>
  
  <p>However, the hostname still hasn't changed. Therefore I find myself a bit stuck. How can I actually change the hostname? I've found little to no results from searching this problem. However, the esp32 is a popular module and I'm sure many other people will find themselves facing the same problem. </p>