diff --git a/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.cpp b/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.cpp index 589a7bcd..722f3bfa 100644 --- a/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.cpp +++ b/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.cpp @@ -44,11 +44,24 @@ void HeaterMeterClientProbe::clear(void) HasTemperatureDph = false; } +void HeaterMeterClientPidInternals::clear(void) +{ + LastUpdate = 0; + + P = 0.0f; + I = 0.0f; + D = 0.0f; + dT = 0.0f; +} + void HeaterMeterClientPidOutput::clear(void) { + Enabled = false; Current = 0; Fan = 0; Servo = 0; + + Internals.clear(); } void HeaterMeterClientPid::clear(void) @@ -154,7 +167,7 @@ void HeaterMeterClient::handleHmStatus(char* data) DeserializationError err = deserializeJson(doc, data); if (err) { - Serial.print(F("Could not deserialize JSON event: ")); + Serial.print(F("Could not deserialize hmstatus event: ")); Serial.println(err.c_str()); return; } @@ -162,12 +175,35 @@ void HeaterMeterClient::handleHmStatus(char* data) updateProxyFromJson(doc); } +void HeaterMeterClient::handlePidInt(char* data) +{ + StaticJsonDocument<128> doc; + DeserializationError err = deserializeJson(doc, data); + if (err) + { + Serial.print(F("Could not deserialize pidint event: ")); + Serial.println(err.c_str()); + return; + } + + state.Output.Internals.LastUpdate = millis(); + state.Output.Internals.P = doc["p"]; + state.Output.Internals.I = doc["i"]; + state.Output.Internals.D = doc["d"]; + state.Output.Internals.dT = doc["t"]; + + if (onPidInt) + onPidInt(); +} + void HeaterMeterClient::handleServerSentEvent(char* data) { //Serial.print(_eventType); Serial.write('='); Serial.print(data); if (strcmp(_eventType, "hmstatus") == 0) handleHmStatus(data); + else if (strcmp(_eventType, "pidint") == 0) + handlePidInt(data); else { //Serial.print(F("Unhandled server-sent event type: ")); diff --git a/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.h b/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.h index 7ae59542..3e6aeb96 100644 --- a/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.h +++ b/arduino/libraries/HeaterMeterClient/src/HeaterMeterClient.h @@ -22,12 +22,25 @@ struct HeaterMeterClientProbe void clear(void); }; +struct HeaterMeterClientPidInternals +{ + uint32_t LastUpdate; + float P; + float I; + float D; + float dT; + + void clear(); + bool valid() { return ((LastUpdate != 0) && (millis() - LastUpdate < 3000)); } +}; + struct HeaterMeterClientPidOutput { bool Enabled; // false if "Off" uint8_t Current; uint8_t Fan; uint8_t Servo; + struct HeaterMeterClientPidInternals Internals; void clear(void); }; @@ -63,6 +76,7 @@ class HeaterMeterClient std::function onConnect; std::function onDisconnect; std::function onHmStatus; + std::function onPidInt; std::function onError; std::function onProtocolStateChange; @@ -80,6 +94,7 @@ class HeaterMeterClient bool readLine(char** pos, size_t* len); void updateProxyFromJson(JsonDocument& doc); void handleHmStatus(char* data); + void handlePidInt(char* data); void handleServerSentEvent(char* data); void handleServerSentLine(void); void clientConnect(void);