Skip to content

Commit

Permalink
Feature: SML power meters: reset SML decoder
Browse files Browse the repository at this point in the history
implement a function which allows to reset the SML decoder. this new
function is used after a datagram ends. for the SML HTTP power meter
this is simple: after all bytes from the request's answer have been
decoded, we reset the decoder. for the SML serial power meter, we
perform the reset after a datagram ended based on timing (no new bytes
have been received for a specific amount of time).
  • Loading branch information
schlimmchen committed Jun 27, 2024
1 parent ea45497 commit 6b09ca0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/PowerMeterSml.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PowerMeterSml : public PowerMeterProvider {
explicit PowerMeterSml(char const* user)
: _user(user) { }

void reset();
void processSmlByte(uint8_t byte);

private:
Expand Down
6 changes: 6 additions & 0 deletions lib/SMLParser/sml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ void checkMagicByte(unsigned char &byte)
}
}

void smlReset(void)
{
len = 4; // expect start sequence
currentState = SML_START;
}

sml_states_t smlState(unsigned char currentByte)
{
unsigned char size;
Expand Down
1 change: 1 addition & 0 deletions lib/SMLParser/sml.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ typedef enum {
SML_COUNT = 255
} sml_units_t;

void smlReset(void);
sml_states_t smlState(unsigned char byte);
bool smlOBISCheck(const unsigned char *obis);
void smlOBISManufacturer(unsigned char *str, int maxSize);
Expand Down
2 changes: 2 additions & 0 deletions src/PowerMeterHttpSml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,7 @@ String PowerMeterHttpSml::poll()
processSmlByte(pStream->read());
}

PowerMeterSml::reset();

return "";
}
2 changes: 2 additions & 0 deletions src/PowerMeterSerialSml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void PowerMeterSerialSml::pollingLoop()

lastAvailable = 0;

PowerMeterSml::reset();

lock.lock();
}
}
10 changes: 8 additions & 2 deletions src/PowerMeterSml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ void PowerMeterSml::doMqttPublish() const
#undef PUB
}

void PowerMeterSml::reset()
{
smlReset();
_cache = { std::nullopt };
}

void PowerMeterSml::processSmlByte(uint8_t byte)
{
switch (smlState(byte)) {
Expand All @@ -52,12 +58,12 @@ void PowerMeterSml::processSmlByte(uint8_t byte)
case SML_FINAL:
gotUpdate();
_values = _cache;
_cache = { std::nullopt };
reset();
MessageOutput.printf("[%s] TotalPower: %5.2f\r\n",
_user.c_str(), getPowerTotal());
break;
case SML_CHECKSUM_ERROR:
_cache = { std::nullopt };
reset();
MessageOutput.printf("[%s] checksum verification failed\r\n",
_user.c_str());
break;
Expand Down

0 comments on commit 6b09ca0

Please sign in to comment.