Skip to content

Commit c51a0ce

Browse files
authored
Merge pull request #1288 from freddanastrom/feature/set-device-hostname
Add feature to set a custom device hostname
2 parents 3c3d3e6 + 02db212 commit c51a0ce

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

Software/USER_SETTINGS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
/* Connectivity options */
120120
#define WIFI
121121
//#define WIFICONFIG //Enable this line to set a static IP address / gateway /subnet mask for the device. see USER_SETTINGS.cpp for the settings
122+
//#define CUSTOM_HOSTNAME \
123+
"battery-emulator" //Enable this line to use a custom hostname for the device, if disabled the default naming format 'esp32-XXXXXX' will be used.
122124
#define WEBSERVER //Enable this line to enable WiFi, and to run the webserver. See USER_SETTINGS.cpp for the Wifi settings.
123125
#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.
124126
#define MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled.

Software/src/devboard/webserver/webserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ String processor(const String& var) {
10081008
}
10091009
content += "</h4>";
10101010
if (status == WL_CONNECTED) {
1011+
content += "<h4>Hostname: " + String(WiFi.getHostname()) + "</h4>";
10111012
content += "<h4>IP: " + WiFi.localIP().toString() + "</h4>";
10121013
} else {
10131014
content += "<h4>Wifi state: " + getConnectResultString(status) + "</h4>";

Software/src/devboard/wifi/wifi.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ static bool connected_once = false;
2828

2929
void init_WiFi() {
3030

31+
#ifdef CUSTOM_HOSTNAME
32+
WiFi.setHostname(CUSTOM_HOSTNAME); // Set custom hostname if defined in USER_SETTINGS.h
33+
#endif
34+
3135
#ifdef WIFIAP
3236
WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection
3337
init_WiFi_AP();
@@ -182,6 +186,9 @@ void init_mDNS() {
182186
// e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C
183187
String mac = WiFi.macAddress();
184188
String mdnsHost = "batteryemulator" + mac.substring(mac.length() - 2);
189+
#ifdef CUSTOM_HOSTNAME // If CUSTOM_HOSTNAME is defined, use the same hostname also for mDNS
190+
mdnsHost = CUSTOM_HOSTNAME;
191+
#endif
185192

186193
// Initialize mDNS .local resolution
187194
if (!MDNS.begin(mdnsHost)) {
@@ -190,7 +197,7 @@ void init_mDNS() {
190197
#endif
191198
} else {
192199
// Advertise via bonjour the service so we can auto discover these battery emulators on the local network.
193-
MDNS.addService("battery_emulator", "tcp", 80);
200+
MDNS.addService(mdnsHost, "tcp", 80);
194201
}
195202
}
196203
#endif // MDNSRESPONDER

0 commit comments

Comments
 (0)