Compare commits

..

2 Commits

Author SHA1 Message Date
Gunnar Skjold
c49ff9f3ec Allow up to 8 cycles to charge capacitor 2026-02-27 09:06:19 +01:00
Gunnar Skjold
ae473cab20 Moved reset of reboot reason to main program 2026-02-27 08:55:10 +01:00
5 changed files with 44 additions and 56 deletions

View File

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

View File

@@ -124,12 +124,16 @@ void AmsConfiguration::clearNetworkConfig(NetworkConfig& config) {
memset(config.ssid, 0, 32);
memset(config.psk, 0, 64);
clearNetworkConfigIp(config);
getUniqueName(config.hostname, 32);
uint16_t chipId;
#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;
@@ -979,23 +983,6 @@ 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": "%06x",
"chipId": "%s",
"cpu": %d,
"mac": "%s",
"apmac": "%s",

View File

@@ -61,14 +61,6 @@ AmsWebServer::AmsWebServer(uint8_t* buf, Stream* Debug, HwTools* hw, ResetDataCo
this->hw = hw;
this->buf = (char*) buf;
this->rdc = rdc;
if(rdc->magic != 0x4a) {
rdc->last_cause = 0;
rdc->cause = 0;
rdc->magic = 0x4a;
} else {
rdc->last_cause = rdc->cause;
rdc->cause = 0;
}
}
void AmsWebServer::setup(AmsConfiguration* config, GpioConfig* gpioConfig, AmsData* meterState, AmsDataStorage* ds, EnergyAccounting* ea, RealtimePlot* rtp, AmsFirmwareUpdater* updater) {
@@ -306,14 +298,22 @@ void AmsWebServer::sysinfoJson() {
SystemConfig sys;
config->getSystemConfig(sys);
char hostname[32];
uint32_t chipId;
#if defined(ESP32)
chipId = ( ESP.getEfuseMac() >> 32 ) % 0xFFFFFFFF;
#else
chipId = ESP.getChipId();
#endif
String chipIdStr = String(chipId, HEX);
String hostname;
if(sys.userConfigured) {
NetworkConfig networkConfig;
config->getNetworkConfig(networkConfig);
strncpy(hostname, networkConfig.hostname, 32);
hostname = String(networkConfig.hostname);
} else {
config->getUniqueName(hostname, 32);
hostname = "ams-"+chipIdStr;
}
IPAddress localIp;
@@ -413,7 +413,7 @@ void AmsWebServer::sysinfoJson() {
#elif defined(ESP8266)
"esp8266",
#endif
config->getChipId(),
chipIdStr.c_str(),
cpu_freq,
macStr,
apMacStr,
@@ -421,7 +421,7 @@ void AmsWebServer::sysinfoJson() {
sys.vendorConfigured ? "true" : "false",
sys.userConfigured ? "true" : "false",
sys.dataCollectionConsent,
hostname,
hostname.c_str(),
performRestart ? "true" : "false",
updater->getProgress() > 0.0 && upinfo.errorCode == 0 ? "true" : "false",
#if defined(ESP8266)

View File

@@ -30,6 +30,7 @@ ADC_MODE(ADC_VCC);
#include "ZmartChargeCloudConnector.h"
#endif
#define MAX_BOOT_CYCLES 8
#define WDT_TIMEOUT 120
#if defined(SLOW_PROC_TRIGGER_MS)
#warning "Using predefined slow process trigger"
@@ -191,8 +192,6 @@ CloudConnector *cloud = NULL;
ZmartChargeCloudConnector *zcloud = NULL;
#endif
#define MAX_BOOT_CYCLES 6
#if defined(ESP32)
__NOINIT_ATTR EnergyAccountingRealtimeData rtd;
RTC_DATA_ATTR uint8_t bootcount = 0;
@@ -466,7 +465,7 @@ void setup() {
float vcc = hw.getVcc();
debugI_P(PSTR("Voltage: %.2fV"), vcc);
bool deepSleep = true;
bool deepSleep = false; // Disable for now, as it makes it difficult to debug why devices rebooted
#if defined(ESP32)
float allowedDrift = bootcount * 0.01;
#else
@@ -500,6 +499,16 @@ void setup() {
#if defined(ESP8266)
resetBootCycleCounter(deepSleep);
#endif
if(rdc.magic != 0x4a) {
rdc.last_cause = 0;
rdc.cause = 0;
rdc.magic = 0x4a;
} else {
rdc.last_cause = rdc.cause;
rdc.cause = 0;
}
hw.ledOff(LED_YELLOW);
hw.ledOff(LED_INTERNAL);
@@ -901,7 +910,13 @@ void handleEnergySpeedometer() {
if(sysConfig.energyspeedometer == 7) {
if(!meterState.getMeterId().isEmpty()) {
if(energySpeedometer == NULL) {
config.getUniqueName(energySpeedometerConfig.clientId, 32);
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());
energySpeedometer = new JsonMqttHandler(energySpeedometerConfig, &Debug, (char*) commonBuffer, &hw, &ds, &updater);
energySpeedometer->setCaVerification(false);
}
@@ -1454,29 +1469,17 @@ void toggleSetupMode() {
#else
WiFi.beginSmartConfig();
#endif
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);
}
uint8_t debugLevel = RemoteDebug::INFO;
#if defined(DEBUG_MODE)
debugLevel = RemoteDebug::VERBOSE;
#endif
WiFi.softAP(ssid);
Debug.setSerialEnabled(true);
Debug.begin(F("192.168.4.1"), 23, debugLevel);
debugI_P(PSTR("SSID: %s"), ssid);
WiFi.softAP(PSTR("AMS2MQTT"));
if(dnsServer == NULL) {
dnsServer = new DNSServer();
}
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
dnsServer->start(53, PSTR("*"), WiFi.softAPIP());
#if defined(DEBUG_MODE)
Debug.setSerialEnabled(true);
Debug.begin(F("192.168.4.1"), 23, RemoteDebug::VERBOSE);
#endif
setupMode = true;
hw.setBootSuccessful(false);