Compare commits

..

2 Commits

Author SHA1 Message Date
Gunnar Skjold
60d9e1fd22 Debugger adjustments 2026-03-05 08:28:32 +01:00
Gunnar Skjold
4698442101 Use unique SSID on first boot 2026-02-19 12:34:13 +01:00
13 changed files with 129 additions and 111 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

@@ -7,7 +7,8 @@
#ifndef _AMSDATA_H
#define _AMSDATA_H
#include <WString.h>
#include "Arduino.h"
#include <Timezone.h>
#include "OBIScodes.h"
enum AmsType {
@@ -27,7 +28,7 @@ public:
AmsData();
void apply(AmsData& other);
void apply(const OBIS_code_t obis, double value, uint64_t millis64);
void apply(const OBIS_code_t obis, double value);
uint64_t getLastUpdateMillis();

View File

@@ -5,7 +5,6 @@
*/
#include "AmsData.h"
#include <algorithm>
AmsData::AmsData() {}
@@ -18,6 +17,7 @@ void AmsData::apply(AmsData& other) {
uint32_t power = (activeImportPower + other.getActiveImportPower()) / 2;
float add = power * (((float) ms) / 3600000.0);
activeImportCounter += add / 1000.0;
//Serial.printf("%dW, %dms, %.6fkWh added\n", other.getActiveImportPower(), ms, add);
}
if(other.getListType() > 1) {
@@ -112,7 +112,7 @@ void AmsData::apply(AmsData& other) {
this->activeExportPower = other.getActiveExportPower();
}
void AmsData::apply(OBIS_code_t obis, double value, uint64_t millis64) {
void AmsData::apply(OBIS_code_t obis, double value) {
if(obis.sensor == 0 && obis.gr == 0 && obis.tariff == 0) {
meterType = value;
}
@@ -127,137 +127,138 @@ void AmsData::apply(OBIS_code_t obis, double value, uint64_t millis64) {
}
}
if(obis.tariff != 0) {
Serial.println("Tariff not implemented");
return;
}
if(obis.gr == 7) { // Instant values
switch(obis.sensor) {
case 1:
activeImportPower = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 2:
activeExportPower = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 3:
reactiveImportPower = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 4:
reactiveExportPower = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 13:
powerFactor = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 21:
l1activeImportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 22:
l1activeExportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 31:
l1current = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 32:
l1voltage = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 33:
l1PowerFactor = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 41:
l2activeImportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 42:
l2activeExportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 51:
l2current = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 52:
l2voltage = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 53:
l2PowerFactor = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 61:
l3activeImportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 62:
l3activeExportPower = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 71:
l3current = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 72:
l3voltage = value;
listType = std::max(listType, (uint8_t) 2);
listType = max(listType, (uint8_t) 2);
break;
case 73:
l3PowerFactor = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
}
} else if(obis.gr == 8) { // Accumulated values
switch(obis.sensor) {
case 1:
activeImportCounter = value;
listType = std::max(listType, (uint8_t) 3);
listType = max(listType, (uint8_t) 3);
break;
case 2:
activeExportCounter = value;
listType = std::max(listType, (uint8_t) 3);
listType = max(listType, (uint8_t) 3);
break;
case 3:
reactiveImportCounter = value;
listType = std::max(listType, (uint8_t) 3);
listType = max(listType, (uint8_t) 3);
break;
case 4:
reactiveExportCounter = value;
listType = std::max(listType, (uint8_t) 3);
listType = max(listType, (uint8_t) 3);
break;
case 21:
l1activeImportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 22:
l1activeExportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 41:
l2activeImportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 42:
l2activeExportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 61:
l3activeImportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
case 62:
l3activeExportCounter = value;
listType = std::max(listType, (uint8_t) 4);
listType = max(listType, (uint8_t) 4);
break;
}
}
if(listType > 0)
lastUpdateMillis = millis64;
lastUpdateMillis = millis();
threePhase = l1voltage > 0 && l2voltage > 0 && l3voltage > 0;
if(!threePhase)

View File

@@ -7,6 +7,8 @@
#ifndef _ENERGYACCOUNTING_H
#define _ENERGYACCOUNTING_H
#include "Arduino.h"
#include "AmsData.h"
#include "AmsDataStorage.h"
#include "PriceService.h"
@@ -81,7 +83,7 @@ public:
void setPriceService(PriceService *ps);
void setTimezone(Timezone*);
EnergyAccountingConfig* getConfig();
bool update(time_t now, uint64_t lastUpdatedMillis, uint8_t listType, uint32_t activeImportPower, uint32_t activeExportPower);
bool update(AmsData* amsData);
bool load();
bool save();
bool isInitialized();

View File

@@ -54,8 +54,9 @@ bool EnergyAccounting::isInitialized() {
return this->init;
}
bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t listType, uint32_t activeImportPower, uint32_t activeExportPower) {
bool EnergyAccounting::update(AmsData* amsData) {
if(config == NULL) return false;
time_t now = time(nullptr);
if(now < FirmwareVersion::BuildEpoch) return false;
if(tz == NULL) {
return false;
@@ -89,7 +90,7 @@ bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t li
calcDayCost();
}
if(local.Hour != realtimeData->currentHour && (listType >= 3 || local.Minute == 1)) {
if(local.Hour != realtimeData->currentHour && (amsData->getListType() >= 3 || local.Minute == 1)) {
tmElements_t oneHrAgo, oneHrAgoLocal;
breakTime(now-3600, oneHrAgo);
uint16_t val = round(ds->getHourImport(oneHrAgo.Hour) / 10.0);
@@ -155,9 +156,9 @@ bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t li
}
}
if(realtimeData->lastImportUpdateMillis < lastUpdatedMillis) {
unsigned long ms = lastUpdatedMillis - realtimeData->lastImportUpdateMillis;
float kwhi = (activeImportPower * (((float) ms) / 3600000.0)) / 1000.0;
if(realtimeData->lastImportUpdateMillis < amsData->getLastUpdateMillis()) {
unsigned long ms = amsData->getLastUpdateMillis() - realtimeData->lastImportUpdateMillis;
float kwhi = (amsData->getActiveImportPower() * (((float) ms) / 3600000.0)) / 1000.0;
if(kwhi > 0) {
realtimeData->use += kwhi;
float importPrice = ps == NULL ? PRICE_NO_VALUE : ps->getCurrentPrice(PRICE_DIRECTION_IMPORT);
@@ -167,12 +168,12 @@ bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t li
realtimeData->costDay += cost;
}
}
realtimeData->lastImportUpdateMillis = lastUpdatedMillis;
realtimeData->lastImportUpdateMillis = amsData->getLastUpdateMillis();
}
if(listType > 1 && realtimeData->lastExportUpdateMillis < lastUpdatedMillis) {
unsigned long ms = lastUpdatedMillis - realtimeData->lastExportUpdateMillis;
float kwhe = (activeExportPower * (((float) ms) / 3600000.0)) / 1000.0;
if(amsData->getListType() > 1 && realtimeData->lastExportUpdateMillis < amsData->getLastUpdateMillis()) {
unsigned long ms = amsData->getLastUpdateMillis() - realtimeData->lastExportUpdateMillis;
float kwhe = (amsData->getActiveExportPower() * (((float) ms) / 3600000.0)) / 1000.0;
if(kwhe > 0) {
realtimeData->produce += kwhe;
float exportPrice = ps == NULL ? PRICE_NO_VALUE : ps->getCurrentPrice(PRICE_DIRECTION_EXPORT);
@@ -182,7 +183,7 @@ bool EnergyAccounting::update(time_t now, uint64_t lastUpdatedMillis, uint8_t li
realtimeData->incomeDay += income;
}
}
realtimeData->lastExportUpdateMillis = lastUpdatedMillis;
realtimeData->lastExportUpdateMillis = amsData->getLastUpdateMillis();
}
if(config != NULL) {

View File

@@ -11,7 +11,6 @@
#include "AmsConfiguration.h"
#include "DataParser.h"
#include "Cosem.h"
#include "Timezone.h"
#if defined(AMS_REMOTE_DEBUG)
#include "RemoteDebug.h"
#endif

View File

@@ -24,6 +24,8 @@ void KmpCommunicator::configure(MeterConfig& meterConfig) {
}
bool KmpCommunicator::loop() {
uint64_t now = millis64();
bool ret = talker->loop();
int lastError = getLastError();
if(ret) {
@@ -56,36 +58,35 @@ AmsData* KmpCommunicator::getData(AmsData& meterState) {
if(talker == NULL) return NULL;
KmpDataHolder kmpData;
talker->getData(kmpData);
uint64_t now = millis64();
AmsData* data = new AmsData();
data->apply(OBIS_ACTIVE_IMPORT_COUNT, kmpData.activeImportCounter, now);
data->apply(OBIS_ACTIVE_EXPORT_COUNT, kmpData.activeExportCounter, now);
data->apply(OBIS_REACTIVE_IMPORT_COUNT, kmpData.reactiveImportCounter, now);
data->apply(OBIS_REACTIVE_EXPORT_COUNT, kmpData.reactiveExportCounter, now);
data->apply(OBIS_ACTIVE_IMPORT, kmpData.activeImportPower, now);
data->apply(OBIS_ACTIVE_EXPORT, kmpData.activeExportPower, now);
data->apply(OBIS_REACTIVE_IMPORT, kmpData.reactiveImportPower, now);
data->apply(OBIS_REACTIVE_EXPORT, kmpData.reactiveExportPower, now);
data->apply(OBIS_VOLTAGE_L1, kmpData.l1voltage, now);
data->apply(OBIS_VOLTAGE_L2, kmpData.l2voltage, now);
data->apply(OBIS_VOLTAGE_L3, kmpData.l3voltage, now);
data->apply(OBIS_CURRENT_L1, kmpData.l1current, now);
data->apply(OBIS_CURRENT_L2, kmpData.l2current, now);
data->apply(OBIS_CURRENT_L3, kmpData.l3current, now);
data->apply(OBIS_POWER_FACTOR_L1, kmpData.l1PowerFactor, now);
data->apply(OBIS_POWER_FACTOR_L2, kmpData.l2PowerFactor, now);
data->apply(OBIS_POWER_FACTOR_L3, kmpData.l3PowerFactor, now);
data->apply(OBIS_POWER_FACTOR, kmpData.powerFactor, now);
data->apply(OBIS_ACTIVE_IMPORT_L1, kmpData.l1activeImportPower, now);
data->apply(OBIS_ACTIVE_IMPORT_L2, kmpData.l2activeImportPower, now);
data->apply(OBIS_ACTIVE_IMPORT_L3, kmpData.l3activeImportPower, now);
data->apply(OBIS_ACTIVE_EXPORT_L1, kmpData.l1activeExportPower, now);
data->apply(OBIS_ACTIVE_EXPORT_L2, kmpData.l2activeExportPower, now);
data->apply(OBIS_ACTIVE_EXPORT_L3, kmpData.l3activeExportPower, now);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L1, kmpData.l1activeImportCounter, now);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L2, kmpData.l2activeImportCounter, now);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L3, kmpData.l3activeImportCounter, now);
data->apply(OBIS_METER_ID, kmpData.meterId, now);
data->apply(OBIS_NULL, AmsTypeKamstrup, now);
data->apply(OBIS_ACTIVE_IMPORT_COUNT, kmpData.activeImportCounter);
data->apply(OBIS_ACTIVE_EXPORT_COUNT, kmpData.activeExportCounter);
data->apply(OBIS_REACTIVE_IMPORT_COUNT, kmpData.reactiveImportCounter);
data->apply(OBIS_REACTIVE_EXPORT_COUNT, kmpData.reactiveExportCounter);
data->apply(OBIS_ACTIVE_IMPORT, kmpData.activeImportPower);
data->apply(OBIS_ACTIVE_EXPORT, kmpData.activeExportPower);
data->apply(OBIS_REACTIVE_IMPORT, kmpData.reactiveImportPower);
data->apply(OBIS_REACTIVE_EXPORT, kmpData.reactiveExportPower);
data->apply(OBIS_VOLTAGE_L1, kmpData.l1voltage);
data->apply(OBIS_VOLTAGE_L2, kmpData.l2voltage);
data->apply(OBIS_VOLTAGE_L3, kmpData.l3voltage);
data->apply(OBIS_CURRENT_L1, kmpData.l1current);
data->apply(OBIS_CURRENT_L2, kmpData.l2current);
data->apply(OBIS_CURRENT_L3, kmpData.l3current);
data->apply(OBIS_POWER_FACTOR_L1, kmpData.l1PowerFactor);
data->apply(OBIS_POWER_FACTOR_L2, kmpData.l2PowerFactor);
data->apply(OBIS_POWER_FACTOR_L3, kmpData.l3PowerFactor);
data->apply(OBIS_POWER_FACTOR, kmpData.powerFactor);
data->apply(OBIS_ACTIVE_IMPORT_L1, kmpData.l1activeImportPower);
data->apply(OBIS_ACTIVE_IMPORT_L2, kmpData.l2activeImportPower);
data->apply(OBIS_ACTIVE_IMPORT_L3, kmpData.l3activeImportPower);
data->apply(OBIS_ACTIVE_EXPORT_L1, kmpData.l1activeExportPower);
data->apply(OBIS_ACTIVE_EXPORT_L2, kmpData.l2activeExportPower);
data->apply(OBIS_ACTIVE_EXPORT_L3, kmpData.l3activeExportPower);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L1, kmpData.l1activeImportCounter);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L2, kmpData.l2activeImportCounter);
data->apply(OBIS_ACTIVE_IMPORT_COUNT_L3, kmpData.l3activeImportCounter);
data->apply(OBIS_METER_ID, kmpData.meterId);
data->apply(OBIS_NULL, AmsTypeKamstrup);
return data;
}

View File

@@ -7,6 +7,7 @@
#ifndef _REALTIMEPLOT_H
#define _REALTIMEPLOT_H
#include <stdint.h>
#include "AmsData.h"
#define REALTIME_SAMPLE 10000

View File

@@ -4,7 +4,6 @@
*
*/
#include "Arduino.h"
#include "RealtimePlot.h"
#include <stdlib.h>

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,17 +1454,29 @@ 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);
}
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);
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);
@@ -1607,7 +1613,7 @@ void handleDataSuccess(AmsData* data) {
debugD_P(PSTR("NOT Ready to update (internal clock %02d:%02d:%02d UTC, meter clock: %02d:%02d:%02d, list type %d, est: %d)"), tm.Hour, tm.Minute, tm.Second, mtm.Hour, mtm.Minute, mtm.Second, data->getListType(), wasCounterEstimated);
}
if(ea.update(dataUpdateTime, data->getLastUpdateMillis(), data->getListType(), data->getActiveImportPower(), data->getActiveExportPower())) {
if(ea.update(data)) {
debugI_P(PSTR("Saving energy accounting"));
if(!ea.save()) {
debugW_P(PSTR("Unable to save energy accounting"));