Use unique SSID on first boot

This commit is contained in:
Gunnar Skjold
2026-02-19 12:34:13 +01:00
parent 0b4884652f
commit 4698442101
5 changed files with 39 additions and 28 deletions

View File

@@ -381,6 +381,8 @@ public:
bool isZmartChargeConfigChanged();
void ackZmartChargeConfig();
uint32_t getChipId();
void getUniqueName(char* buffer, size_t length);
void clear();

View File

@@ -124,16 +124,12 @@ void AmsConfiguration::clearNetworkConfig(NetworkConfig& config) {
memset(config.ssid, 0, 32);
memset(config.psk, 0, 64);
clearNetworkConfigIp(config);
uint16_t chipId;
getUniqueName(config.hostname, 32);
#if defined(ESP32)
chipId = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
config.power = 195;
#else
chipId = ESP.getChipId();
config.power = 205;
#endif
strcpy(config.hostname, (String("ams-") + String(chipId, HEX)).c_str());
config.mdns = true;
config.sleep = 0xFF;
config.use11b = 1;
@@ -983,6 +979,23 @@ void AmsConfiguration::setUiLanguageChanged() {
uiLanguageChanged = true;
}
uint32_t AmsConfiguration::getChipId() {
uint32_t chipId;
#if defined(ESP32)
for(int i=0; i<17; i=i+8) {
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
#else
chipId = ESP.getChipId();
#endif
return chipId;
}
void AmsConfiguration::getUniqueName(char* buffer, size_t length) {
uint32_t chipId = getChipId();
snprintf(buffer, length, "ams-%06x", chipId);
}
void AmsConfiguration::clear() {
EEPROM.begin(EEPROM_SIZE);

View File

@@ -1,7 +1,7 @@
{
"version": "%s",
"chip": "%s",
"chipId": "%s",
"chipId": "%06x",
"cpu": %d,
"mac": "%s",
"apmac": "%s",

View File

@@ -306,22 +306,14 @@ void AmsWebServer::sysinfoJson() {
SystemConfig sys;
config->getSystemConfig(sys);
uint32_t chipId;
#if defined(ESP32)
chipId = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
#else
chipId = ESP.getChipId();
#endif
String chipIdStr = String(chipId, HEX);
String hostname;
char hostname[32];
if(sys.userConfigured) {
NetworkConfig networkConfig;
config->getNetworkConfig(networkConfig);
hostname = String(networkConfig.hostname);
strncpy(hostname, networkConfig.hostname, 32);
} else {
hostname = "ams-"+chipIdStr;
config->getUniqueName(hostname, 32);
}
IPAddress localIp;
@@ -421,7 +413,7 @@ void AmsWebServer::sysinfoJson() {
#elif defined(ESP8266)
"esp8266",
#endif
chipIdStr.c_str(),
config->getChipId(),
cpu_freq,
macStr,
apMacStr,
@@ -429,7 +421,7 @@ void AmsWebServer::sysinfoJson() {
sys.vendorConfigured ? "true" : "false",
sys.userConfigured ? "true" : "false",
sys.dataCollectionConsent,
hostname.c_str(),
hostname,
performRestart ? "true" : "false",
updater->getProgress() > 0.0 && upinfo.errorCode == 0 ? "true" : "false",
#if defined(ESP8266)

View File

@@ -901,13 +901,7 @@ void handleEnergySpeedometer() {
if(sysConfig.energyspeedometer == 7) {
if(!meterState.getMeterId().isEmpty()) {
if(energySpeedometer == NULL) {
uint16_t chipId;
#if defined(ESP32)
chipId = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
#else
chipId = ESP.getChipId();
#endif
strcpy(energySpeedometerConfig.clientId, (String("ams") + String(chipId, HEX)).c_str());
config.getUniqueName(energySpeedometerConfig.clientId, 32);
energySpeedometer = new JsonMqttHandler(energySpeedometerConfig, &Debug, (char*) commonBuffer, &hw, &ds, &updater);
energySpeedometer->setCaVerification(false);
}
@@ -1460,7 +1454,17 @@ void toggleSetupMode() {
#else
WiFi.beginSmartConfig();
#endif
WiFi.softAP(PSTR("AMS2MQTT"));
char ssid[32];
if(sysConfig.vendorConfigured) {
// Use the standard SSID if the vendor has configured the device
strcpy_P(ssid, PSTR("AMS2MQTT"));
} else {
// If not vendor configured, use a unique SSID to avoid conflicts if multiple devices are in setup mode at the same time
config.getUniqueName(ssid, 32);
}
WiFi.softAP(ssid);
debugI_P(PSTR("SSID: %s"), ssid);
if(dnsServer == NULL) {
dnsServer = new DNSServer();