Skip to content

[ESP8266/ESP32] Problem setting hostname while on dynamic ip. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2019

Conversation

maurodelucca
Copy link
Contributor

While using a dynamic ip the system doesn't take into account the hostname setting.
Calling WiFi.hostname() after begin() solved the issue.
This only applies to esp8266, haven't tried on esp32.

Copy link
Owner

@rjwats rjwats left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to confirm first that the ordering issue doesn't also affect the ESP32 before merging. Bear with me while I investigate.

@rjwats
Copy link
Owner

rjwats commented Apr 27, 2019

Both ESP32 and esp8266 will not accept a dhcp hostname until the WiFi is set to an active mode. It looks like the advice is to put the device into the correct mode, and set the hostname before calling WiFi.begin().

Your fix effectively does this, however I'm not able to properly test this approach under ESP32 because there is another issue affecting the DHCP hostname feature at the moment. There is a pending PR to fix the issue:

espressif/arduino-esp32#2537
espressif/esp-lwip#6

I'll keep an eye on this. Meanwhile would you mind moving the hostname calls for both devices below the begin call, because it's clearly wrong in both instances, so we can merge it in.

@maurodelucca
Copy link
Contributor Author

This is getting really interesting/fun :-). I have some thoughts/findings regarding this topic:

  1. I am not sure, but does it make sense to set the hostname while on static ip? From the ESP8266WiFi doc it only applies to dhcp: esp8266wifi/station-class.html#hostname

  2. After reading your post it kept wonder in my mind the idea that dhcp hostname should be set before WiFi.begin(). Reading the code of ESP8266WiFiSTAClass::config() and ESP8266WiFiSTAClass::begin() the first thing they do is enabling STA with WiFi.enableSTA(true). Although it is not documented, it is at least named as a not properly documented functions: esp8266wifi/generic-class.html#other-function-calls.
    Pretty much it checks if STA is previously active, otherwise is activated along with whatever is already activated. This is the code:

/**
 * control STA mode
 * @param enable bool
 * @return ok
 */
bool ESP8266WiFiGenericClass::enableSTA(bool enable) {

    WiFiMode_t currentMode = getMode();
    bool isEnabled = ((currentMode & WIFI_STA) != 0);

    if(isEnabled != enable) {
        if(enable) {
            return mode((WiFiMode_t)(currentMode | WIFI_STA));
        } else {
            return mode((WiFiMode_t)(currentMode & (~WIFI_STA)));
        }
    } else {
        return true;
    }
}

Long story short, I tried calling WiFi.enableSTA(true) and then setting the hostname prior to WiFi.begin() and it worked!

void WiFiSettingsService::reconfigureWiFiConnection() {
    Serial.println("Reconfiguring WiFi...");

    // disconnect and de-configure wifi and software access point
    WiFi.disconnect(true);

    WiFi.enableSTA(true);

    // configure static ip config for station mode (if set)
    if (_staticIPConfig) {
      WiFi.config(_localIP, _gatewayIP,  _subnetMask, _dnsIP1, _dnsIP2);
    }

    // connect to the network
#if defined(ESP8266)
    WiFi.hostname(_hostname);
#elif defined(ESP_PLATFORM)
    WiFi.setHostname(_hostname.c_str());
#endif

    WiFi.begin(_ssid.c_str(), _password.c_str());
}

What do you think?

While using a dynamic ip the system doesn't take into account the hostname setting.
Calling setting hostname after begin solved the issue.
This only applies to esp8266, haven't tried on esp32.
Calling WiFi.config() with INADDR_ANY/INADDR_NONE to set dynamic ip / enable dhcp.
This solution solved issue on setting hostname for esp8266 and esp32.
Also it solved issues moving from static ip to dynamic ip.
@maurodelucca
Copy link
Contributor Author

Finally I came up with this. While trying to solve the hostname issue, I found that changing from static ip to dynamic ip was not working. Investing the WiFi API, I found there is a variable named _useStaticIp that is only being set inside WiFi.config. So, I added a new condition in WiFiSettingService.cpp, and now when _staticIPConfig is false I also call WiFi.config but with a INADDR_ANY (esp8266) / INADDR_NONE (esp32) value. This solved the issue of moving from static to dynamic ip on both platforms, AND IT ALSO SOLVED THE HOSTNAME ISSUE!!
Let me know what you think.
Best, Mauro.

@maurodelucca maurodelucca changed the title [ESP8266] Problem setting hostname while on dynamic ip. [ESP8266/ESP32] Problem setting hostname while on dynamic ip. Apr 28, 2019
@rjwats
Copy link
Owner

rjwats commented Apr 28, 2019 via email

@rjwats rjwats merged commit 2131d86 into rjwats:master Apr 28, 2019
@maurodelucca maurodelucca deleted the fix_hostname branch April 28, 2019 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants