ESP-IDF: ESP32 Setting Hostname

If you had observed, the default host name for TCP/IP adapter is espresiff (this name will appear in your router’s DHCP client list). For DIY projects, this is no issue. if you are developing a product you might want to have a unique id/name as host name for your ESP32.

We have already covered how to connect to a WLAN here and how to use ESP32 as TCP client here. In this post we will see how to set the hostname for tcp/ip adapter. The code is very similar to the code present here, we will add one more method (tcpip_adapter_set_hostname) to call to set the hostname.

set hostname method signature


esp_err_ttcpip_adapter_set_hostname(tcpip_adapter_if_t tcpip_ifconst char *hostname)

method accepts two parameters, first one the interface name and second is the hostname we want to set. The interface name in our case is TCPIP_ADAPTER_IF_STA (we are using esp32 in station mode).

 static void initialise_wifi(void)
{
    esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
    tcpip_adapter_init();
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_wifi_start() );
    esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA ,"icircuit");
    if(ret != ESP_OK ){
      ESP_LOGE(MAIN_TAG,"failed to set hostname:%d",ret);  
    }
} 

full code is available on git repo

Tags:
2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *