slightly cleanup code

This commit is contained in:
Siwat Sirichai 2024-03-16 22:55:25 +07:00
parent 1a9cbd34db
commit e5b0ac9f04
2 changed files with 152 additions and 205 deletions

View file

@ -1,7 +1,22 @@
#include <main.hpp> #include <main.hpp>
bool analogCardAvailable = false;
/***********************************************
* Begin Configuration *
***********************************************/
// Analog Card & Current Transformer Configuration
bool analogCardAvailable = false;
AnalogCard analogCard = AnalogCard();
float voltage = CT_RMS_VOLTAGE;
CurrentTransformerCard ct_light_phase1 = CurrentTransformerCard(&analogCard, CT_PIN_AC_PHASE1, &voltage, &adcToCurrent, 1000);
CurrentTransformerCard ct_light_phase2 = CurrentTransformerCard(&analogCard, CT_PIN_AC_PHASE2, &voltage, &adcToCurrent, 1000);
CurrentTransformerCard ct_socket = CurrentTransformerCard(&analogCard, CT_PIN_SOCKET, &voltage, &adcToCurrent, 1000);
CurrentTransformerCard ct_ac_phase1 = CurrentTransformerCard(&analogCard, CT_PIN_AC_PHASE1, &voltage, &adcToCurrent, 1000);
CurrentTransformerCard ct_ac_phase2 = CurrentTransformerCard(&analogCard, CT_PIN_AC_PHASE2, &voltage, &adcToCurrent, 1000);
CurrentTransformerCard ct_ac_phase3 = CurrentTransformerCard(&analogCard, CT_PIN_AC_PHASE3, &voltage, &adcToCurrent, 1000);
// Remote Variables
RemoteVariable pm25_in = RemoteVariable(); RemoteVariable pm25_in = RemoteVariable();
RemoteVariable pm25_out = RemoteVariable(); RemoteVariable pm25_out = RemoteVariable();
RemoteVariable temp_out = RemoteVariable(); RemoteVariable temp_out = RemoteVariable();
@ -9,9 +24,7 @@ RemoteVariable weather = RemoteVariable();
RemoteVariable pm_switch = RemoteVariable(); RemoteVariable pm_switch = RemoteVariable();
RemoteVariable pm_fan_speed = RemoteVariable(); RemoteVariable pm_fan_speed = RemoteVariable();
const char *mode_names_daikin[] = {"off", "cool", "fan_only", "dry"}; // Light Configuration
const char *mode_names_york[] = {"off", "cool", "fan_only"};
const char *fan_speed_names[] = {"auto", "high", "medium", "low"};
uint8_t row = 4; uint8_t row = 4;
uint8_t column = 2; uint8_t column = 2;
const uint8_t light_array[4][2] = { const uint8_t light_array[4][2] = {
@ -20,8 +33,12 @@ const uint8_t light_array[4][2] = {
{LIGHT_ROW3_COLUMN1, LIGHT_ROW3_COLUMN2}, {LIGHT_ROW3_COLUMN1, LIGHT_ROW3_COLUMN2},
{LIGHT_ROW4_COLUMN1, LIGHT_ROW4_COLUMN2}}; {LIGHT_ROW4_COLUMN1, LIGHT_ROW4_COLUMN2}};
// Air Conditioner Configuration
const char *mode_names_daikin[] = {"off", "cool", "fan_only", "dry"};
const char *mode_names_york[] = {"off", "cool", "fan_only"};
const char *fan_speed_names[] = {"auto", "high", "medium", "low"};
AirConditioner ac = { AirConditioner ac_daikin = {
.max_temperature = 30, .max_temperature = 30,
.min_temperature = 18, .min_temperature = 18,
.modes = 4, .modes = 4,
@ -46,13 +63,12 @@ ESPMegaPRO espmega = ESPMegaPRO();
ISEDisplay iseDisplay = ISEDisplay(&iseDisplayAdapter, &light_array[0][0], row, column); ISEDisplay iseDisplay = ISEDisplay(&iseDisplayAdapter, &light_array[0][0], row, column);
ClimateCard climateCard_daikin = ClimateCard(AIR_CONDITIONER_DAIKIN_IR_PIN, ac_daikin, ClimateCard climateCard_daikin = ClimateCard(AIR_CONDITIONER_DAIKIN_IR_PIN, ac_daikin,
AIR_CONDITIONER_SENSOR_TYPE, AIR_CONDITIONER_SENSOR_PIN, AIR_CONDITIONER_SENSOR_TYPE, AIR_CONDITIONER_SENSOR_PIN,
AIR_CONDITIONER_RMT_CHANNEL0); AIR_CONDITIONER_RMT_CHANNEL0);
ClimateCard climateCard_york = ClimateCard(AIR_CONDITIONER_YORK_IR_PIN, ac_york, ClimateCard climateCard_york = ClimateCard(AIR_CONDITIONER_YORK_IR_PIN, ac_york,
AC_SENSOR_TYPE_NONE, 0, AC_SENSOR_TYPE_NONE, 0,
AIR_CONDITIONER_RMT_CHANNEL1); AIR_CONDITIONER_RMT_CHANNEL1);
float adcToCurrent(uint16_t adc_value) float adcToCurrent(uint16_t adc_value)
{ {
@ -65,24 +81,12 @@ float adcToCurrent(uint16_t adc_value)
void handleMqttMessage(char *topic, char *payload) void handleMqttMessage(char *topic, char *payload)
{ {
} // Unused for now
void sendStopBytes()
{
Serial.write(0xFF);
Serial.write(0xFF);
Serial.write(0xFF);
}
void sendExtStopBytes()
{
iseDisplayAdapter.write(0xFF);
iseDisplayAdapter.write(0xFF);
iseDisplayAdapter.write(0xFF);
} }
void setup() void setup()
{ {
// ------------ GPIO 2 Factory Reset Check ------------
gpio_config_t gpio_2_conf; gpio_config_t gpio_2_conf;
gpio_2_conf.intr_type = GPIO_INTR_DISABLE; gpio_2_conf.intr_type = GPIO_INTR_DISABLE;
gpio_2_conf.mode = GPIO_MODE_INPUT; gpio_2_conf.mode = GPIO_MODE_INPUT;
@ -90,6 +94,10 @@ void setup()
gpio_config(&gpio_2_conf); gpio_config(&gpio_2_conf);
// If GPIO 2 is pulled low, clear the FRAM then reboot (Reset the device to factory defaults) // If GPIO 2 is pulled low, clear the FRAM then reboot (Reset the device to factory defaults)
bool clear_fram = !gpio_get_level(GPIO_NUM_2); bool clear_fram = !gpio_get_level(GPIO_NUM_2);
// ------------ End GPIO 2 Factory Reset Check ------------
// ------------ Display Pre Initialization Routine ------------
Serial.begin(115200); Serial.begin(115200);
iseDisplayAdapter.begin(ISE_DISPLAY_BAUD_RATE, SERIAL_8N1, ISE_DISPLAY_RX_PIN, ISE_DISPLAY_TX_PIN); iseDisplayAdapter.begin(ISE_DISPLAY_BAUD_RATE, SERIAL_8N1, ISE_DISPLAY_RX_PIN, ISE_DISPLAY_TX_PIN);
sendExtStopBytes(); sendExtStopBytes();
@ -100,7 +108,15 @@ void setup()
sendStopBytes(); sendStopBytes();
Serial.print("boot_state.txt=\"Core Initializing . . .\""); Serial.print("boot_state.txt=\"Core Initializing . . .\"");
sendStopBytes(); sendStopBytes();
// ------------ End Display Pre Initialization Routine ------------
// Give flow of control to OS and scheduler
espmega.begin(); espmega.begin();
// // ------------ Factory Reset Routine ------------
// Disable factory reset for now
// if (clear_fram) // if (clear_fram)
// { // {
// Serial.print("boot_state.txt=\"Factory Resetting . . .\""); // Serial.print("boot_state.txt=\"Factory Resetting . . .\"");
@ -111,6 +127,10 @@ void setup()
// } // }
// esp_restart(); // esp_restart();
// } // }
// // ------------ End Factory Reset Routine ------------
// ------------ IoT Module Initialization Routine ------------
Serial.print("boot_state.txt=\"IoT Initializing . . .\""); Serial.print("boot_state.txt=\"IoT Initializing . . .\"");
sendStopBytes(); sendStopBytes();
espmega.enableIotModule(); espmega.enableIotModule();
@ -125,8 +145,10 @@ void setup()
espmega.iot->loadMqttConfig(); espmega.iot->loadMqttConfig();
espmega.iot->connectToMqtt(); espmega.iot->connectToMqtt();
Serial.print("boot_state.txt=\"Display Initializing . . .\""); Serial.print("boot_state.txt=\"Display Initializing . . .\"");
espmega.enableInternalDisplay(&Serial);
espmega.enableWebServer(80); espmega.enableWebServer(80);
// ------------ End IoT Module Initialization Routine ------------
// ------------ Inputs and Outputs Initialization Routine ------------
espmega.inputs.registerCallback(on_pin_change); espmega.inputs.registerCallback(on_pin_change);
espmega.outputs.setAutoSaveToFRAM(true); espmega.outputs.setAutoSaveToFRAM(true);
// Set value of pin 0-12 to 4095 // Set value of pin 0-12 to 4095
@ -134,27 +156,37 @@ void setup()
{ {
espmega.outputs.setValue(i, 4095); espmega.outputs.setValue(i, 4095);
} }
espmega.outputs.setState(12, true); // ------------ End Inputs and Outputs Initialization Routine ------------
espmega.outputs.setValue(12,4095);
// ------------ Climate Cards Initialization Routine ------------
ESP_LOGD("ISE OS", "Setting up climate cards"); ESP_LOGD("ISE OS", "Setting up climate cards");
// Daikin Climate Card
espmega.installCard(2, &climateCard_daikin); espmega.installCard(2, &climateCard_daikin);
climateCard_daikin.bindFRAM(&espmega.fram, 5000); climateCard_daikin.bindFRAM(&espmega.fram, 5000);
climateCard_daikin.loadStateFromFRAM(); climateCard_daikin.loadStateFromFRAM();
climateCard_daikin.setFRAMAutoSave(true); climateCard_daikin.setFRAMAutoSave(true);
//espmega.display->bindclimateCard(&climateCard_daikin); // Bind daikin climate card to the internal display
espmega.display->bindClimateCard(&climateCard_daikin);
espmega.installCard(10, &climateCard_daikin); // York Climate Card
espmega.installCard(10, &climateCard_york);
climateCard_york.bindFRAM(&espmega.fram, 5005); climateCard_york.bindFRAM(&espmega.fram, 5005);
climateCard_york.loadStateFromFRAM(); climateCard_york.loadStateFromFRAM();
climateCard_york.setFRAMAutoSave(true); climateCard_york.setFRAMAutoSave(true);
espmega.display->bindClimateCard(&climateCard_york); // ------------ End Climate Cards Initialization Routine ------------
// ------------ Current Transformer Cards Initialization Routine ------------
ESP_LOGD("ISE OS", "Installing current transformer cards"); ESP_LOGD("ISE OS", "Installing current transformer cards");
// First try to install the analog card
analogCardAvailable = espmega.installCard(4, &analogCard); analogCardAvailable = espmega.installCard(4, &analogCard);
if (analogCardAvailable || CT_FORCE_ENABLE) { // If the analog card is available, install the current transformer cards
// If the analog card is not available, current transformer cards will not be installed
// Unless CT_FORCE_ENABLE is set to true
// This is to prevent soft locking the device when the device tries to read from an ADC channel that does not exist
if (analogCardAvailable || CT_FORCE_ENABLE)
{
ESP_LOGV("ISE OS", "Analog card available, installing current transformer cards"); ESP_LOGV("ISE OS", "Analog card available, installing current transformer cards");
espmega.installCard(5, &ct_light_phase1); espmega.installCard(5, &ct_light_phase1);
ct_light_phase1.bindFRAM(&espmega.fram, 5010); ct_light_phase1.bindFRAM(&espmega.fram, 5010);
@ -174,25 +206,17 @@ void setup()
espmega.iot->registerCard(7); espmega.iot->registerCard(7);
espmega.iot->registerCard(8); espmega.iot->registerCard(8);
espmega.iot->registerCard(9); espmega.iot->registerCard(9);
} else { espmega.iot->registerCard(10);
}
else
{
ESP_LOGE("ISE OS", "Analog card not available, current transformer cards cannot and will not be installed."); ESP_LOGE("ISE OS", "Analog card not available, current transformer cards cannot and will not be installed.");
} }
ESP_LOGD("ISE OS", "Registering Current Transformer Cards with IoT Module"); ESP_LOGD("ISE OS", "Registering Current Transformer Cards with IoT Module");
// ------------ End Current Transformer Cards Initialization Routine ------------
// auto binded_display_update_on_pm25_out = std::bind(&display_update,0,std::placeholders::_1);
// auto binded_display_update_on_pm25_in = std::bind(&display_update,1,std::placeholders::_1);
// auto binded_display_update_on_temp_out = std::bind(&display_update,2,std::placeholders::_1);
// auto binded_display_update_on_weather = std::bind(&display_update,3,std::placeholders::_1);
// auto binded_display_update_on_pm_switch = std::bind(&display_update,4,std::placeholders::_1);
// auto binded_display_update_on_pm_fan_speed = std::bind(&display_update,5,std::placeholders::_1);
// pm25_out.registerCallback(std::function<void (char*)>(binded_display_update_on_pm25_out));
// pm25_in.registerCallback(std::function<void (char*)>(binded_display_update_on_pm25_in));
// temp_out.registerCallback(std::function<void (char*)>(binded_display_update_on_temp_out));
// weather.registerCallback(std::function<void (char*)>(binded_display_update_on_weather));
// pm_switch.registerCallback(std::function<void (char*)>(binded_display_update_on_pm_switch));
// pm_fan_speed.registerCallback(std::function<void (char*)>(binded_display_update_on_pm_fan_speed));
// ------------ Remote Variables Initialization Routine ------------
pm25_out.registerCallback(&pm25outupdatedisplay); pm25_out.registerCallback(&pm25outupdatedisplay);
pm25_in.registerCallback(&pm25inupdatedisplay); pm25_in.registerCallback(&pm25inupdatedisplay);
temp_out.registerCallback(&tempoutupdatedisplay); temp_out.registerCallback(&tempoutupdatedisplay);
@ -200,12 +224,7 @@ void setup()
pm_switch.registerCallback(&pmswitchupdatedisplay); pm_switch.registerCallback(&pmswitchupdatedisplay);
pm_fan_speed.registerCallback(&pmfanspeedupdatedisplay); pm_fan_speed.registerCallback(&pmfanspeedupdatedisplay);
// placeholder
// PM2.5 PPM Remote Variable // PM2.5 PPM Remote Variable
// 12 bytes remote variable, 11 characters + null terminator
// Enable value request at /iqair/pm25_request
pm25_out.begin(6, "/aqi/value", espmega.iot, true, "/aqi/request_value"); pm25_out.begin(6, "/aqi/value", espmega.iot, true, "/aqi/request_value");
// Temperature Remote Variable // Temperature Remote Variable
temp_out.begin(6, "/temp/value", espmega.iot, true, "/temp/request_value"); temp_out.begin(6, "/temp/value", espmega.iot, true, "/temp/request_value");
@ -219,190 +238,107 @@ void setup()
// Air Purifier Fan Speed Remote Variable // Air Purifier Fan Speed Remote Variable
pm_fan_speed.begin(6, "/pm/fan_speed", espmega.iot, true, "/pm/request_fan_speed"); pm_fan_speed.begin(6, "/pm/fan_speed", espmega.iot, true, "/pm/request_fan_speed");
pm_fan_speed.enableSetValue("/pm/set_fan_speed"); pm_fan_speed.enableSetValue("/pm/set_fan_speed");
espmega.iot->registerCard(0); // Register the Input Card // ------------ End Remote Variables Initialization Routine ------------
espmega.iot->registerCard(1); // Register the Output Card
espmega.iot->registerCard(2); // Register the Climate Card Daikin
// ------------ IoT Card Registration Routine ------------
espmega.iot->registerCard(0); // Register the Input Card
espmega.iot->registerCard(1); // Register the Output Card
espmega.iot->registerCard(2); // Register the Climate Card Daikin
espmega.iot->registerCard(10); // Register the Climate Card York espmega.iot->registerCard(10); // Register the Climate Card York
// ------------ End IoT Card Registration Routine ------------
// ------------ Display Initialization Routine ------------
espmega.enableInternalDisplay(&Serial);
auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, &espmega); auto bindedGetTime = std::bind(&ESPMegaPRO::getTime, &espmega);
iseDisplay.begin(&espmega.inputs, &espmega.outputs, &climateCard_daikin, &climateCard_york, &pm_switch, &pm_fan_speed); iseDisplay.begin(&espmega.inputs, &espmega.outputs, &climateCard_daikin, &climateCard_york, &pm_switch, &pm_fan_speed);
espmega.iot->registerRelativeMqttCallback(&handleMqttMessage); espmega.iot->registerRelativeMqttCallback(&handleMqttMessage);
iseDisplay.registerPageChangeCallback(&handlePageChange); iseDisplay.registerPageChangeCallback(&handlePageChange);
// ------------ End Display Initialization Routine ------------
} }
// void update_display(uint8_t type, char *value){ void pm25outupdatedisplay(char *value)
// switch (type) {
// {
// case 0:
// iseDisplay.updatePMoutside(get_pm25_out());
// break;
// case 1:
// iseDisplay.updatePMinside(get_pm25_in());
// break;
// case 2:
// iseDisplay.updateTempOutside(get_temp_out());
// break;
// case 3:
// iseDisplay.updateWeather(weather.getValue());
// break;
// case 4:
// iseDisplay.updateAirPurifierState();
// break;
// case 5:
// iseDisplay.updateAirPurifierState();
// break;
// default:
// break;
// }
// }
void pm25outupdatedisplay(char* value){
iseDisplay.updatePMoutside(get_pm25_out()); iseDisplay.updatePMoutside(get_pm25_out());
} }
void pm25inupdatedisplay(char* value){ void pm25inupdatedisplay(char *value)
{
iseDisplay.updatePMinside(get_pm25_in()); iseDisplay.updatePMinside(get_pm25_in());
} }
void tempoutupdatedisplay(char* value){ void tempoutupdatedisplay(char *value)
{
iseDisplay.updateTempOutside(get_temp_out()); iseDisplay.updateTempOutside(get_temp_out());
} }
void weatherupdatedisplay(char* value){ void weatherupdatedisplay(char *value)
{
iseDisplay.updateWeather(weather.getValue()); iseDisplay.updateWeather(weather.getValue());
} }
void pmfanspeedupdatedisplay(char* value){ void pmfanspeedupdatedisplay(char *value)
{
iseDisplay.updateAirPurifierState(); iseDisplay.updateAirPurifierState();
} }
void pmswitchupdatedisplay(char* value){ void pmswitchupdatedisplay(char *value)
{
ESP_LOGI("PM switch", "getting PM switch state from MQTT: %d", pm_switch.getValue()); ESP_LOGI("PM switch", "getting PM switch state from MQTT: %d", pm_switch.getValue());
iseDisplay.updateAirPurifierState(); iseDisplay.updateAirPurifierState();
ESP_LOGI("PM switch", "toggling PM switch state from: %d to %d", pm_switch.getValue(), !pm_switch.getValue()); ESP_LOGI("PM switch", "toggling PM switch state from: %d to %d", pm_switch.getValue(), !pm_switch.getValue());
} }
void loop() void loop()
{ {
espmega.loop(); espmega.loop();
iseDisplay.loop(); iseDisplay.loop();
// Update the time every 15 seconds // Update the time every 15 seconds
// static bool run_when_init = false; // No static ? , this shit will reset to 0 every loop
static uint32_t last_time_updated = 0; static uint32_t last_time_updated = 0;
// rtctime_t time = espmega.getTime();
// when init update once then update every 15 seconds
// You don't need this, when it go from page 0 to 1 it call handlePageChange
// if(!run_when_init){
// time = espmega.getTime();
// iseDisplay.updateDateTimeText(time);
// last_time_updated = millis();
// run_when_init = true;
// }
if (millis() - last_time_updated > 15000) if (millis() - last_time_updated > 15000)
{ {
rtctime_t time = espmega.getTime(); rtctime_t time = espmega.getTime();
// log time
ESP_LOGI("time", "Time: %d:%d:%d", time.hours, time.minutes, time.seconds);
ESP_LOGI("Date", "Date: %d/%d/%d", time.day, time.month, time.year);
iseDisplay.updateDateTimeText(time); iseDisplay.updateDateTimeText(time);
last_time_updated = millis(); last_time_updated = millis();
} }
// // 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);
// iseDisplay.updatePMoutside(pm25_out_value);
// last_pm25_out_update = millis();
// }
// // Update the PM2.5 PPM value every 15 seconds
// static uint32_t last_pm25_in_update = 0;
// if (millis() - last_pm25_in_update > 15000)
// {
// uint8_t pm25_in_value = get_pm25_in();
// iseDisplay.updatePMinside(pm25_in_value);
// last_pm25_in_update = millis();
// }
// Update the temperature value every 15 seconds
// static uint32_t last_temp_out_update = 0;
// if (millis() - last_temp_out_update > 15000)
// {
// float temp_out_value = get_temp_out();
// iseDisplay.updateTempOutside(temp_out_value);
// last_temp_out_update = millis();
// }
// // Update the weather value every 15 seconds
// static uint32_t last_weather_update = 0;
// if (millis() - last_weather_update > 15000)
// {
// char *weather_value = weather.getValue();
// 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 > 500)
// {
// iseDisplay.updateAirPurifierState();
// last_pm_switch_update = millis();
// }
} }
void on_pin_change(uint8_t pin, uint8_t value) void on_pin_change(uint8_t pin, uint8_t value)
{ {
// For input pin 0-3, map it with output pin 0-3 switch (pin)
// For input pin 4-7, map it with output pin 8-11 {
// For input pin 12, map it with output pin 12 case LIGHT_ROW1_COLUMN1_IN:
// When Input pin change, Toggle the output pin espmega.outputs.setState(LIGHT_ROW1_COLUMN1, !espmega.outputs.getState(LIGHT_ROW1_COLUMN1));
switch (pin){ break;
case LIGHT_ROW1_COLUMN1_IN: case LIGHT_ROW1_COLUMN2_IN:
espmega.outputs.setState(LIGHT_ROW1_COLUMN1, !espmega.outputs.getState(LIGHT_ROW1_COLUMN1)); espmega.outputs.setState(LIGHT_ROW1_COLUMN2, !espmega.outputs.getState(LIGHT_ROW1_COLUMN2));
break; break;
case LIGHT_ROW1_COLUMN2_IN: case LIGHT_ROW2_COLUMN1_IN:
espmega.outputs.setState(LIGHT_ROW1_COLUMN2, !espmega.outputs.getState(LIGHT_ROW1_COLUMN2)); espmega.outputs.setState(LIGHT_ROW2_COLUMN1, !espmega.outputs.getState(LIGHT_ROW2_COLUMN1));
break; break;
case LIGHT_ROW2_COLUMN1_IN: case LIGHT_ROW2_COLUMN2_IN:
espmega.outputs.setState(LIGHT_ROW2_COLUMN1, !espmega.outputs.getState(LIGHT_ROW2_COLUMN1)); espmega.outputs.setState(LIGHT_ROW2_COLUMN2, !espmega.outputs.getState(LIGHT_ROW2_COLUMN2));
break; break;
case LIGHT_ROW2_COLUMN2_IN: case LIGHT_ROW3_COLUMN1_IN:
espmega.outputs.setState(LIGHT_ROW2_COLUMN2, !espmega.outputs.getState(LIGHT_ROW2_COLUMN2)); espmega.outputs.setState(LIGHT_ROW3_COLUMN1, !espmega.outputs.getState(LIGHT_ROW3_COLUMN1));
break; break;
case LIGHT_ROW3_COLUMN1_IN: case LIGHT_ROW3_COLUMN2_IN:
espmega.outputs.setState(LIGHT_ROW3_COLUMN1, !espmega.outputs.getState(LIGHT_ROW3_COLUMN1)); espmega.outputs.setState(LIGHT_ROW3_COLUMN2, !espmega.outputs.getState(LIGHT_ROW3_COLUMN2));
break; break;
case LIGHT_ROW3_COLUMN2_IN: case LIGHT_ROW4_COLUMN1_IN:
espmega.outputs.setState(LIGHT_ROW3_COLUMN2, !espmega.outputs.getState(LIGHT_ROW3_COLUMN2)); espmega.outputs.setState(LIGHT_ROW4_COLUMN1, !espmega.outputs.getState(LIGHT_ROW4_COLUMN1));
break; break;
case LIGHT_ROW4_COLUMN1_IN: case LIGHT_ROW4_COLUMN2_IN:
espmega.outputs.setState(LIGHT_ROW4_COLUMN1, !espmega.outputs.getState(LIGHT_ROW4_COLUMN1)); espmega.outputs.setState(LIGHT_ROW4_COLUMN2, !espmega.outputs.getState(LIGHT_ROW4_COLUMN2));
break; break;
case LIGHT_ROW4_COLUMN2_IN: case COMPUTER_DESK_SWITCH_IN:
espmega.outputs.setState(LIGHT_ROW4_COLUMN2, !espmega.outputs.getState(LIGHT_ROW4_COLUMN2)); espmega.outputs.setState(COMPUTER_DESK_SWITCH_OUT, !espmega.outputs.getState(COMPUTER_DESK_SWITCH_OUT));
break; break;
case COMPUTER_DESK_SWITCH_IN:
espmega.outputs.setState(COMPUTER_DESK_SWITCH_OUT, !espmega.outputs.getState(COMPUTER_DESK_SWITCH_OUT));
break;
} }
// if (pin < 4)
// {
// bool new_value = !espmega.outputs.getState(pin);
// espmega.outputs.setState(pin, new_value);
// }
// else if (pin < 8)
// {
// bool new_value = !espmega.outputs.getState(pin + 4);
// espmega.outputs.setState(pin + 4, new_value);
// }
// else if (pin == 12)
// {
// bool new_value = !espmega.outputs.getState(12);
// espmega.outputs.setState(12, new_value);
// }
} }
uint16_t get_pm25_out() uint16_t get_pm25_out()
{ {
uint16_t pm25_out_value = 0; uint16_t pm25_out_value = 0;
// Read PM2.5 PPM from sensor // Read PM2.5 PPM from home assistant
pm25_out_value = atoi(pm25_out.getValue()); pm25_out_value = atoi(pm25_out.getValue());
ESP_LOGI("PM2.5", "getting PM2.5 PPM from MQTT: %d", pm25_out_value); ESP_LOGI("PM2.5", "getting PM2.5 PPM from MQTT: %d", pm25_out_value);
return pm25_out_value; return pm25_out_value;
@ -411,7 +347,7 @@ uint16_t get_pm25_out()
uint16_t get_pm25_in() uint16_t get_pm25_in()
{ {
uint16_t pm25_in_value = 0; uint16_t pm25_in_value = 0;
// Read PM2.5 PPM from sensor // Read PM2.5 PPM from home assistant
pm25_in_value = atoi(pm25_in.getValue()); pm25_in_value = atoi(pm25_in.getValue());
ESP_LOGI("PM2.5", "getting PM2.5 PPM from MQTT: %d", pm25_in_value); ESP_LOGI("PM2.5", "getting PM2.5 PPM from MQTT: %d", pm25_in_value);
return pm25_in_value; return pm25_in_value;
@ -420,7 +356,7 @@ uint16_t get_pm25_in()
float get_temp_out() float get_temp_out()
{ {
float temp_out_value = 0; float temp_out_value = 0;
// Read temperature from sensor // Read temperature from home assistant
temp_out_value = atof(temp_out.getValue()); temp_out_value = atof(temp_out.getValue());
ESP_LOGI("Temperature", "getting Temperature from MQTT: %f", temp_out_value); ESP_LOGI("Temperature", "getting Temperature from MQTT: %f", temp_out_value);
return temp_out_value; return temp_out_value;
@ -429,24 +365,30 @@ float get_temp_out()
uint8_t get_pm_fanspeed() uint8_t get_pm_fanspeed()
{ {
uint8_t pm_fan_speed_value = 0; uint8_t pm_fan_speed_value = 0;
// Read PM2.5 fan speed from sensor // Read PM2.5 fan speed from home assistant
pm_fan_speed_value = (int) atof(pm_fan_speed.getValue()); 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); ESP_LOGI("PM fan speed", "getting PM2.5 PPM from MQTT: %d", pm_fan_speed_value);
return pm_fan_speed_value; return pm_fan_speed_value;
} }
/**
* @brief Get the PM switch state from home assistant
* @return true if the PM switch is on, false if the PM switch is off
*/
bool get_pm_switch() bool get_pm_switch()
{ {
ESP_LOGI("PM switch", "getting PM switch state from MQTT: %d", pm_switch.getValue()); 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; bool is_pm_switch_on = strcmp(pm_switch.getValue(), "on") == 0;
return is_pm_switch_on; return is_pm_switch_on;
} }
void toggle_pm_switch() void toggle_pm_switch()
{ {
bool is_pm_switch_on = get_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); 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"); pm_switch.setValue(is_pm_switch_on ? "0" : "1");
} }
void set_pm_fanspeed(uint8_t speed) void set_pm_fanspeed(uint8_t speed)
{ {
ESP_LOGI("PM fan speed", "setting PM fan speed to: %d", speed); ESP_LOGI("PM fan speed", "setting PM fan speed to: %d", speed);
@ -457,9 +399,6 @@ void set_pm_fanspeed(uint8_t speed)
void handlePageChange(uint8_t page) void handlePageChange(uint8_t page)
{ {
// Which already send the time
// printESP_LOGI the page have changed
ESP_LOGI("Page", "Page change to: %d", page); ESP_LOGI("Page", "Page change to: %d", page);
rtctime_t time = espmega.getTime(); rtctime_t time = espmega.getTime();
iseDisplay.updateDateTimeText(time); iseDisplay.updateDateTimeText(time);
@ -477,17 +416,23 @@ void handlePageChange(uint8_t page)
break; break;
} }
// iseDisplay.updatePMinside();
iseDisplay.updatePMoutside(get_pm25_out()); iseDisplay.updatePMoutside(get_pm25_out());
iseDisplay.updatePMinside(get_pm25_in()); iseDisplay.updatePMinside(get_pm25_in());
iseDisplay.updateWeather(weather.getValue()); iseDisplay.updateWeather(weather.getValue());
iseDisplay.updateTempOutside(get_temp_out()); iseDisplay.updateTempOutside(get_temp_out());
iseDisplay.updateACState(); iseDisplay.updateACState();
}
/* iseDisplay.updateACState();
iseDisplay.updateAirPurifierState(); void sendStopBytes()
iseDisplay.updateLightGroupStatePageStandby(); {
iseDisplay.updateLightGroupStatePageDashboard(); Serial.write(0xFF);
iseDisplay.updateuserACmode(); Serial.write(0xFF);
iseDisplay.updateAirPurifierStateStandby(); */ Serial.write(0xFF);
}
void sendExtStopBytes()
{
iseDisplayAdapter.write(0xFF);
iseDisplayAdapter.write(0xFF);
iseDisplayAdapter.write(0xFF);
} }

View file

@ -35,6 +35,8 @@ SET_LOOP_TASK_STACK_SIZE(32*1024);
#define CT_PIN_AC_PHASE2 4 #define CT_PIN_AC_PHASE2 4
#define CT_PIN_AC_PHASE3 5 #define CT_PIN_AC_PHASE3 5
float adcToCurrent(uint16_t adc_value);
void handleMqttMessage(char *topic, char *payload); void handleMqttMessage(char *topic, char *payload);
void subscribeToMqttTopics(); void subscribeToMqttTopics();
void handlePageChange(uint8_t page); void handlePageChange(uint8_t page);