From daeeece63a72cad2343eeaf91a688039c83f9bc8 Mon Sep 17 00:00:00 2001 From: Fredrik Date: Thu, 3 Jul 2025 12:21:55 +0200 Subject: [PATCH 1/5] Add feature to set a custom device hostname --- Software/USER_SETTINGS.cpp | 1 + Software/USER_SETTINGS.h | 1 + Software/src/devboard/webserver/webserver.cpp | 1 + Software/src/devboard/wifi/wifi.cpp | 9 ++++++++- Software/src/devboard/wifi/wifi.h | 1 + 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index 7ddeb02c..d0b9c028 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h 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 const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index b888f4b8..c2141747 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -118,6 +118,7 @@ /* Connectivity options */ #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 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 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. diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index 09b6db98..0e3517b6 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -1008,6 +1008,7 @@ String processor(const String& var) { } content += ""; if (status == WL_CONNECTED) { + content += "

Hostname: " + String(WiFi.getHostname()) + "

"; content += "

IP: " + WiFi.localIP().toString() + "

"; } else { content += "

Wifi state: " + getConnectResultString(status) + "

"; diff --git a/Software/src/devboard/wifi/wifi.cpp b/Software/src/devboard/wifi/wifi.cpp index 0d18ba12..6f5bcfb8 100644 --- a/Software/src/devboard/wifi/wifi.cpp +++ b/Software/src/devboard/wifi/wifi.cpp @@ -28,6 +28,10 @@ static bool connected_once = false; void init_WiFi() { +#ifdef HOSTNAME + WiFi.setHostname(hostname); +#endif + #ifdef WIFIAP WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection 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 String mac = WiFi.macAddress(); 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 if (!MDNS.begin(mdnsHost)) { @@ -190,7 +197,7 @@ void init_mDNS() { #endif } else { // 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 diff --git a/Software/src/devboard/wifi/wifi.h b/Software/src/devboard/wifi/wifi.h index bed27416..f54f905c 100644 --- a/Software/src/devboard/wifi/wifi.h +++ b/Software/src/devboard/wifi/wifi.h @@ -14,6 +14,7 @@ extern std::string password; extern const uint8_t wifi_channel; extern const char* ssidAP; extern const char* passwordAP; +extern const char* hostname; void init_WiFi(); void wifi_monitor(); From 8860bcf0076ed658e98f7decd6ae959932a3a480 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Jul 2025 11:12:40 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Software/USER_SETTINGS.cpp | 10 +++++----- Software/src/devboard/wifi/wifi.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index d0b9c028..b40a2f1b 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -21,11 +21,11 @@ volatile CAN_Configuration can_config = { .shunt = CAN_NATIVE // (OPTIONAL) Which CAN is your shunt connected to? }; -std::string ssid = WIFI_SSID; // Set in USER_SECRETS.h -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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h -const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection +std::string ssid = WIFI_SSID; // Set in USER_SECRETS.h +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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h +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 diff --git a/Software/src/devboard/wifi/wifi.cpp b/Software/src/devboard/wifi/wifi.cpp index 6f5bcfb8..6dfccc19 100644 --- a/Software/src/devboard/wifi/wifi.cpp +++ b/Software/src/devboard/wifi/wifi.cpp @@ -186,7 +186,7 @@ void init_mDNS() { // e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C String mac = WiFi.macAddress(); 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 +#ifdef HOSTNAME // If HOSTNAME is defined, use the same hostname from USER_SETTINGS.h also for mDNS mdnsHost = hostname; #endif From 88c3103000d115354df63dbb066ffe382c226970 Mon Sep 17 00:00:00 2001 From: Fredrik Date: Mon, 7 Jul 2025 08:47:33 +0200 Subject: [PATCH 3/5] Simplifying setup process and clarifying the name --- Software/USER_SETTINGS.cpp | 1 - Software/USER_SETTINGS.h | 2 +- Software/src/devboard/wifi/wifi.cpp | 8 ++++---- Software/src/devboard/wifi/wifi.h | 1 - 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index b40a2f1b..1b6d93a9 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -26,7 +26,6 @@ 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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h 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 const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index c2141747..b09ed532 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -118,7 +118,7 @@ /* Connectivity options */ #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 HOSTNAME //Enable this line to use a custom hostname for the device, hostname is set in USER_SETTINGS.cpp +#define CUSTOM_HOSTNAME "battery-emulator" //Enable this line to use a custom hostname for the device, if disabled the default naming format 'esp32-XXXXXX' will be used. #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 MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled. diff --git a/Software/src/devboard/wifi/wifi.cpp b/Software/src/devboard/wifi/wifi.cpp index 6dfccc19..f7b59d23 100644 --- a/Software/src/devboard/wifi/wifi.cpp +++ b/Software/src/devboard/wifi/wifi.cpp @@ -28,8 +28,8 @@ static bool connected_once = false; void init_WiFi() { -#ifdef HOSTNAME - WiFi.setHostname(hostname); +#ifdef CUSTOM_HOSTNAME + WiFi.setHostname(CUSTOM_HOSTNAME); // Set custom hostname if defined in USER_SETTINGS.h #endif #ifdef WIFIAP @@ -186,8 +186,8 @@ void init_mDNS() { // e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C String mac = WiFi.macAddress(); 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; +#ifdef CUSTOM_HOSTNAME // If CUSTOM_HOSTNAME is defined, use the same hostname also for mDNS + mdnsHost = CUSTOM_HOSTNAME; #endif // Initialize mDNS .local resolution diff --git a/Software/src/devboard/wifi/wifi.h b/Software/src/devboard/wifi/wifi.h index f54f905c..bed27416 100644 --- a/Software/src/devboard/wifi/wifi.h +++ b/Software/src/devboard/wifi/wifi.h @@ -14,7 +14,6 @@ extern std::string password; extern const uint8_t wifi_channel; extern const char* ssidAP; extern const char* passwordAP; -extern const char* hostname; void init_WiFi(); void wifi_monitor(); From 9219cb2e5418f3592c3be7407bb4634d046cd01a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Jul 2025 06:47:49 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- Software/USER_SETTINGS.cpp | 10 +++++----- Software/USER_SETTINGS.h | 3 ++- Software/src/devboard/wifi/wifi.cpp | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index 1b6d93a9..7ddeb02c 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -21,11 +21,11 @@ volatile CAN_Configuration can_config = { .shunt = CAN_NATIVE // (OPTIONAL) Which CAN is your shunt connected to? }; -std::string ssid = WIFI_SSID; // Set in USER_SECRETS.h -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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h -const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection +std::string ssid = WIFI_SSID; // Set in USER_SECRETS.h +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* passwordAP = AP_PASSWORD; // Set in USER_SECRETS.h +const uint8_t wifi_channel = 0; // Set to 0 for automatic channel selection #ifdef WEBSERVER const char* http_username = HTTP_USERNAME; // Set in USER_SECRETS.h diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index b09ed532..6e50d3c4 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -118,7 +118,8 @@ /* Connectivity options */ #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 CUSTOM_HOSTNAME "battery-emulator" //Enable this line to use a custom hostname for the device, if disabled the default naming format 'esp32-XXXXXX' will be used. +#define CUSTOM_HOSTNAME \ + "battery-emulator" //Enable this line to use a custom hostname for the device, if disabled the default naming format 'esp32-XXXXXX' will be used. #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 MDNSRESPONDER //Enable this line to enable MDNS, allows battery monitor te be found by .local address. Requires WEBSERVER to be enabled. diff --git a/Software/src/devboard/wifi/wifi.cpp b/Software/src/devboard/wifi/wifi.cpp index f7b59d23..b671af8b 100644 --- a/Software/src/devboard/wifi/wifi.cpp +++ b/Software/src/devboard/wifi/wifi.cpp @@ -186,7 +186,7 @@ void init_mDNS() { // e.g batteryemulator8C.local where the mac address is 08:F9:E0:D1:06:8C String mac = WiFi.macAddress(); String mdnsHost = "batteryemulator" + mac.substring(mac.length() - 2); -#ifdef CUSTOM_HOSTNAME // If CUSTOM_HOSTNAME is defined, use the same hostname also for mDNS +#ifdef CUSTOM_HOSTNAME // If CUSTOM_HOSTNAME is defined, use the same hostname also for mDNS mdnsHost = CUSTOM_HOSTNAME; #endif From 02db212cd5ecaffa54de39896cfef3340c201823 Mon Sep 17 00:00:00 2001 From: Fredrik Date: Mon, 7 Jul 2025 08:59:57 +0200 Subject: [PATCH 5/5] Disable CUSTOM_HOSTNAME as default --- Software/USER_SETTINGS.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index 6e50d3c4..94c5e541 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -118,7 +118,7 @@ /* Connectivity options */ #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 CUSTOM_HOSTNAME \ +//#define CUSTOM_HOSTNAME \ "battery-emulator" //Enable this line to use a custom hostname for the device, if disabled the default naming format 'esp32-XXXXXX' will be used. #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.