diff --git a/src/defines.h b/src/defines.h index 8167589..f43ea4d 100644 --- a/src/defines.h +++ b/src/defines.h @@ -55,7 +55,7 @@ #endif #ifndef DEFAULT_HOSTNAME - #define DEFAULT_HOSTNAME "opentherm" + #define DEFAULT_HOSTNAME "" #endif #ifndef DEFAULT_AP_SSID @@ -111,7 +111,7 @@ #endif #ifndef DEFAULT_MQTT_PREFIX - #define DEFAULT_MQTT_PREFIX "opentherm" + #define DEFAULT_MQTT_PREFIX "" #endif #ifndef DEFAULT_OT_IN_GPIO diff --git a/src/main.cpp b/src/main.cpp index 1aa5e8a..b415649 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -102,6 +102,12 @@ void setup() { break; } + // generate hostname if it is empty + if (!strlen(networkSettings.hostname)) { + strcpy(networkSettings.hostname, getChipId("otgateway-").c_str()); + fsNetworkSettings.update(); + } + network = (new NetworkMgr) ->setHostname(networkSettings.hostname) ->setStaCredentials( @@ -148,6 +154,12 @@ void setup() { break; } + // generate mqtt prefix if it is empty + if (!strlen(settings.mqtt.prefix)) { + strcpy(settings.mqtt.prefix, getChipId("otgateway_").c_str()); + fsSettings.update(); + } + // Logs settings if (!settings.system.serial.enabled) { Serial.end(); diff --git a/src/utils.h b/src/utils.h index 4a108da..51ccae1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,5 +1,37 @@ #include +String getChipId(const char* prefix = nullptr, const char* suffix = nullptr) { + String chipId; + chipId.reserve( + 6 + + (prefix != nullptr ? strlen(prefix) : 0) + + (suffix != nullptr ? strlen(suffix) : 0) + ); + + if (prefix != nullptr) { + chipId.concat(prefix); + } + + uint32_t cid = 0; + #if defined(ARDUINO_ARCH_ESP8266) + cid = ESP.getChipId(); + #elif defined(ARDUINO_ARCH_ESP32) + // https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ChipID/GetChipID/GetChipID.ino + for (uint8_t i = 0; i < 17; i = i + 8) { + cid |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; + } + #endif + + chipId += String(cid, HEX); + + if (suffix != nullptr) { + chipId.concat(suffix); + } + + chipId.trim(); + return chipId; +} + bool isLeapYear(short year) { if (year % 4 != 0) { return false;