Page 1 of 1

ESP32 - Hostname

Posted: Wed Mar 27, 2024 4:49 pm
by antonins
Hello all,

I'm working on ESP32-WROOM-32E.

I made a connection to a wifi but I want to change the hostname but it doesn't work.

I don't understand from when the issue come.

Please find below the code

Code: Select all

#include <Arduino.h>  //Basique ESP32
#include <WiFi.h>             //Wifi ??


const char* ssid = "Bbox-641xxxxx";
const char* password = "xxxxxxxxxxxxx";

void setup() {
  Serial.begin(115200);
  delay(1000);

  // Se connecter au réseau WiFi
  Serial.println();
  Serial.print("Connexion au réseau ");
  Serial.println(ssid);
  WiFi.disconnect(true,true);
  delay(1000);
  WiFi.setHostname("test1324");
  WiFi.mode(WIFI_STA);  

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connecté");
  Serial.print("Adresse IP: ");
  Serial.println(WiFi.localIP());
  Serial.println("Hostname : ");
  Serial.println(WiFi.getHostname());
}

void loop() {

}
When I have a look on the BBox interface, the hostname is still ESP32-FCDKDML

Have you any idea of where the problem is coming from ?

Thanks for your help,
Antonin

Re: ESP32 - Hostname

Posted: Wed Mar 27, 2024 8:07 pm
by Basalt
I've been working on changing the hostname lately, this is roughly my solution:
- in setup, add ARDUINO_EVENT_WIFI_STA_DISCONNECTED callback event: start 10 sec reconnect timer (Ticker, once)
- Add reconnect timer callback function: IF (WiFi.status() != WL_CONNECTED) THEN call WiFi.reconnect() ELSE ignore
- In setup, add ARDUINO_EVENT_WIFI_STA_CONNECTED callback event: stop the reconnect timer (just in case we got connected before timeout)
- To change hostname:
-- optionally, stop MDNS
-- call WiFi.setHostname(name)
-- call WiFi.disconnect(false, false) => this will trigger ARDUINO_EVENT_WIFI_STA_DISCONNECTED => WiFi will reconnect
-- optionally, startup MDNS again
Windows (Chrome, ping, ...) could find the new hostname quite fast.
My Asus router is very slow in showing the new name in the connected devices list, but does show it quickly in the DHCP leases
Also, my iPhone had some brief struggles finding the new hostname.