Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aquamqtt): invalid temp parsing, broken ota, provide overrides via mqtt #7

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AquaMQTT/include/config/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ constexpr char ntpServer[] = "pool.ntp.org";

constexpr EOperationMode OPERATION_MODE = EOperationMode::MITM;

constexpr uint8_t WATCHDOG_TIMEOUT_S = 5;
constexpr uint8_t WATCHDOG_TIMEOUT_S = 60;
constexpr uint16_t MQTT_STATS_UPDATE_MS = 5000;
constexpr uint8_t MQTT_MAX_TOPIC_SIZE = 50;
constexpr uint8_t MQTT_MAX_PAYLOAD_SIZE = 50;
Expand Down
2 changes: 0 additions & 2 deletions AquaMQTT/include/config/ExampleConfiguration.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_EXAMPLECONFIGURATION_H
#define AQUAMQTT_EXAMPLECONFIGURATION_H

#include "Arduino.h"

namespace aquamqtt
{
namespace config
Expand Down
2 changes: 0 additions & 2 deletions AquaMQTT/include/handler/RTC.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_RTC_H
#define AQUAMQTT_RTC_H

#include <Arduino.h>

#include "RTClib.h"

namespace aquamqtt
Expand Down
2 changes: 1 addition & 1 deletion AquaMQTT/include/message/HMIMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HMIMessage

bool isTimerModeEnabled();

void setTimerModeEnabled(boolean enabled);
void setTimerModeEnabled(bool enabled);

bool isEmergencyModeEnabled();

Expand Down
14 changes: 7 additions & 7 deletions AquaMQTT/include/message/MessageConstants.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef AQUAMQTT_MESSAGECONSTANTS_H
#define AQUAMQTT_MESSAGECONSTANTS_H

#include "Arduino.h"
#include "mqtt/MQTTDefinitions.h"

namespace aquamqtt
Expand Down Expand Up @@ -107,15 +106,16 @@ static const char* testModeStr(HMITestMode config)
}
}


enum HMIInstallation
{
INST_HP_UNKNOWN = -1,
INST_HP_ONLY = 0,
INST_HP_AND_EXT_PRIO_HP = 1,
INST_HP_AND_EXT_OPT_HP = 17,
INST_HP_AND_EXT_OPT_EXT = 33,
INST_HP_AND_EXT_PRIO_EXT = 49,
INST_HP_AND_SOLAR = 50,
INST_HP_ONLY = 0, // 0000 0000
INST_HP_AND_EXT_PRIO_HP = 1, // 0000 0001
INST_HP_AND_EXT_OPT_HP = 17, // 0001 0001
INST_HP_AND_EXT_OPT_EXT = 33, // 0010 0001
INST_HP_AND_EXT_PRIO_EXT = 49, // 0011 0001
INST_HP_AND_SOLAR = 2, // 0000 0010
};

static const char* installationModeStr(HMIInstallation installation)
Expand Down
3 changes: 3 additions & 0 deletions AquaMQTT/include/mqtt/IMQTTCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class IMQTTCallback

// $prefix/aquamqtt/ctrl/waterTempTarget + float
virtual void onWaterTempTargetChanged(std::unique_ptr<float> value) = 0;

// $prefix/aquamqtt/ctrl/reset
virtual void onResetOverrides() = 0;
};
} // namespace aquamqtt
#endif // AQUAMQTT_IMQTTCALLBACK_H
6 changes: 6 additions & 0 deletions AquaMQTT/include/mqtt/MQTTDefinitions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef AQUAMQTT_MQTTDEFINITIONS_H
#define AQUAMQTT_MQTTDEFINITIONS_H

#include "pgmspace.h"

namespace aquamqtt
{
namespace mqtt
Expand Down Expand Up @@ -100,6 +102,10 @@ const char STATS_MSG_UNHANDLED[] PROGMEM = { "msgUnhandled" };
const char STATS_MSG_SENT[] PROGMEM = { "msgSent" };
const char STATS_MSG_CRC_NOK[] PROGMEM = { "msgCRCNOK" };
const char STATS_DROPPED_BYTES[] PROGMEM = { "droppedBytes" };
const char STATS_ACTIVE_OVERRIDES[] PROGMEM = { "activeOverrides" };

// CTRL
const char AQUAMQTT_RESET_OVERRIDES[] PROGMEM = { "reset" };

} // namespace mqtt
} // namespace aquamqtt
Expand Down
15 changes: 11 additions & 4 deletions AquaMQTT/include/state/HMIStateProxy.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_HMISTATEPROXY_H
#define AQUAMQTT_HMISTATEPROXY_H

