Skip to content

Commit

Permalink
clean up MQTT Brew stuff
Browse files Browse the repository at this point in the history
adding case kInteger to assignMQTTParam() so backflushCycles could be changed thru MQTT
  • Loading branch information
LoQue90 committed Oct 14, 2024
1 parent 7876a45 commit 4af1bf3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 92 deletions.
2 changes: 0 additions & 2 deletions src/brewHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
*
*/
// TODO:
// FEATURE_BREWCONTROL has to be removed from userconfig, setup on website
// Flush Timer configurable and seperated from shottimer?
// check all Scale stuff
// check MQTT/HASSIO for all brew stuff
// show heating logo if steam temp isn´t reached?
// how handle brew, backflush, manualflush, hotwater if mashine is in steam mode
// show sections on website only if needed
Expand Down
57 changes: 25 additions & 32 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,6 @@ void setup() {
mqttVars["brewTempOffset"] = [] { return &editableVars.at("BREW_TEMP_OFFSET"); };
mqttVars["steamON"] = [] { return &editableVars.at("STEAM_MODE"); };
mqttVars["steamSetpoint"] = [] { return &editableVars.at("STEAM_SETPOINT"); };
mqttVars["brewPidDelay"] = [] { return &editableVars.at("PID_BD_DELAY"); };
mqttVars["backflushOn"] = [] { return &editableVars.at("BACKFLUSH_ON"); };
mqttVars["startUsePonM"] = [] { return &editableVars.at("START_USE_PONM"); };
mqttVars["startKp"] = [] { return &editableVars.at("START_KP"); };
mqttVars["startTn"] = [] { return &editableVars.at("START_TN"); };
Expand All @@ -1343,33 +1341,18 @@ void setup() {
mqttVars["aggIMax"] = [] { return &editableVars.at("PID_I_MAX"); };
mqttVars["steamKp"] = [] { return &editableVars.at("STEAM_KP"); };
mqttVars["standbyModeOn"] = [] { return &editableVars.at("STANDBY_MODE_ON"); };

if (featureBrewControl == 1) {
mqttVars["brewtime"] = [] { return &editableVars.at("BREW_TIME"); };
mqttVars["preinfusion"] = [] { return &editableVars.at("BREW_PREINFUSION"); };
mqttVars["preinfusionPause"] = [] { return &editableVars.at("BREW_PREINFUSIONPAUSE"); };
mqttVars["backflushCycles"] = [] { return &editableVars.at("BACKFLUSH_CYCLES"); };
mqttVars["backflushFillTime"] = [] { return &editableVars.at("BACKFLUSH_FILL_TIME"); };
mqttVars["backflushFlushTime"] = [] { return &editableVars.at("BACKFLUSH_FLUSH_TIME"); };
}

if (FEATURE_SCALE == 1) {
mqttVars["weightSetpoint"] = [] { return &editableVars.at("SCALE_WEIGHTSETPOINT"); };
mqttVars["scaleCalibration"] = [] { return &editableVars.at("SCALE_CALIBRATION"); };
#if SCALE_TYPE == 0
mqttVars["scale2Calibration"] = [] { return &editableVars.at("SCALE2_CALIBRATION"); };
#endif
mqttVars["scaleKnownWeight"] = [] { return &editableVars.at("SCALE_KNOWN_WEIGHT"); };
mqttVars["scaleTareOn"] = [] { return &editableVars.at("TARE_ON"); };
mqttVars["scaleCalibrationOn"] = [] { return &editableVars.at("CALIBRATION_ON"); };
}

if (FEATURE_BREWSWITCH == 1) {
mqttVars["pidUseBD"] = [] { return &editableVars.at("PID_BD_ON"); };
mqttVars["aggbKp"] = [] { return &editableVars.at("PID_BD_KP"); };
mqttVars["aggbTn"] = [] { return &editableVars.at("PID_BD_TN"); };
mqttVars["aggbTv"] = [] { return &editableVars.at("PID_BD_TV"); };
}
mqttVars["brewTime"] = [] { return &editableVars.at("BREW_TIME"); };
mqttVars["preinfusion"] = [] { return &editableVars.at("BREW_PREINFUSION"); };
mqttVars["preinfusionPause"] = [] { return &editableVars.at("BREW_PREINFUSIONPAUSE"); };
mqttVars["backflushOn"] = [] { return &editableVars.at("BACKFLUSH_ON"); };
mqttVars["backflushCycles"] = [] { return &editableVars.at("BACKFLUSH_CYCLES"); };
mqttVars["backflushFillTime"] = [] { return &editableVars.at("BACKFLUSH_FILL_TIME"); };
mqttVars["backflushFlushTime"] = [] { return &editableVars.at("BACKFLUSH_FLUSH_TIME"); };
mqttVars["brewPidDelay"] = [] { return &editableVars.at("PID_BD_DELAY"); };
mqttVars["pidUseBD"] = [] { return &editableVars.at("PID_BD_ON"); };
mqttVars["aggbKp"] = [] { return &editableVars.at("PID_BD_KP"); };
mqttVars["aggbTn"] = [] { return &editableVars.at("PID_BD_TN"); };
mqttVars["aggbTv"] = [] { return &editableVars.at("PID_BD_TV"); };

// Values reported to MQTT
mqttSensors["temperature"] = [] { return temperature; };
Expand All @@ -1379,17 +1362,27 @@ void setup() {
mqttSensors["currentKi"] = [] { return bPID.GetKi(); };
mqttSensors["currentKd"] = [] { return bPID.GetKd(); };
mqttSensors["machineState"] = [] { return machineState; };

#if FEATURE_BREWSWITCH == 1
mqttSensors["timeBrewed"] = [] { return timeBrewed / 1000; };
#endif
#if FEATURE_PRESSURESENSOR == 1
mqttSensors["pressure"] = [] { return inputPressureFilter; };
#endif

#if FEATURE_SCALE == 1
mqttVars["weightSetpoint"] = [] { return &editableVars.at("SCALE_WEIGHTSETPOINT"); };
mqttVars["scaleCalibration"] = [] { return &editableVars.at("SCALE_CALIBRATION"); };
#if SCALE_TYPE == 0
mqttVars["scale2Calibration"] = [] { return &editableVars.at("SCALE2_CALIBRATION"); };
#endif
mqttVars["scaleKnownWeight"] = [] { return &editableVars.at("SCALE_KNOWN_WEIGHT"); };
mqttVars["scaleTareOn"] = [] { return &editableVars.at("TARE_ON"); };
mqttVars["scaleCalibrationOn"] = [] { return &editableVars.at("CALIBRATION_ON"); };

mqttSensors["currentWeight"] = [] { return weight; };
#endif

#if FEATURE_PRESSURESENSOR == 1
mqttSensors["pressure"] = [] { return inputPressureFilter; };
#endif
initTimer1();

storageSetup();
Expand Down
101 changes: 44 additions & 57 deletions src/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,21 @@ void assignMQTTParam(char* param, double value) {
writeSysParamsToStorage();
break;

case kInteger:
*(int*)var->ptr = static_cast<int>(value);
mqtt_publish(param, number2string(static_cast<int>(value)), true);
break;

default:
LOGF(WARNING, "%s is not a recognized type for this MQTT parameter.", var->type);
break;
}
}
else {
LOGF(WARNING, "Value out of range for MQTT parameter %s", param);
LOGF(WARNING, "%s is not a valid MQTT parameter.", param);
}
} catch (const std::out_of_range& e) {
LOGF(WARNING, "%s is not a valid MQTT parameter.", param);
LOGF(WARNING, "Value out of range for MQTT parameter %s", param);
}
}

