only fan speed not working

This commit is contained in:
reaw 2024-02-13 07:36:31 +07:00
parent 34eeca14e0
commit c08a762c00
6 changed files with 106 additions and 52 deletions

Binary file not shown.

View file

@ -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

View file

@ -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();

View file

@ -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;

View file

@ -1,9 +1,11 @@
#include <main.hpp>
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(); */
}
// 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(); */
}

View file

@ -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);