tcpip_adapter_set_hostname() not working

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: tcpip_adapter_set_hostname() not working

Postby boarchuz » Tue Nov 03, 2020 3:53 pm

mzimmers wrote:
Mon Nov 02, 2020 10:47 pm
Still not working -- any other ideas?
What error did you get?
Did you use a string literal as in my snippet, or the NVS-loaded value? If the latter are you sure it's valid?
Did you remove tcpip_adapter_set_hostname from the event handler?

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: tcpip_adapter_set_hostname() not working

Postby mzimmers » Tue Nov 03, 2020 4:36 pm

Hi boarchuz -

I don't get an error. I use a pointer to a char array - I believe it's valid. Yes, I removed the tcpip_adapter call.

Here's the code:

Code: Select all

esp_err_t Wifi::setHostname()
{
    string s;
    esp_err_t err;

    char hostname[32] = { NULL_CHAR }; // can't find a macro for this length in a header file.
    char *p = hostname;

    do
    {
        s = m_tasks->flash->get(NVS_KEY_DEVICE_NAME);
        strcpy(p, s.c_str());
        ESP_LOGI(TAG, "setHostname(): setting hostname to \"%s\".", p);
        if ((err = esp_netif_set_hostname(m_netif, p)) != ESP_OK)
        {
            ESP_LOGE(TAG, "setHostname(): set_hostname() returned %s.",
                     esp_err_to_name(err));
            continue;
        }

        memset(hostname, NULL_CHAR, 32);

        err = esp_netif_get_hostname(m_netif, const_cast<const char **>(&p));
        if (err == ESP_OK)
        {
            ESP_LOGI(TAG, "setHostname(): get_hostname returned \"%s\".", hostname);
        }
        else
        {
            ESP_LOGE(TAG, "setHostname(): get_hostname returned %s.",
                     esp_err_to_name(err));
        }
    }
    while (false);
    return err;
}
And here's the output:

Code: Select all

I (6472) Wifi: setHostname(): setting hostname to "WiFi Button".
I (6472) Wifi: setHostname(): get_hostname returned "".
BTW: the code above isn't called in the event handler; it's done as part of the setup for the WiFi driver.

EDIT: I tried removing the space character from the name; same result.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: tcpip_adapter_set_hostname() not working

Postby boarchuz » Wed Nov 04, 2020 1:13 am

esp_netif_get_hostname will modify p; it doesn't point to 'hostname' any more. Try:

Code: Select all

ESP_LOGI(TAG, "setHostname(): get_hostname returned \"%s\".", p);

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: tcpip_adapter_set_hostname() not working

Postby mzimmers » Wed Nov 04, 2020 1:50 am

Ahh...OK, that helps. Results are now:

Code: Select all

I (7839) Wifi: setHostname(): setting hostname to "WiFi Button".
I (7839) Wifi: setHostname(): get_hostname returned "WiFi Button".
Unfortunately, according to my home network, it still reports as "espressif." It claims to be real-time, though I don't know (it's a Google mesh). Do I need to do anything else to get this to "take?"

Thanks...

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: tcpip_adapter_set_hostname() not working

Postby boarchuz » Wed Nov 04, 2020 2:01 am

You could upload to a new ESP32 if you have one about, to test that it's working. Or try some utility to get names of devices on your network. Or wait/restart the router and see if that clears the cached name.

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: tcpip_adapter_set_hostname() not working

Postby mzimmers » Wed Nov 04, 2020 5:51 pm

I reset and restarted my router (what a major pain -- thanks, Google Nest). Same results.

Who is online

Users browsing this forum: Google [Bot] and 108 guests