fixed climate card mqtt crash bug

This commit is contained in:
Siwat Sirichai 2023-12-31 23:29:40 +07:00
parent 9cd5b7132b
commit 4c77474a94
8 changed files with 128 additions and 50 deletions

View file

@ -5,10 +5,17 @@
// #define FRAM_DEBUG
#define MQTT_DEBUG
// #define WRITE_DEFAULT_NETCONF
#define CLIMATE_CARD_ENABLE
#define MQTT_CARD_REGISTER
#define DISPLAY_ENABLE
// Demo PLC firmware using the ESPMegaPRO OOP library
ESPMegaPRO espmega = ESPMegaPRO();
#ifdef CLIMATE_CARD_ENABLE
// Climate Card
const uint16_t irCode[15][4][4][1] = {0};
const char *mode_names[] = {"Off", "Fan-only", "Cool"};
const char *fan_speed_names[] = {"Auto", "Low", "Medium", "High"};
@ -30,6 +37,7 @@ AirConditioner ac = {
.getInfraredCode = &getInfraredCode};
ClimateCard climateCard = ClimateCard(14, ac);
#endif
void input_change_callback(uint8_t pin, uint8_t value)
{
@ -39,14 +47,15 @@ void input_change_callback(uint8_t pin, uint8_t value)
Serial.println(value);
}
#ifdef WRITE_DEFAULT_NETCONF
void setNetworkConfig()
{
NetworkConfig config = {
.ip = {192, 168, 0, 11},
.gateway = {192, 168, 0, 1},
.ip = {10, 16, 6, 213},
.gateway = {10, 16, 6, 1},
.subnet = {255, 255, 255, 0},
.dns1 = {192, 168, 0, 1},
.dns2 = {192, 168, 0, 1},
.dns1 = {10, 192, 1, 1},
.dns2 = {10, 192, 1, 1},
.useStaticIp = true,
.useWifi = false,
.wifiUseAuth = false,
@ -69,6 +78,7 @@ void setMqttConfig()
espmega.iot->setMqttConfig(config);
espmega.iot->saveMqttConfig();
}
#endif
void setup()
{
@ -80,20 +90,31 @@ void setup()
ETH.begin();
ESP_LOGI("Initializer", "Binding Ethernet to IOT module");
espmega.iot->bindEthernetInterface(&ETH);
#ifdef WRITE_DEFAULT_NETCONF
setNetworkConfig();
#else
ESP_LOGI("Initializer", "Loading network config");
espmega.iot->loadNetworkConfig();
#endif
ESP_LOGI("Initializer", "Connecting to network");
espmega.iot->connectNetwork();
#ifdef WRITE_DEFAULT_NETCONF
setMqttConfig();
#else
ESP_LOGI("Initializer", "Loading MQTT config");
espmega.iot->loadMqttConfig();
#endif
ESP_LOGI("Initializer", "Connecting to MQTT");
espmega.iot->connectToMqtt();
#ifdef MQTT_CARD_REGISTER
ESP_LOGI("Initializer", "Registering cards 0");
espmega.iot->registerCard(0);
ESP_LOGI("Initializer", "Registering cards 1");
espmega.iot->registerCard(1);
#endif
ESP_LOGI("Initializer", "Registering Input change callback");
espmega.inputs.registerCallback(input_change_callback);
#ifdef CLIMATE_CARD_ENABLE
ESP_LOGI("Initializer", "Installing climate card");
espmega.installCard(2, &climateCard);
ESP_LOGI("Initializer", "Binding climate card to FRAM");
@ -102,10 +123,15 @@ void setup()
climateCard.loadStateFromFRAM();
ESP_LOGI("Initializer", "Enabling climate card FRAM autosave");
climateCard.setFRAMAutoSave(true);
ESP_LOGI("Initializer", "Registering cards 2");
espmega.iot->registerCard(2);
#endif
#ifdef DISPLAY_ENABLE
ESP_LOGI("Initializer", "Enabling internal display");
espmega.enableInternalDisplay(&Serial);
ESP_LOGI("Initializer", "Binding climate card to internal display");
espmega.display->bindClimateCard(&climateCard);
#endif
}
void loop()
@ -130,7 +156,7 @@ void loop()
if (millis() - last_mqtt_publish >= 5000)
{
last_mqtt_publish = millis();
espmega.iot->publishToTopic("/espmegai/alive", "true");
espmega.iot->publish("/espmegai/alive", "true");
}
static uint32_t last_mqtt_status = 0;
if (millis() - last_mqtt_status >= 1000)