From c08a762c0008a1479abf1b7c8f6b515ed90c7506 Mon Sep 17 00:00:00 2001 From: reaw Date: Tue, 13 Feb 2024 07:36:31 +0700 Subject: [PATCH] only fan speed not working --- ise-display/ise-display-prod.HMI | Bin 37446253 -> 37446253 bytes platformio.ini | 2 +- src/ise_display.cpp | 56 ++++++++++-------- src/ise_display.hpp | 4 +- src/main.cpp | 94 +++++++++++++++++++++++-------- src/main.hpp | 2 + 6 files changed, 106 insertions(+), 52 deletions(-) diff --git a/ise-display/ise-display-prod.HMI b/ise-display/ise-display-prod.HMI index c7b0d8c691b9188fce140c1de61a1087d7918c7c..786086ce406c394e4460d4daebfc746f5bb953ae 100644 GIT binary patch delta 2865 zcmdVa_jgZs9LMqR_nT!T_Wn}TuDyw>Srt3AtN7Ygn^+|wyou2o{gSVkL0h9mG&Gbp zR6MrYh&N_09?|zqI6l#44_G zE@yIL5C4F`ypD2%yE*=G9K90hSWv{_aC)ONw69+2U%Lr8qa1PCb?lCHI8tIAyLG!9 zzGp7Zh1HC51ZvCaa3ns+kk{#~=nV|6RUUdnbmw zW5V4|~yCXe^J$@@OoN#`0(^k2@&-@_*ri zM2|aliq){17HYNZU8`;HSsklu^{l=%u!i=&HL?$^v4vR^YiiA`xwWvC*2==|Lu+kq ztgW@P_SV5V+DF#OKDN%*#k!iux>%Q`6Pw>I*eP%~Z40Z1tt`syS+|id8-}PsOSE zYJpm)7OBN*iHcWC)iSkQeWkuu->4O8rCOy{t8dkJYK>Z})~N)wUVX0;)dsau{h*T6 zCbe1psJ5uB>L>NHN>VP_^4yixXVRc0P zsg9~+>bN?gPO4Mtv^t~Cs&neRx}YwqOX{+^qOPiI>bkn2ZmL`Awn|lZ)LoUP?y39g zfqJMOsmJPxda9nO=jw%ess2*0)a&VCHJvjRxWEtmApim)2+~1%$N(837(yTuWQHt| z6|zBg$N@Pa7vzRKkQefS8}dT|cnbM4;`Q*d<31~W9STBpeuNw8+3;r&=Yz=Z|DP`KwtP2`aypf00UtV z42B^v6ox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`zJYJy2z&=e;d?j+KfsUh z6Z{N$kPpY<1e}CZa2n3QFHity;a4~Z=ivfegiCN4uE15e2G`*?_#OU$8*meDx%B#T zYjA-Z{JOIKL1`!hW#MkP2ZEs-l!pos0{4OkDnccw3{@Z$ zszNoW4mF@A+z0nVEvO9-KphB!2O%8lLOqCpNT?4D;2~%TjUWnc-<&4yEWQ5Rm23YA D2QJFo diff --git a/platformio.ini b/platformio.ini index c0e538c..6740886 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,7 +12,7 @@ platform = espressif32 board = wt32-eth01 framework = arduino -lib_deps = siwats/ESPMegaPROR3@^2.2.5 +lib_deps = siwats/ESPMegaPROR3@2.2.5 monitor_speed = 115200 build_flags = -DCORE_DEBUG_LEVEL=5 upload_port = COM28 diff --git a/src/ise_display.cpp b/src/ise_display.cpp index 70aad6e..6b77596 100644 --- a/src/ise_display.cpp +++ b/src/ise_display.cpp @@ -6,11 +6,13 @@ ISEDisplay::ISEDisplay(HardwareSerial *adapter) : ESPMegaDisplay(adapter, 115200 // TODO : Implement // debug to work -void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCard, ClimateCard *climateCard) +void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCard, ClimateCard *climateCard, RemoteVariable* pm_switch, RemoteVariable* pm_fan_speed) { this->inputCard = inputCard; this->outputCard = outputCard; this->climateCard = climateCard; + this->pm_switch = pm_switch; + this->remote_pm_fan_speed = pm_fan_speed; auto bindedHandlePWMChange = std::bind(&ISEDisplay::handlePWMChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); auto bindedHandleACChange = std::bind(&ISEDisplay::handleACChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); auto bindedHandleTouch = std::bind(&ISEDisplay::handleTouch, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); @@ -18,6 +20,7 @@ void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCar this->climateCallbackHandle = this->climateCard->registerChangeCallback(bindedHandleACChange); this->user_mode = 1; // initialized to cool by default this->pm_fan_speed = 10; + //remote_pm_fan_speed->setValue(pm_fan_speed); this->ac_fan_speed = 0; this->ac_mode = 0; this->ac_temperature = 25; @@ -36,8 +39,7 @@ void ISEDisplay::begin(DigitalInputCard *inputCard, DigitalOutputCard *outputCar this->updateAirPurifierState(); this->updateACState(); this->updateLightGroupStatePageDashboard(); - this->outputCard->setValue(6, pm_fan_speed); - this->outputCard->setValue(5, 0); + this->outputCard->setValue(1, 0); this->outputCard->setValue(2, 0); this->outputCard->setValue(3, 0); @@ -70,6 +72,7 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type { ESP_LOGD("ISEDisplay", "Touch detected on page %d, component %d, touch type %d", page, component, touch_type); // time_since_last_screen_update = millis(); // update time since last activity + char buffer[4]; if (page == PAGE_STANDBY) { switch (component) @@ -232,22 +235,24 @@ void ISEDisplay::handleTouch(uint8_t page, uint8_t component, uint8_t touch_type case COMPONENT_PM_FAN_SPEED_DECREASE: if (touch_type != TOUCH_TYPE_RELEASE) break; - pm_fan_speed = this->outputCard->getValue(6); + pm_fan_speed = (int) atof(remote_pm_fan_speed->getValue()); ESP_LOGI("ISEDisplay", "Current PM fan speed: %d", pm_fan_speed); if (pm_fan_speed >= 1 && pm_fan_speed <= 20) - this->outputCard->setValue(6, (pm_fan_speed - 1)); + itoa(pm_fan_speed - 1, buffer, DEC); + remote_pm_fan_speed->setValue(buffer); ESP_LOGI("ISEDisplay", "New PM fan speed: %d", pm_fan_speed); - updateAirPurifierState(); + //updateAirPurifierState(); break; case COMPONENT_PM_FAN_SPEED_INCREASE: if (touch_type != TOUCH_TYPE_RELEASE) break; - pm_fan_speed = this->outputCard->getValue(6); + pm_fan_speed = (int) atof(remote_pm_fan_speed->getValue()); ESP_LOGI("ISEDisplay", "Current PM fan speed: %d", pm_fan_speed); if (pm_fan_speed >= 0 && pm_fan_speed <= 19) - this->outputCard->setValue(6, (pm_fan_speed + 1)); + itoa(pm_fan_speed + 1, buffer, DEC); + remote_pm_fan_speed->setValue(buffer); ESP_LOGI("ISEDisplay", "New PM fan speed: %d", pm_fan_speed); - updateAirPurifierState(); + //updateAirPurifierState(); break; default: break; @@ -273,7 +278,7 @@ void ISEDisplay::handlePWMChange(uint8_t pin, bool state, uint16_t value) updateLightGroupStatePageStandby(); // time_since_last_screen_update = millis(); // update time since last activity } - else if (pin == 4 || pin == 5) + else if (pin == 5 || pin == 6) { // Air Purifier updateAirPurifierStateStandby(); @@ -288,7 +293,7 @@ void ISEDisplay::handlePWMChange(uint8_t pin, bool state, uint16_t value) updateLightGroupStatePageDashboard(); // time_since_last_screen_update = millis(); // update time since last activity } - else if (pin == 4 || pin == 5) + else if (pin == 5 || pin == 6) { // Air Purifier updateAirPurifierState(); @@ -438,7 +443,11 @@ void ISEDisplay::updatePMinside(u_int16_t pm25_inside) void ISEDisplay::setPMstate(bool is_pm_on, uint8_t pm_fan_speed) { - // TODO : set data to HA's Xiaomi air purifier sensor + ESP_LOGI("ISEDisplay", "Setting PM state: %d, fan speed: %d", is_pm_on, pm_fan_speed); + char buffer[4]; + itoa(pm_fan_speed, buffer, DEC); + remote_pm_fan_speed->setValue(buffer); + pm_switch->setValue(is_pm_on ? "1" : "0"); } void ISEDisplay::setACstate(uint8_t ac_fan_speed, uint8_t ac_mode, uint8_t ac_temperature) @@ -479,26 +488,22 @@ void ISEDisplay::toggleLightGroupStateStandby() void ISEDisplay::togglePM() { // Get the current group state - bool state = this->outputCard->getState(5); + bool state = strcmp(pm_switch->getValue(), "on") == 0; ESP_LOGI("ISEDisplay", "Current PM state: %d", state); // Toggle the state - state = !state; - // Set the state - this->outputCard->setState(5, state); + pm_switch->setValue(state ? "off" : "on"); ESP_LOGI("ISEDisplay", "New PM state: %d", state); - updateAirPurifierState(); + //updateAirPurifierState(); } void ISEDisplay::togglePMStandby() { // Get the current group state - bool state = this->outputCard->getState(5); + bool state = strcmp(pm_switch->getValue(), "on") == 0; ESP_LOGI("ISEDisplay", "Current PM state: %d", state); // Toggle the state - state = !state; - // Set the state - this->outputCard->setState(5, state); + pm_switch->setValue(state ? "off" : "on"); ESP_LOGI("ISEDisplay", "New PM state: %d", state); - updateAirPurifierStateStandby(); + //updateAirPurifierStateStandby(); } void ISEDisplay::toggleAC() { @@ -690,7 +695,7 @@ void ISEDisplay::toggleSliderLight(uint8_t row, uint8_t lightLevel) void ISEDisplay::updateAirPurifierStateStandby() { // Get the state - bool state = this->outputCard->getState(5); + bool state = strcmp(pm_switch->getValue(), "on") == 0; // Send the state to the display this->takeSerialMutex(); @@ -708,8 +713,9 @@ void ISEDisplay::updateAirPurifierStateStandby() void ISEDisplay::updateAirPurifierState() { // Get the state - bool state = this->outputCard->getState(5); - pm_fan_speed = this->outputCard->getValue(6); + bool state = strcmp(pm_switch->getValue(), "on") == 0; + ESP_LOGI("ISEDisplay", "Updating air purifier state to: %d", state); + pm_fan_speed = (int) atof(remote_pm_fan_speed->getValue()); // Send the state to the display this->takeSerialMutex(); diff --git a/src/ise_display.hpp b/src/ise_display.hpp index c830090..1dc9bfd 100644 --- a/src/ise_display.hpp +++ b/src/ise_display.hpp @@ -20,7 +20,7 @@ class ISEDisplay : public ESPMegaDisplay { public: ISEDisplay(HardwareSerial* adapter); - void begin(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, ClimateCard* climateCard); + void begin(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, ClimateCard* climateCard, RemoteVariable* pm_switch, RemoteVariable* pm_fan_speed); void updateLightGroupStatePageDashboard(); void updateLightGroupStatePageStandby(); void updateAirPurifierState(); @@ -46,6 +46,8 @@ class ISEDisplay : public ESPMegaDisplay { DigitalInputCard* inputCard; DigitalOutputCard *outputCard; ClimateCard *climateCard; + RemoteVariable *pm_switch; + RemoteVariable *remote_pm_fan_speed; uint8_t outputCallbackHandle; uint8_t climateCallbackHandle; uint8_t user_mode; diff --git a/src/main.cpp b/src/main.cpp index 545bfe1..6370dac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,11 @@ #include +RemoteVariable pm25_in = RemoteVariable(); RemoteVariable pm25_out = RemoteVariable(); RemoteVariable temp_out = RemoteVariable(); RemoteVariable weather = RemoteVariable(); -RemoteVariable pm25_in = RemoteVariable(); +RemoteVariable pm_switch = RemoteVariable(); +RemoteVariable pm_fan_speed = RemoteVariable(); const char *mode_names[] = {"off", "fan_only", "cool"}; const char *fan_speed_names[] = {"auto", "high", "medium", "low"}; @@ -98,13 +100,6 @@ void setup() climateCard.loadStateFromFRAM(); climateCard.setFRAMAutoSave(true); espmega.display->bindClimateCard(&climateCard); - espmega.iot->registerCard(0); // Register the Input Card - espmega.iot->registerCard(1); // Register the Output Card - espmega.iot->registerCard(2); // Register the Climate Card - auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, &espmega); - iseDisplay.begin(&espmega.inputs, &espmega.outputs, &climateCard); - espmega.iot->registerRelativeMqttCallback(&handleMqttMessage); - iseDisplay.registerPageChangeCallback(&handlePageChange); // placeholder // PM2.5 PPM Remote Variable // 12 bytes remote variable, 11 characters + null terminator @@ -116,6 +111,21 @@ void setup() weather.begin(45, "/weather/value", espmega.iot, true, "/weather/request_value"); // PM2.5 PPM Remote Variable pm25_in.begin(6, "/pm/value", espmega.iot, true, "/pm/request_value"); + // Air Purifier Switch Remote Variable + pm_switch.begin(6, "/pm/switch_state", espmega.iot, true, "/pm/request_switch_state"); + pm_switch.enableSetValue("/pm/set_switch_state"); + // Air Purifier Fan Speed Remote Variable + pm_fan_speed.begin(6, "/pm/fan_speed", espmega.iot, true, "/pm/request_fan_speed"); + pm_fan_speed.enableSetValue("/pm/set_fan_speed"); + espmega.iot->registerCard(0); // Register the Input Card + espmega.iot->registerCard(1); // Register the Output Card + espmega.iot->registerCard(2); // Register the Climate Card + auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, &espmega); + iseDisplay.begin(&espmega.inputs, &espmega.outputs, &climateCard, &pm_switch, &pm_fan_speed); + espmega.iot->registerRelativeMqttCallback(&handleMqttMessage); + iseDisplay.registerPageChangeCallback(&handlePageChange); + + } void loop() @@ -145,12 +155,12 @@ void loop() iseDisplay.updateDateTimeText(time); last_time_updated = millis(); } - //Update the PM2.5 PPM value every 15 seconds + // Update the PM2.5 PPM value every 15 seconds static uint32_t last_pm25_out_update = 0; if (millis() - last_pm25_out_update > 15000) { uint16_t pm25_out_value = get_pm25_out(); - //ESP_LOGI("loopPM2.5","updating PM2.5 from MQTT inside loop: %d", pm25_out_value); + // ESP_LOGI("loopPM2.5","updating PM2.5 from MQTT inside loop: %d", pm25_out_value); iseDisplay.updatePMoutside(pm25_out_value); last_pm25_out_update = millis(); } @@ -178,11 +188,16 @@ void loop() iseDisplay.updateWeather(weather_value); last_weather_update = millis(); } + // Update the PM state every 1 seconds + static uint32_t last_pm_switch_update = 0; + if (millis() - last_pm_switch_update > 5000) + { + iseDisplay.updateAirPurifierState(); + } } void on_pin_change(uint8_t pin, uint8_t value) -{ -} +{} uint16_t get_pm25_out() { @@ -211,6 +226,35 @@ float get_temp_out() return temp_out_value; } +uint8_t get_pm_fanspeed() +{ + uint8_t pm_fan_speed_value = 0; + // Read PM2.5 fan speed from sensor + pm_fan_speed_value = (int) atof(pm_fan_speed.getValue()); + ESP_LOGI("PM fan speed", "getting PM2.5 PPM from MQTT: %d", pm_fan_speed_value); + return pm_fan_speed_value; +} + +bool get_pm_switch() +{ + ESP_LOGI("PM switch", "getting PM switch state from MQTT: %d", pm_switch.getValue()); + bool is_pm_switch_on = strcmp(pm_switch.getValue(),"on")== 0; + return is_pm_switch_on; +} +void toggle_pm_switch() +{ + bool is_pm_switch_on = get_pm_switch(); + ESP_LOGI("PM switch", "toggling PM switch state from: %d to %d", is_pm_switch_on, !is_pm_switch_on); + pm_switch.setValue(is_pm_switch_on ? "0" : "1"); +} +void set_pm_fanspeed(uint8_t speed) +{ + ESP_LOGI("PM fan speed", "setting PM fan speed to: %d", speed); + char buffer[4]; + itoa(speed, buffer, DEC); + pm_fan_speed.setValue(buffer); +} + void handlePageChange(uint8_t page) { @@ -233,16 +277,16 @@ void handlePageChange(uint8_t page) break; } - // iseDisplay.updatePMinside(); - iseDisplay.updatePMoutside(get_pm25_out()); - iseDisplay.updatePMinside(get_pm25_in()); - iseDisplay.updateWeather(weather.getValue()); - iseDisplay.updateTempOutside(get_temp_out()); - - /* iseDisplay.updateACState(); - iseDisplay.updateAirPurifierState(); - iseDisplay.updateLightGroupStatePageStandby(); - iseDisplay.updateLightGroupStatePageDashboard(); - iseDisplay.updateuserACmode(); - iseDisplay.updateAirPurifierStateStandby(); */ - } \ No newline at end of file + // iseDisplay.updatePMinside(); + iseDisplay.updatePMoutside(get_pm25_out()); + iseDisplay.updatePMinside(get_pm25_in()); + iseDisplay.updateWeather(weather.getValue()); + iseDisplay.updateTempOutside(get_temp_out()); + + /* iseDisplay.updateACState(); + iseDisplay.updateAirPurifierState(); + iseDisplay.updateLightGroupStatePageStandby(); + iseDisplay.updateLightGroupStatePageDashboard(); + iseDisplay.updateuserACmode(); + iseDisplay.updateAirPurifierStateStandby(); */ +} \ No newline at end of file diff --git a/src/main.hpp b/src/main.hpp index 73b888c..e98301f 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -37,3 +37,5 @@ void on_pin_change(uint8_t pin, uint8_t value); uint16_t get_pm25_out(); uint16_t get_pm25_in(); float get_temp_out(); +void toggle_pm_switch(); +void set_pm_fanspeed(uint8_t speed); \ No newline at end of file