Skip to content
Open
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
28 changes: 26 additions & 2 deletions src/esp8266-midea-dehumidifier/esp8266-midea-dehumidifier.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ char MQTT_TOPIC_AUTOCONF_HUMIDITY_SENSOR[128];

char MQTT_TOPIC_AUTOCONF_FAN[128];
char MQTT_TOPIC_AUTOCONF_HUMIDIFIER[128];
char MQTT_TOPIC_AUTOCONF_IONISER[128];



Expand All @@ -65,6 +66,7 @@ void setup() {
state.mode = setpoint;
state.humiditySetpoint = 55;
state.currentHumidity = 45;
state.ion = false;
state.errorCode = 0;

delay(3000);
Expand All @@ -81,7 +83,7 @@ void setup() {

snprintf(MQTT_TOPIC_AUTOCONF_FAN, 127, "homeassistant/fan/%s/%s_fan/config", FIRMWARE_PREFIX, identifier);
snprintf(MQTT_TOPIC_AUTOCONF_HUMIDIFIER, 127, "homeassistant/humidifier/%s/%s_dehumidifier/config", FIRMWARE_PREFIX, identifier);

snprintf(MQTT_TOPIC_AUTOCONF_IONISER, 127, "homeassistant/switch/%s/%s_ioniser/config", FIRMWARE_PREFIX, identifier);

WiFi.hostname(identifier);

Expand Down Expand Up @@ -210,6 +212,7 @@ void publishState() {
stateJson["state"] = state.powerOn ? "on" : "off";
stateJson["humiditySetpoint"] = state.humiditySetpoint;
stateJson["humidityCurrent"] = state.currentHumidity;
stateJson["ion"] = state.ion ? "on" : "off";
stateJson["errorCode"] = state.errorCode;

switch (state.fanSpeed) {
Expand Down Expand Up @@ -259,7 +262,8 @@ void mqttCallback(char* topic, byte* payload, unsigned int length) {
commandJson["state"].as<String>(),
commandJson["mode"].as<String>(),
commandJson["fanSpeed"].as<String>(),
commandJson["humiditySetpoint"].as<byte>()
commandJson["humiditySetpoint"].as<byte>(),
commandJson["ion"].as<String>()
);

getStatus();
Expand Down Expand Up @@ -383,4 +387,24 @@ void publishAutoConfig() {
mqttClient.publish(MQTT_TOPIC_AUTOCONF_HUMIDIFIER, mqttPayload, true);

autoconfPayload.clear();

//Ioniser
autoconfPayload["device"] = device.as<JsonObject>();
autoconfPayload["availability_topic"] = MQTT_TOPIC_AVAILABILITY;
autoconfPayload["name"] = identifier + String(" Ioniser");
autoconfPayload["unique_id"] = identifier + String("_ioniser");

autoconfPayload["state_topic"] = MQTT_TOPIC_STATE;
autoconfPayload["command_topic"] = MQTT_TOPIC_COMMAND;

autoconfPayload["value_template"] = "{{value_json.ion}}";
autoconfPayload["payload_on"] = "{\"ion\": \"on\"}";
autoconfPayload["payload_off"] = "{\"ion\": \"off\"}";
autoconfPayload["state_on"] = "on";
autoconfPayload["state_off"] = "off";

serializeJson(autoconfPayload, mqttPayload);
mqttClient.publish(MQTT_TOPIC_AUTOCONF_IONISER, mqttPayload, true);

autoconfPayload.clear();
}
22 changes: 17 additions & 5 deletions src/esp8266-midea-dehumidifier/serialCommunication.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ void parseState() {
state.powerOn = serialRxBuf[11] & 0x01 > 0;
state.mode = (dehumMode_t)(serialRxBuf[12] & 0x0f);
state.fanSpeed = (fanSpeed_t)(serialRxBuf[13] & 0x7f);

state.humiditySetpoint = serialRxBuf[17] >= 100 ? 99 : serialRxBuf[17];
state.currentHumidity = serialRxBuf[26];
state.ion = serialRxBuf[19] == 0x40;
state.errorCode = serialRxBuf[31];

clearRxBuf();
Expand Down Expand Up @@ -68,13 +68,14 @@ void writeHeader(byte msgType, byte agreementVersion, byte packetLength) {
currentHeader[9] = msgType;
}

void handleStateUpdateRequest(String requestedState, String mode, String fanSpeed, byte humiditySetpoint) {
void handleStateUpdateRequest(String requestedState, String mode, String fanSpeed, byte humiditySetpoint, String ion) {
dehumidifierState_t newState;

newState.powerOn = state.powerOn;
newState.mode = state.mode;
newState.fanSpeed = state.fanSpeed;
newState.humiditySetpoint = state.humiditySetpoint;
newState.ion = state.ion;

if (requestedState == "on") {
newState.powerOn = true;
Expand Down Expand Up @@ -104,17 +105,26 @@ void handleStateUpdateRequest(String requestedState, String mode, String fanSpee
newState.humiditySetpoint = humiditySetpoint;
}

if (ion == "on"){
newState.ion = true;
}
if (ion == "off"){
newState.ion = false;
}

if ( //Only send if we have updates
newState.powerOn != state.powerOn ||
newState.mode != state.mode ||
newState.fanSpeed != state.fanSpeed ||
newState.humiditySetpoint != state.humiditySetpoint
newState.humiditySetpoint != state.humiditySetpoint ||
newState.ion != state.ion
) {
updateSetStatus(
newState.powerOn,
newState.mode,
newState.fanSpeed,
newState.humiditySetpoint
newState.humiditySetpoint,
newState.ion
);
sendSetStatus();

Expand All @@ -124,6 +134,7 @@ void handleStateUpdateRequest(String requestedState, String mode, String fanSpee
state.mode = newState.mode;
state.fanSpeed = newState.fanSpeed;
state.humiditySetpoint = newState.humiditySetpoint;
state.ion = newState.ion;
delay(30);
}
}
Expand All @@ -132,14 +143,15 @@ void sendSetStatus() {
sendMessage(0x02, 0x03, 25, setStatusCommand);
}

void updateSetStatus(boolean powerOn, dehumMode_t dehumMode, fanSpeed_t fanSpeed, byte humiditySetpoint) {
void updateSetStatus(boolean powerOn, dehumMode_t dehumMode, fanSpeed_t fanSpeed, byte humiditySetpoint, boolean ion) {
memset(setStatusCommand, 0, sizeof(setStatusCommand));

setStatusCommand[0] = 0x48; //Magic
setStatusCommand[1] = powerOn ? 0x01 : 0x00;
setStatusCommand[2] = (byte)(dehumMode & 0x0f);
setStatusCommand[3] = (byte)fanSpeed;
setStatusCommand[7] = humiditySetpoint;
setStatusCommand[9] = ion ? 0x40 : 0x00;
}

void updateAndSendNetworkStatus(boolean isConnected) {
Expand Down
1 change: 1 addition & 0 deletions src/esp8266-midea-dehumidifier/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ struct dehumidifierState_t {
fanSpeed_t fanSpeed;
byte humiditySetpoint;
byte currentHumidity;
boolean ion;
byte errorCode;
};