Skip to content

setting hostname issue #174

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

Closed
societyofrobots opened this issue Mar 8, 2021 · 5 comments · Fixed by #241
Closed

setting hostname issue #174

societyofrobots opened this issue Mar 8, 2021 · 5 comments · Fixed by #241

Comments

@societyofrobots
Copy link

Had trouble setting the hostname; it was only displaying 'espressif'. This caused an issue when having more than one ESP32 connected on the same router.

Others also had the problem, but you weren't able to solve it at the time.

I managed to solve it with a dirty hack (I'm a weak programmer, forgive me!).

In setup(), right after
server.onNotFound({ iotWebConf.handleNotFound(); });
I added this code:

WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // call is only a workaround for bug in WiFi class
WiFi.setHostname(thingName.c_str());

This is the info that helped me do this:

https://stackoverflow.com/questions/57570586/how-to-correctly-set-the-hostname-for-the-tcp-ip-adapter-on-the-esp32
espressif/arduino-esp32#806 (comment)
espressif/arduino-esp32#2537 (comment)

@prampec
Copy link
Owner

prampec commented Mar 28, 2021

Yes. I know, that there are some tricks for ESP8266 and other tricks for ESP32.
E.g. some extra delays in some particular places might also has effect on the hostname settings. It would be nice to have these thing solved by the libraries.
Apparently I set hostname and MDNS before before connecting to WiFi. Should I do if afterwards?

@societyofrobots
Copy link
Author

I just tried it before iotWebConf.init(); and it did not work. Only works after.

@prampec
Copy link
Owner

prampec commented Mar 29, 2021

Can you please try and add following line to checkWifiConnection method before return true statement (line: 806)?!

  WiFi.setHostname(this->_thingName);

@societyofrobots
Copy link
Author

societyofrobots commented Mar 29, 2021

Tried, nope, doesn't work.

Using these three lines at 806 however does fix it:

WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE); // call is only a workaround for bug in WiFi class
WiFi.setHostname(this->_thingName);

@EricDuminil
Copy link
Contributor

After a bit of googling and experimenting with ESP8266 and ESP32, I found out that:

  • WiFi.getHostname() and WiFi.setHostname() have both been defined for ESP8266 and ESP32, so there's no need for #ifdefs
  • WiFi.setHostname() needs to happen right before WiFi.begin() on ESP8266. Otherwise, the hostname gets reset to ESP-xxxxxx, e.g. as soon as WiFi.mode(WIFI_STA) is called.
  • WiFi.getHostname() will only return the set hostname after WiFi.begin() on ESP32.

As far as I can tell, those changes ( https://github.com/prampec/IotWebConf/compare/master...EricDuminil:fix/set_hostname?expand=1 ) allow to reliably set the hostname on ESP8266 and ESP32. @prampec I'll prepare a pull-request if you're interested.

Sadly, it doesn't mean that mDNS will work reliably. This technology apparently depends on the client, the router, and moon phases.

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 a pull request may close this issue.

3 participants