Skip to content

WiFi.setHostname() not working #6278

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
1 task done
dsilletti opened this issue Feb 13, 2022 · 8 comments
Closed
1 task done

WiFi.setHostname() not working #6278

dsilletti opened this issue Feb 13, 2022 · 8 comments
Labels
Area: BT&Wifi BT & Wifi related issues

Comments

@dsilletti
Copy link

Board

ESP32

Device Description

plain module

Hardware Configuration

N/A

Version

latest master

IDE Name

Arduino IDE

Operating System

macOS 12.1

Flash frequency

40Mhz

PSRAM enabled

no

Upload speed

115200

Description

WiFi.setHostname() does not work, hostname remain always esp32-XXXXXX

Sketch

WiFi.disconnect(true,true);
delay(1000);
WiFi.mode(WIFI_STA);
esp_wifi_set_country_code("IT", true);
WiFi.setHostname("hostname");

Debug Message

N/A

Other Steps to Reproduce

WiFi.setHostname("hostname");

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@dsilletti dsilletti added the Status: Awaiting triage Issue is waiting for triage label Feb 13, 2022
@me-no-dev
Copy link
Member

how did you test this? it's often that the DHCP server might cache the hostname

@dsilletti
Copy link
Author

dsilletti commented Feb 14, 2022

@me-no-dev I power cycled the Wi-Fi router that provides the DHCP server several times, power cycled the esp32 several times, but the hostname I see on the router is always esp32-XXXXXX

@JonGordon123
Copy link

On most routers if you want to clear the cache, you have to reset it using the reset button... and not just power cycle it. You could... connect to a different router, or reset the router, or change the esp32 MAC address to confirm the hostname shows up in the router.

@me-no-dev
Copy link
Member

I have mikrotiks here for routers and they have "clear leases" option that allowed me to confirm the hostname change, else I also thought it did not work.

@dsilletti
Copy link
Author

dsilletti commented Feb 14, 2022

Interesting that even if I change the MAC address, the IP address I've got from the DHCP is different, but the hostname I see on the router is still the same esp32-XXXXXX. @me-no-dev could you please share your relevant code lines, this is in my running sketch (including the MAC address change I applied now for testing):

  WiFi.disconnect(true,true);
  delay(1000);
  WiFi.mode(WIFI_STA);
  uint8_t newMACAddress[] = {0x32, 0xAE, 0xA4, 0x07, 0x0D, 0x66};
  esp_wifi_set_mac(WIFI_IF_STA, &newMACAddress[0]);
  esp_wifi_set_country_code("IT", true);
  WiFi.setHostname("hostname");

@VojtechBartoska VojtechBartoska added Area: BT&Wifi BT & Wifi related issues and removed Status: Awaiting triage Issue is waiting for triage labels Feb 15, 2022
@gonzabrusco
Copy link
Contributor

Apparently WiFi.setHostname should be called before setting WiFi.mode in order to work.

@dsilletti
Copy link
Author

dsilletti commented Feb 22, 2022

Thank you @gonzabrusco it works!
Calling WiFi.setHostname before WiFi.mode solved the issue.

 WiFi.disconnect(true,true);
 delay(1000);
 WiFi.setHostname("hostname");
 WiFi.mode(WIFI_STA);  

@Xylopyrographer
Copy link
Contributor

Wouldn't it be super if the all the WiFi call setting sequences were documented somewhere... 😉

pimlie added a commit to pimlie/homeplate that referenced this issue Jul 17, 2024
See espressif/arduino-esp32#6278, setHostname needs to be called before setting the wifi  mode cause otherwise it will not / not always be set correctly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues
Projects
None yet
Development

No branches or pull requests

6 participants