From ec332c5f76004374d6873dec7747df6ab6988bd3 Mon Sep 17 00:00:00 2001 From: Tobias Maier Date: Thu, 14 Nov 2024 23:15:43 +0100 Subject: [PATCH] added proper hostname --- platformio.ini | 4 +--- src/networking/networking.cpp | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 72dc857..8ad159e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,8 +18,6 @@ lib_deps = robtillaart/INA226@^0.4.4 bblanchon/ArduinoJson@^6.21.3 ArduinoLog - upload_protocol = espota upload_port = 192.168.5.181 - -lib_extra_dirs = libs \ No newline at end of file +lib_extra_dirs = libs diff --git a/src/networking/networking.cpp b/src/networking/networking.cpp index d90c005..0622a49 100644 --- a/src/networking/networking.cpp +++ b/src/networking/networking.cpp @@ -12,9 +12,30 @@ extern NetworkData wifi_data; extern NetworkData ethernet_data; extern Preferences prefs; +enum HostnameType { + Wireless, + Ethernet, + Generic +}; + +const char * get_hostname(HostnameType host_type) { + String default_hostname = "Waterlevel-" + String(mac_address, HEX); + String hostname = prefs.getString("hostname", default_hostname); + switch (host_type) + { + case Wireless: + return (hostname + "-wl").c_str(); + case Ethernet: + return (hostname + "-eth").c_str(); + case Generic: + return hostname.c_str(); + } +} + void wifi_task(void* parameter) { Log.verbose("Starting WiFi Task"); + WiFi.setHostname(get_hostname(Wireless)); while (true) { if (prefs.getString(ssid_key, "") == "" || failed_connection_attempts > 5) { wifi_data.link = false; @@ -24,7 +45,7 @@ void wifi_task(void* parameter) Log.verbose("No SSID saved, starting SoftAP"); } - String ap_ssid = "Watermeter-" + String(mac_address); + String ap_ssid = get_hostname(Wireless); WiFi.softAP(ap_ssid, ""); Log.verbose("[WIFI_TASK] Waiting for SSID now..."); @@ -60,11 +81,11 @@ void ethernet_task(void* parameter) { Log.verbose("Connecting Ethernet"); ETH.begin(0, 17, 23, 18); - ETH.setHostname("watermeter"); + ETH.setHostname(get_hostname(Ethernet)); while (true) { ethernet_data.link = ETH.linkUp(); ethernet_data.rssi = ETH.linkSpeed(); ethernet_data.ip_address = ETH.localIP().toString(); delay(60 * 1000); } -} +} \ No newline at end of file