#include <Arduino.h>

#include "TimeLib.h"
#include "message/HMIMessage.h"
#include "mqtt/IMQTTCallback.h"
Expand All @@ -11,6 +9,12 @@
namespace aquamqtt
{

struct AquaMqttOverrides
{
bool operationMode;
bool waterTempTarget;
};

/**
* HMIStateProxy accesses current HMI message from the DHW state
* and applies overrides depending on the AquaMQTT Operation Mode
Expand All @@ -27,7 +31,7 @@ class HMIStateProxy : public IMQTTCallback
HMIStateProxy(const HMIStateProxy&) = delete;

private:
HMIStateProxy();;
HMIStateProxy();

public:
HMIStateProxy& operator=(const HMIStateProxy&) = delete;
Expand All @@ -42,14 +46,17 @@ class HMIStateProxy : public IMQTTCallback

void onWaterTempTargetChanged(std::unique_ptr<float> value) override;

void onResetOverrides() override;

AquaMqttOverrides getOverrides();

private:
TaskHandle_t mNotify;
SemaphoreHandle_t mMutex;

// since we do not have optionals, use nullptr if override is not set
std::unique_ptr<float> mTargetTemperature;
std::unique_ptr<message::HMIOperationMode> mOperationMode;
std::unique_ptr<bool> mTimerModeEnabled;
};

} // namespace aquamqtt
Expand Down
2 changes: 0 additions & 2 deletions AquaMQTT/include/task/ControllerTask.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_CONTROLLERTASK_H
#define AQUAMQTT_CONTROLLERTASK_H

#include <Arduino.h>

#include "buffer/FrameBuffer.h"
#include "message/MessageConstants.h"

Expand Down
2 changes: 0 additions & 2 deletions AquaMQTT/include/task/HMITask.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_HMITASK_H
#define AQUAMQTT_HMITASK_H

#include <Arduino.h>

#include "buffer/FrameBuffer.h"
#include "message/MessageConstants.h"

Expand Down
2 changes: 0 additions & 2 deletions AquaMQTT/include/task/ListenerTask.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef AQUAMQTT_LISTENERTASK_H
#define AQUAMQTT_LISTENERTASK_H

#include <Arduino.h>

#include "buffer/FrameBuffer.h"

namespace aquamqtt
Expand Down
1 change: 0 additions & 1 deletion AquaMQTT/include/task/MQTTTask.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef AQUAMQTT_MQTTTASK_H
#define AQUAMQTT_MQTTTASK_H

#include <Arduino.h>
#include <MQTT.h>
#include <WiFiClient.h>

Expand Down
3 changes: 3 additions & 0 deletions AquaMQTT/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ test_framework = googletest

[env:native]
platform = native
build_src_filter = src/**
lib_deps =
googletest

[env:arduino_nano_esp32]
platform = espressif32
Expand Down
50 changes: 32 additions & 18 deletions AquaMQTT/src/message/HMIMessage.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "message/HMIMessage.h"

#include <cstdio>

namespace aquamqtt
{
namespace message
Expand All @@ -10,12 +12,13 @@ HMIMessage::HMIMessage(uint8_t* data) : mData(data)
}
float HMIMessage::waterTempTarget()
{
return (float) (((int16_t) mData[2] << 8) | mData[1]) / 10.0;
return (float) (((short int) (mData[2] << 8) | mData[1]) / 10.0);
}
void HMIMessage::setWaterTempTarget(float targetTemperature)
{
mData[1] = (int) targetTemperature * 10 & 0xFF;
mData[2] = ((int) targetTemperature * 10 >> 8) & 0xFF;
short int rawValue = targetTemperature * 100 / 10;
mData[1] = rawValue & 0xFF;
mData[2] = (rawValue >> 8) & 0xFF;
}
HMIOperationMode HMIMessage::operationMode()
{
Expand Down Expand Up @@ -46,7 +49,7 @@ bool HMIMessage::isTimerModeEnabled()
{
return mData[3] & 0x40;
}
void HMIMessage::setTimerModeEnabled(boolean enabled)
void HMIMessage::setTimerModeEnabled(bool enabled)
{
mData[3] = (mData[3] & ~(1 << 6)) | (enabled << 6);
}
Expand Down Expand Up @@ -94,24 +97,35 @@ HMIAirDuctConfig HMIMessage::airDuctConfig()
}
HMIInstallation HMIMessage::installationMode()
{
switch (mData[7])
if (mData[7] & 0x02)
{
case 0:
return HMIInstallation::INST_HP_ONLY;
case 1:
return HMIInstallation::INST_HP_AND_EXT_PRIO_HP;
case 17:
return HMIInstallation::INST_HP_AND_EXT_OPT_HP;
case 33:
return HMIInstallation::INST_HP_AND_EXT_OPT_EXT;
case 49:
return HMIInstallation::INST_HP_AND_SOLAR;
}
else if (mData[7] & 0x01)
{
if (mData[7] & 0x10 && mData[7] & 0x20)
{
return HMIInstallation::INST_HP_AND_EXT_PRIO_EXT;
case 50:
return HMIInstallation::INST_HP_AND_SOLAR;
default:
return HMIInstallation::INST_HP_UNKNOWN;
}
else if (!(mData[7] & 0x10) && mData[7] & 0x20)
{
return HMIInstallation::INST_HP_AND_EXT_OPT_EXT;
}
else if (mData[7] & 0x10 && !(mData[7] & 0x20))
{
return HMIInstallation::INST_HP_AND_EXT_OPT_HP;
}
else
{
return HMIInstallation::INST_HP_AND_EXT_PRIO_HP;
}
}
else
{
return HMIInstallation::INST_HP_ONLY;
}
}

HMITestMode HMIMessage::testMode()
{
switch (mData[22])
Expand Down
8 changes: 4 additions & 4 deletions AquaMQTT/src/message/MainStatusMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ namespace message

float message::MainStatusMessage::hotWaterTemp()
{
return (float) (((int16_t) mData[2] << 8) | mData[1]) / 10.0;
return (float) (((short int) (mData[2] << 8) | mData[1]) / 10.0);
}
float MainStatusMessage::airTemp()
{
return (float) (((int16_t) mData[4] << 8) | mData[3]) / 10.0;
return (float) (((short int) (mData[4] << 8) | mData[3]) / 10.0);
}
float MainStatusMessage::evaporatorLowerAirTemp()
{
return (float) (((int16_t) mData[6] << 8) | mData[5]) / 10.0;
return (float) (((short int) (mData[6] << 8) | mData[5]) / 10.0);
}
float MainStatusMessage::evaporatorUpperAirTemp()
{
return (float) (((int16_t) mData[8] << 8) | mData[7]) / 10.0;
return (float) (((short int) (mData[8] << 8) | mData[7]) / 10.0);
}
uint16_t MainStatusMessage::fanSpeed()
{
Expand Down
34 changes: 33 additions & 1 deletion AquaMQTT/src/state/HMIStateProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ HMIStateProxy::HMIStateProxy()
, mNotify(nullptr)
, mOperationMode(nullptr)
, mTargetTemperature(nullptr)
, mTimerModeEnabled(nullptr)
{
}

Expand Down Expand Up @@ -125,4 +124,37 @@ void HMIStateProxy::onWaterTempTargetChanged(std::unique_ptr<float> value)

xSemaphoreGive(mMutex);
}

void HMIStateProxy::onResetOverrides()
{
if (!xSemaphoreTake(mMutex, portMAX_DELAY))
{
return;
}

mOperationMode = std::unique_ptr<message::HMIOperationMode>(nullptr);
mTargetTemperature = std::unique_ptr<float>(nullptr);

// message 194 has changed
if (mNotify != nullptr)
{
xTaskNotifyIndexed(mNotify, 0, (1UL << 8UL), eSetBits);
}

xSemaphoreGive(mMutex);
}
AquaMqttOverrides HMIStateProxy::getOverrides()
{
if (!xSemaphoreTake(mMutex, portMAX_DELAY))
{
return AquaMqttOverrides{};
}

auto retVal = AquaMqttOverrides{ mOperationMode != nullptr, mTargetTemperature != nullptr };

xSemaphoreGive(mMutex);

return retVal;
}

} // namespace aquamqtt
Loading