Expand Down Expand Up @@ -504,88 +509,70 @@ DiscoveryObject GenerateNumberDevice(String name, String displayName, int min_va
* @return 0 if successful, MQTT connection error code if failed to send messages
*/
int sendHASSIODiscoveryMsg() {
// Number Devices
// Sensor, number and switch objects which will always be published

DiscoveryObject machineStateDevice = GenerateSensorDevice("machineState", "Machine State", "", "enum");
DiscoveryObject actual_temperature = GenerateSensorDevice("temperature", "Boiler Temperature", "°C", "temperature");
DiscoveryObject heaterPower = GenerateSensorDevice("heaterPower", "Heater Power", "%", "power_factor");

DiscoveryObject brewSetpoint = GenerateNumberDevice("brewSetpoint", "Brew setpoint", BREW_SETPOINT_MIN, BREW_SETPOINT_MAX, 0.1, "°C");
DiscoveryObject steamSetPoint = GenerateNumberDevice("steamSetpoint", "Steam setpoint", STEAM_SETPOINT_MIN, STEAM_SETPOINT_MAX, 0.1, "°C");
DiscoveryObject steamSetpoint = GenerateNumberDevice("steamSetpoint", "Steam setpoint", STEAM_SETPOINT_MIN, STEAM_SETPOINT_MAX, 0.1, "°C");
DiscoveryObject brewTempOffset = GenerateNumberDevice("brewTempOffset", "Brew Temp. Offset", BREW_TEMP_OFFSET_MIN, BREW_TEMP_OFFSET_MAX, 0.1, "°C");
DiscoveryObject brewPidDelay = GenerateNumberDevice("brewPidDelay", "Brew Pid Delay", BREW_PID_DELAY_MIN, BREW_PID_DELAY_MAX, 0.1, "");
DiscoveryObject startKp = GenerateNumberDevice("startKp", "Start kP", PID_KP_START_MIN, PID_KP_START_MAX, 0.1, "");
DiscoveryObject startTn = GenerateNumberDevice("startTn", "Start Tn", PID_TN_START_MIN, PID_TN_START_MAX, 0.1, "");
DiscoveryObject steamKp = GenerateNumberDevice("steamKp", "Start Kp", PID_KP_STEAM_MIN, PID_KP_STEAM_MAX, 0.1, "");
DiscoveryObject aggKp = GenerateNumberDevice("aggKp", "aggKp", PID_KP_REGULAR_MIN, PID_KP_REGULAR_MAX, 0.1, "");
DiscoveryObject aggTn = GenerateNumberDevice("aggTn", "aggTn", PID_TN_REGULAR_MIN, PID_TN_REGULAR_MAX, 0.1, "");
DiscoveryObject aggTv = GenerateNumberDevice("aggTv", "aggTv", PID_TV_REGULAR_MIN, PID_TV_REGULAR_MAX, 0.1, "");
DiscoveryObject aggIMax = GenerateNumberDevice("aggIMax", "aggIMax", PID_I_MAX_REGULAR_MIN, PID_I_MAX_REGULAR_MAX, 0.1, "");

#if FEATURE_BREWCONTROL == 1
DiscoveryObject brewtime = GenerateNumberDevice("brewtime", "Brew time", BREW_TIME_MIN, BREW_TIME_MAX, 0.1, "s");
DiscoveryObject brewTime = GenerateNumberDevice("brewTime", "Brew time", BREW_TIME_MIN, BREW_TIME_MAX, 0.1, "s");
DiscoveryObject preinfusion = GenerateNumberDevice("preinfusion", "Preinfusion filling time", PRE_INFUSION_TIME_MIN, PRE_INFUSION_TIME_MAX, 0.1, "s");
DiscoveryObject preinfusionPause = GenerateNumberDevice("preinfusionPause", "Preinfusion pause time", PRE_INFUSION_PAUSE_MIN, PRE_INFUSION_PAUSE_MAX, 0.1, "s");
DiscoveryObject backflushCycles = GenerateNumberDevice("backflushCycles", "Backflush Cycles", BACKFLUSH_CYCLES_MIN, BACKFLUSH_CYCLES_MAX, 1, "");
DiscoveryObject backflushFillTime = GenerateNumberDevice("backflushFillTime", "Backflush filling time", BACKFLUSH_FILL_TIME_MIN, BACKFLUSH_FILL_TIME_MAX, 0.1, "s");
DiscoveryObject backflushFlushTime = GenerateNumberDevice("backflushFlushTime", "Backflush flushing time", BACKFLUSH_FLUSH_TIME_MIN, BACKFLUSH_FLUSH_TIME_MAX, 0.1, "s");

#endif

// Sensor Devices
DiscoveryObject actual_temperature = GenerateSensorDevice("temperature", "Boiler Temperature", "°C", "temperature");
DiscoveryObject heaterPower = GenerateSensorDevice("heaterPower", "Heater Power", "%", "power_factor");
DiscoveryObject machineStateDevice = GenerateSensorDevice("machineState", "Machine State", "", "enum");
DiscoveryObject currentWeight = GenerateSensorDevice("currentWeight", "Weight", "g", "weight");

#if FEATURE_PRESSURESENSOR == 1
DiscoveryObject pressure = GenerateSensorDevice("pressure", "Pressure", "bar", "pressure");
#endif

// Switch Devices
DiscoveryObject pidOn = GenerateSwitchDevice("pidON", "Use PID");
DiscoveryObject steamON = GenerateSwitchDevice("steamON", "Steam");
#if FEATURE_BREWCONTROL == 1
DiscoveryObject backflushOn = GenerateSwitchDevice("backflushOn", "Backflush");
#endif
DiscoveryObject startUsePonM = GenerateSwitchDevice("startUsePonM", "Use PonM");
DiscoveryObject backflushOn = GenerateSwitchDevice("backflushOn", "Backflush");

// Button Devices
DiscoveryObject scaleCalibrateButton = GenerateButtonDevice("scaleCalibrationOn", "Calibrate Scale");
DiscoveryObject scaleTareButton = GenerateButtonDevice("scaleTareOn", "Tare Scale");
// List of all DiscoveryObjects, will be always published
std::vector<DiscoveryObject> discoveryObjects = {
machineStateDevice, actual_temperature, heaterPower, brewSetpoint, steamSetpoint, brewTempOffset, startKp, startTn, steamKp, aggKp, aggTn, aggTv, aggIMax,
brewTime, preinfusion, preinfusionPause, backflushCycles, backflushFillTime, backflushFlushTime, pidOn, steamON, startUsePonM, backflushOn};

std::vector<DiscoveryObject> discoveryObjects = {brewSetpoint,
steamSetPoint,
brewTempOffset,
brewPidDelay,
startKp,
startTn,
steamKp,
aggKp,
aggTn,
aggTv,
aggIMax,
actual_temperature,
heaterPower,
machineStateDevice,
#if FEATURE_BREWCONTROL == 1
brewtime,
preinfusion,
preinfusionPause,
backflushOn,
backflushCycles,
backflushFillTime,
backflushFlushTime,
#endif
pidOn,
steamON,
startUsePonM
// Sensor, number and switch object which will be published based on feature set
#if FEATURE_BREWSWITCH == 1

#if FEATURE_PRESSURESENSOR == 1
,
pressure
DiscoveryObject timeBrewed = GenerateSensorDevice("timeBrewed", "Time brewed", "s", "duration");

DiscoveryObject brewPidDelay = GenerateNumberDevice("brewPidDelay", "Brew Pid Delay", BREW_PID_DELAY_MIN, BREW_PID_DELAY_MAX, 0.1, "s");

discoveryObjects.push_back(timeBrewed);
discoveryObjects.push_back(brewPidDelay);
#endif
};

#if FEATURE_SCALE == 1

DiscoveryObject currentWeight = GenerateSensorDevice("currentWeight", "Weight", "g", "weight");

DiscoveryObject scaleCalibrateButton = GenerateButtonDevice("scaleCalibrationOn", "Calibrate Scale");
DiscoveryObject scaleTareButton = GenerateButtonDevice("scaleTareOn", "Tare Scale");

DiscoveryObject weightSetpoint = GenerateNumberDevice("weightSetpoint", "Weight setpoint", WEIGHTSETPOINT_MIN, WEIGHTSETPOINT_MAX, 0.1, "g");

discoveryObjects.push_back(currentWeight);
discoveryObjects.push_back(scaleCalibrateButton);
discoveryObjects.push_back(scaleTareButton);
discoveryObjects.push_back(weightSetpoint);
#endif

#if FEATURE_PRESSURESENSOR == 1

DiscoveryObject pressure = GenerateSensorDevice("pressure", "Pressure", "bar", "pressure");

discoveryObjects.push_back(pressure);
#endif

// Send the Objects to Hassio
Expand Down
2 changes: 1 addition & 1 deletion src/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ typedef struct __attribute__((packed)) {
int backflushCycles;
double backflushFillTimeMs;
double backflushFlushTimeMs;
bool featureBrewControl;
uint8_t featureBrewControl;

} sto_data_t;

Expand Down

0 comments on commit 4af1bf3

Please sign in to comment.