Skip to content

Commit 0dfd2d9

Browse files
authored
Various bug fixes (#1041)
* Fixing board type overwrite, zmartcharge default issues and disabling entsoe * Fixed Zmartcharge configuration issue
1 parent 7a4ab77 commit 0dfd2d9

File tree

9 files changed

+93
-53
lines changed

9 files changed

+93
-53
lines changed

lib/AmsConfiguration/src/AmsConfiguration.cpp

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
bool AmsConfiguration::getSystemConfig(SystemConfig& config) {
1414
EEPROM.begin(EEPROM_SIZE);
1515
uint8_t configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
16+
EEPROM.get(CONFIG_SYSTEM_START, config);
17+
EEPROM.end();
1618
if(configVersion == EEPROM_CHECK_SUM) {
17-
EEPROM.get(CONFIG_SYSTEM_START, config);
18-
EEPROM.end();
1919
return true;
2020
} else {
21-
if(configVersion == EEPROM_CLEARED_INDICATOR) {
21+
if(configVersion == EEPROM_CLEARED_INDICATOR && config.boardType > 0 && config.boardType < 250) {
2222
config.vendorConfigured = true;
2323
} else {
2424
config.vendorConfigured = false;
2525
config.boardType = 0xFF;
26+
clear();
2627
}
2728
config.userConfigured = false;
2829
config.dataCollectionConsent = 0;
@@ -282,8 +283,9 @@ void AmsConfiguration::ackWebChange() {
282283
}
283284

284285
bool AmsConfiguration::getMeterConfig(MeterConfig& config) {
285-
if(hasConfig()) {
286-
EEPROM.begin(EEPROM_SIZE);
286+
EEPROM.begin(EEPROM_SIZE);
287+
uint8_t configVersion = EEPROM.read(EEPROM_CONFIG_ADDRESS);
288+
if(configVersion == EEPROM_CHECK_SUM || configVersion == EEPROM_CLEARED_INDICATOR) {
287289
EEPROM.get(CONFIG_METER_START, config);
288290
EEPROM.end();
289291
if(config.bufferSize < 1 || config.bufferSize > 64) {
@@ -903,12 +905,12 @@ bool AmsConfiguration::getZmartChargeConfig(ZmartChargeConfig& config) {
903905
EEPROM.end();
904906
stripNonAscii((uint8_t*) config.token, 21);
905907
stripNonAscii((uint8_t*) config.baseUrl, 64);
906-
if(strlen(config.token) < 20) {
908+
if(strlen(config.token) != 20 || !config.enabled) {
907909
config.enabled = false;
908910
memset(config.token, 0, 64);
909911
memset(config.baseUrl, 0, 64);
910912
}
911-
if(strncmp_P(config.baseUrl, PSTR("https"), 5) != 0) {
913+
if(strlen(config.baseUrl) == 0 || strncmp_P(config.baseUrl, PSTR("https"), 5) != 0) {
912914
memset(config.baseUrl, 0, 64);
913915
snprintf_P(config.baseUrl, 64, PSTR("https://main.zmartcharge.com/api"));
914916
}
@@ -923,8 +925,8 @@ bool AmsConfiguration::setZmartChargeConfig(ZmartChargeConfig& config) {
923925
ZmartChargeConfig existing;
924926
if(getZmartChargeConfig(existing)) {
925927
zcChanged |= config.enabled != existing.enabled;
926-
zcChanged |= memcmp(config.token, existing.token, 21) != 0;
927-
zcChanged |= memcmp(config.token, existing.baseUrl, 64) != 0;
928+
zcChanged |= strcmp(config.token, existing.token) != 0;
929+
zcChanged |= strcmp(config.baseUrl, existing.baseUrl) != 0;
928930
} else {
929931
zcChanged = true;
930932
}
@@ -1022,6 +1024,10 @@ void AmsConfiguration::clear() {
10221024
clearCloudConfig(cloud);
10231025
EEPROM.put(CONFIG_CLOUD_START, cloud);
10241026

1027+
ZmartChargeConfig zc;
1028+
clearZmartChargeConfig(zc);
1029+
EEPROM.put(CONFIG_ZC_START, zc);
1030+
10251031
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CLEARED_INDICATOR);
10261032
EEPROM.commit();
10271033
EEPROM.end();
@@ -1348,10 +1354,10 @@ void AmsConfiguration::print(Print* debugger)
13481354
debugger->printf_P(PSTR("Area: %s\r\n"), price.area);
13491355
debugger->printf_P(PSTR("Currency: %s\r\n"), price.currency);
13501356
debugger->printf_P(PSTR("ENTSO-E Token: %s\r\n"), price.entsoeToken);
1357+
debugger->println(F(""));
1358+
delay(10);
1359+
debugger->flush();
13511360
}
1352-
debugger->println(F(""));
1353-
delay(10);
1354-
debugger->flush();
13551361
}
13561362

13571363
UiConfig ui;
@@ -1369,9 +1375,29 @@ void AmsConfiguration::print(Print* debugger)
13691375
String uuid = ESPRandom::uuidToString(cc.clientId);;
13701376
debugger->println(F("--Cloud configuration--"));
13711377
debugger->printf_P(PSTR("Enabled: %s\r\n"), cc.enabled ? "Yes" : "No");
1372-
debugger->printf_P(PSTR("Hostname: %s\r\n"), cc.hostname);
1373-
debugger->printf_P(PSTR("Client ID: %s\r\n"), uuid.c_str());
1374-
debugger->printf_P(PSTR("Interval: %d\r\n"), cc.interval);
1378+
if(cc.enabled) {
1379+
debugger->printf_P(PSTR("Hostname: %s\r\n"), cc.hostname);
1380+
debugger->printf_P(PSTR("Client ID: %s\r\n"), uuid.c_str());
1381+
debugger->printf_P(PSTR("Interval: %d\r\n"), cc.interval);
1382+
}
1383+
debugger->println(F(""));
1384+
delay(10);
1385+
debugger->flush();
1386+
}
1387+
#endif
1388+
1389+
#if defined(ZMART_CHARGE)
1390+
ZmartChargeConfig zc;
1391+
if(getZmartChargeConfig(zc)) {
1392+
debugger->println(F("--ZmartCharge configuration--"));
1393+
debugger->printf_P(PSTR("Enabled: %s\r\n"), zc.enabled ? "Yes" : "No");
1394+
if(zc.enabled) {
1395+
debugger->printf_P(PSTR("Base URL: '%s'\r\n"), zc.baseUrl);
1396+
debugger->printf_P(PSTR("Token: '%s'\r\n"), zc.token);
1397+
}
1398+
debugger->println(F(""));
1399+
delay(10);
1400+
debugger->flush();
13751401
}
13761402
#endif
13771403

lib/MeterCommunicators/include/PassiveMeterCommunicator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ class PassiveMeterCommunicator : public MeterCommunicator {
6262
HardwareSerial *hwSerial = NULL;
6363
uint8_t rxBufferErrors = 0;
6464

65-
bool autodetect = false, validDataReceived = false;
65+
bool autodetect = false;
66+
uint8_t validDataReceived = 0;
6667
unsigned long meterAutodetectLastChange = 0;
6768
long rate = 10000;
6869
uint32_t autodetectBaud = 0;

lib/MeterCommunicators/src/PassiveMeterCommunicator.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ AmsData* PassiveMeterCommunicator::getData(AmsData& meterState) {
286286
len = 0;
287287
if(data != NULL) {
288288
if(data->getListType() > 0) {
289-
validDataReceived = true;
289+
validDataReceived++;
290290
if(rxBufferErrors > 0) rxBufferErrors--;
291291
}
292292
}
@@ -586,15 +586,15 @@ void PassiveMeterCommunicator::setupHanPort(uint32_t baud, uint8_t parityOrdinal
586586
autodetectBaud = baud = 2400;
587587
}
588588

589+
if(parityOrdinal == 0) {
590+
parityOrdinal = 11; // 8E1
591+
}
592+
589593
#if defined(AMS_REMOTE_DEBUG)
590594
if (debugger->isActive(RemoteDebug::INFO))
591595
#endif
592596
debugger->printf_P(PSTR("(setupHanPort) Setting up HAN on pin %d/%d with baud %d and parity %d\n"), rxpin, txpin, baud, parityOrdinal);
593597

594-
if(parityOrdinal == 0) {
595-
parityOrdinal = 3; // 8N1
596-
}
597-
598598
if(rxpin == 3 || rxpin == 113) {
599599
#if ARDUINO_USB_CDC_ON_BOOT
600600
hwSerial = &Serial0;
@@ -799,6 +799,7 @@ HardwareSerial* PassiveMeterCommunicator::getHwSerial() {
799799

800800
void PassiveMeterCommunicator::rxerr(int err) {
801801
if(err == 0) return;
802+
if(lastError == 90+err) return; // Do not flood with same error
802803
switch(err) {
803804
case 2:
804805
#if defined(AMS_REMOTE_DEBUG)
@@ -836,10 +837,9 @@ void PassiveMeterCommunicator::rxerr(int err) {
836837
unsigned long now = millis();
837838
if(autodetect) {
838839
meterAutodetectLastChange = 0;
839-
} else if(meterAutodetectLastChange > 0 && now - meterAutodetectLastChange < 120000 && validDataReceived) {
840+
} else if(validDataReceived > 2) {
840841
meterConfig.parity = getNextParity(meterConfig.parity);
841842
configChanged = true;
842-
setupHanPort(meterConfig.baud, meterConfig.parity, meterConfig.invert);
843843
}
844844
break;
845845
}
@@ -849,12 +849,14 @@ void PassiveMeterCommunicator::rxerr(int err) {
849849

850850
void PassiveMeterCommunicator::handleAutodetect(unsigned long now) {
851851
if(!autodetect) return;
852+
if(now - meterAutodetectLastChange < 12000) return;
852853

853-
if(!validDataReceived) {
854-
if(now - meterAutodetectLastChange > 12000 && (meterConfig.baud == 0 || meterConfig.parity == 0)) {
854+
if(validDataReceived < 2) {
855+
if(meterConfig.baud == 0 || meterConfig.parity == 0) {
855856
autodetect = true;
856857
if(lastError == 95) { // If parity error, switch parity
857858
autodetectParity = getNextParity(autodetectParity);
859+
lastError = 0;
858860
} else {
859861
autodetectCount++;
860862
}

lib/PriceService/src/PriceService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void PriceService::setTimezone(Timezone* tz) {
7878
}
7979

8080
char* PriceService::getToken() {
81-
return this->config->entsoeToken;
81+
return ""; // Currently the implementation is not working, so lets disable it for al. Old code: this->config->entsoeToken;
8282
}
8383

8484
char* PriceService::getCurrency() {

lib/SvelteUi/app/dist/index.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/SvelteUi/app/src/lib/ConfigurationPanel.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,9 @@
332332
</div>
333333
<div class="my-1">
334334
<label><input type="checkbox" name="pe" value="true" bind:checked={configuration.p.e} class="rounded mb-1"/> {translations.conf?.price?.enabled ?? "Enabled"}</label>
335-
{#if configuration.p.e && sysinfo.chip != 'esp8266'}
336-
<br/><input name="pt" bind:value={configuration.p.t} type="text" class="in-s" placeholder={translations.conf?.price?.api_key_placeholder ?? ""} pattern={charAndNumPattern}/>
335+
{#if configuration.p.e && sysinfo.chip != 'esp8266' && configuration.p.t}
336+
<input name="pt" type="hidden" bind:value={configuration.p.t}/>
337+
<br/><input type="text" class="in-s" placeholder="ENTSO-E API key disabled, ref issue #1030" disabled/>
337338
{/if}
338339
</div>
339340
<div class="my-1">

lib/SvelteUi/src/AmsWebServer.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,17 @@ void AmsWebServer::handleSave() {
11831183
if(!checkSecurity(1))
11841184
return;
11851185

1186+
#if defined(AMS_REMOTE_DEBUG)
1187+
if (debugger->isActive(RemoteDebug::DEBUG)) {
1188+
#endif
1189+
debugger->printf(PSTR("Received %d args for /save\n"), server.args());
1190+
for(uint8_t i = 0; i < server.args(); i++) {
1191+
debugger->printf_P(PSTR(" %s: %s\n"), server.argName(i).c_str(), server.arg(i).c_str());
1192+
}
1193+
#if defined(AMS_REMOTE_DEBUG)
1194+
}
1195+
#endif
1196+
11861197
SystemConfig sys;
11871198
config->getSystemConfig(sys);
11881199

lib/ZmartCharge/src/ZmartChargeCloudConnector.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void ZmartChargeCloudConnector::update(AmsData& data) {
3838
return;
3939
}
4040

41-
if(((now - lastUpdate) / 1000) > (fast || lastFailed ? heartbeatFast : heartbeat)) {
41+
if(lastUpdate == 0 || ((now - lastUpdate) / 1000) > (fast || lastFailed ? heartbeatFast : heartbeat)) {
4242
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf_P(PSTR("(ZmartCharge) Preparing to update cloud\n"));
4343
memset(json, 0, BufferSize);
4444
snprintf_P(json, BufferSize, ZC_LB_JSON,
@@ -86,9 +86,9 @@ void ZmartChargeCloudConnector::update(AmsData& data) {
8686
}
8787
http->end();
8888
} else {
89-
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(ZmartCharge) Communication error, returned status: %d\n"), status);
90-
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf(http->errorToString(status).c_str());
91-
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf(http->getString().c_str());
89+
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf_P(PSTR("(ZmartCharge) Communication error with %s, returned status: %d\n"), baseUrl, status);
90+
if(debugger->isActive(RemoteDebug::ERROR)) debugger->printf("%s\n", http->errorToString(status).c_str());
91+
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("%s\n", http->getString().c_str());
9292

9393
http->end();
9494
}

src/AmsToMqttBridge.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,15 @@ void setup() {
322322
if(!config.getGpioConfig(gpioConfig)) {
323323
config.clearGpio(gpioConfig);
324324
}
325-
if(config.getSystemConfig(sysConfig)) {
326-
config.getMeterConfig(meterConfig);
327-
if(sysConfig.boardType < 20) {
328-
config.clearGpio(gpioConfig, false);
329-
hw.applyBoardConfig(sysConfig.boardType, gpioConfig, meterConfig, meterConfig.rxPin);
330-
config.setMeterConfig(meterConfig);
331-
config.setGpioConfig(gpioConfig);
332-
}
333-
} else {
334-
config.clearMeter(meterConfig);
335-
sysConfig.boardType = 0;
336-
sysConfig.vendorConfigured = false;
337-
sysConfig.userConfigured = false;
338-
sysConfig.dataCollectionConsent = false;
325+
config.getSystemConfig(sysConfig);
326+
config.getMeterConfig(meterConfig);
327+
328+
if(sysConfig.boardType < 20) {
329+
Serial.printf_P(PSTR("Applying default GPIO configuration for board type %d\n"), sysConfig.boardType);
330+
config.clearGpio(gpioConfig, false);
331+
hw.applyBoardConfig(sysConfig.boardType, gpioConfig, meterConfig, meterConfig.rxPin);
332+
config.setMeterConfig(meterConfig);
333+
config.setGpioConfig(gpioConfig);
339334
}
340335

341336
delay(1);
@@ -363,6 +358,8 @@ void setup() {
363358
if(!hw.ledBlink(LED_RED, 6)) {
364359
hw.ledBlink(LED_INTERNAL, 6);
365360
}
361+
ESP.restart();
362+
return;
366363
}
367364
}
368365
}
@@ -432,10 +429,11 @@ void setup() {
432429
float vcc = hw.getVcc();
433430

434431
debugI_P(PSTR("AMS reader %s started"), FirmwareVersion::VersionString);
432+
debugI_P(PSTR("Configuration version: %d, board type: %d"), config.getConfigVersion(), sysConfig.boardType);
435433
debugI_P(PSTR("Voltage: %.2fV"), vcc);
436434

437435
float vccBootLimit = gpioConfig.vccBootLimit == 0 ? 0 : min(3.29, gpioConfig.vccBootLimit / 10.0); // Make sure it is never above 3.3v
438-
if(vccBootLimit > 2.5 && vccBootLimit < 3.3 && (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released)
436+
if(vcc > 2.5 && vccBootLimit > 2.5 && vccBootLimit < 3.3 && (gpioConfig.apPin == 0xFF || digitalRead(gpioConfig.apPin) == HIGH)) { // Skip if user is holding AP button while booting (HIGH = button is released)
439437
if (vcc < vccBootLimit) {
440438
{
441439
Debug.printf_P(PSTR("(setup) Voltage is too low (%.2f < %.2f), sleeping\n"), vcc, vccBootLimit);
@@ -1422,6 +1420,7 @@ bool readHanPort() {
14221420
}
14231421
if(mc->isConfigChanged()) {
14241422
mc->getCurrentConfig(meterConfig);
1423+
debugI_P(PSTR("Meter configuration based on auto-detect"));
14251424
config.setMeterConfig(meterConfig);
14261425
mc->ackConfigChanged();
14271426
}

0 commit comments

Comments
 (0)