Add feature to set a custom device hostname

This commit is contained in:
Fredrik 2025-07-03 12:21:55 +02:00
parent f94a011063
commit daeeece63a
5 changed files with 12 additions and 1 deletions

View file

@ -26,6 +26,7 @@ std::string password = WIFI_PASSWORD; // Set in USER_SECRETS.h
const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters, also used for device name on web interface const char* ssidAP = "Battery Emulator"; // Maximum of 63 characters, also used for device name on web interface
const char* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h const char* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h
const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection
const char* hostname = "battery-emulator"; // Hostname for the device, needs to be enabled in USER_SETTINGS.h
#ifdef WEBSERVER #ifdef WEBSERVER
const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h

View file

@ -118,6 +118,7 @@
/* Connectivity options */ /* Connectivity options */
#define WIFI #define WIFI
//#define WIFICONFIG //Enable this line to set a static IP address / gateway /subnet mask for the device. see USER_SETTINGS.cpp for the settings //#define WIFICONFIG //Enable this line to set a static IP address / gateway /subnet mask for the device. see USER_SETTINGS.cpp for the settings
//#define HOSTNAME //Enable this line to use a custom hostname for the device, hostname is set in USER_SETTINGS.cpp
#define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings. #define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings.
#define WIFIAP //When enabled, the emulator will broadcast its own access point Wifi. Can be used at the same time as a normal Wifi connection to a router. #define WIFIAP //When enabled, the emulator will broadcast its own access point Wifi. Can be used at the same time as a normal Wifi connection to a router.
#define MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled. #define MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled.

View file

@ -1008,6 +1008,7 @@ String processor(const String& var) {
} }
content += "</h4>"; content += "</h4>";
if (status == WL_CONNECTED) { if (status == WL_CONNECTED) {
content += "<h4>Hostname: " + String(WiFi.getHostname()) + "</h4>";
content += "<h4>IP: " + WiFi.localIP().toString() + "</h4>"; content += "<h4>IP: " + WiFi.localIP().toString() + "</h4>";
} else { } else {
content += "<h4>Wifi state: " + getConnectResultString(status) + "</h4>"; content += "<h4>Wifi state: " + getConnectResultString(status) + "</h4>";

View file

@ -28,6 +28,10 @@ static bool connected_once = false;
void init_WiFi() { void init_WiFi() {
#ifdef HOSTNAME
WiFi.setHostname(hostname);
#endif
#ifdef WIFIAP #ifdef WIFIAP
WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection
init_WiFi_AP(); init_WiFi_AP();
@ -182,6 +186,9 @@ void init_mDNS() {
// e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C // e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C
String mac = WiFi.macAddress(); String mac = WiFi.macAddress();
String mdnsHost = "batteryemulator" + mac.substring(mac.length() - 2); String mdnsHost = "batteryemulator" + mac.substring(mac.length() - 2);
#ifdef HOSTNAME // If HOSTNAME is defined, use the same hostname from USER_SETTINGS.h also for mDNS
mdnsHost = hostname;
#endif
// Initialize mDNS .local resolution // Initialize mDNS .local resolution
if (!MDNS.begin(mdnsHost)) { if (!MDNS.begin(mdnsHost)) {
@ -190,7 +197,7 @@ void init_mDNS() {
#endif #endif
} else { } else {
// Advertise via bonjour the service so we can auto discover these battery emulators on the local network. // Advertise via bonjour the service so we can auto discover these battery emulators on the local network.
MDNS.addService("battery_emulator", "tcp", 80); MDNS.addService(mdnsHost, "tcp", 80);
} }
} }
#endif // MDNSRESPONDER #endif // MDNSRESPONDER

View file

@ -14,6 +14,7 @@ extern std::string password;
extern const uint8_t wifi_channel; extern const uint8_t wifi_channel;
extern const char* ssidAP; extern const char* ssidAP;
extern const char* passwordAP; extern const char* passwordAP;
extern const char* hostname;
void init_WiFi(); void init_WiFi();
void wifi_monitor(); void wifi_monitor();