diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp index e7108fc..274ac17 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.cpp @@ -1,9 +1,8 @@ #include -ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin) +ClimateCard::ClimateCard(uint8_t ir_pin) { this->ir_pin = ir_pin; - irsender.begin(ir_pin); // Initialize Pointers this->dht = nullptr; this->ds18b20 = nullptr; @@ -20,12 +19,19 @@ ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin) this->state.ac_temperature = 25; this->state.ac_mode = 0; this->state.ac_fan_speed = 0; + // Initialize RMT + gpio_num_t gpio_num = gpio_num_t(ir_pin); + rmt_config_t rmt_tx = RMT_DEFAULT_CONFIG_TX(gpio_num, RMT_TX_CHANNEL); + rmt_tx.clk_div = 80; // 1MHz clock + rmt_config(&rmt_tx); + rmt_driver_install(rmt_tx.channel, 0, 0); } ClimateCard::~ClimateCard() { delete dht; delete ds18b20; + rmt_driver_uninstall(RMT_TX_CHANNEL); } bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin) @@ -151,9 +157,20 @@ void ClimateCard::updateSensor() void ClimateCard::updateAirConditioner() { - irsender.sendRaw(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature], - sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature]) / sizeof(uint16_t), - NEC_KHZ); + size_t itemCount = sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature]) / sizeof(ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][0]); + rmt_item32_t items[itemCount]; + // Convert IR timing array to RMT items + for (size_t i = 0; i < itemCount; i++) + { + items[i].level0 = 1; + items[i].duration0 = ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][i]; + items[i].level1 = 0; + items[i].duration1 = ac.infraredCodes[this->state.ac_mode][this->state.ac_fan_speed][this->state.ac_temperature][i]; + } + // Send IR signal + rmt_write_items(RMT_TX_CHANNEL, items, itemCount, true); + rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY); + // Publish state for (uint8_t i = 0; i < callbacks.size(); i++) { callbacks[i](this->state.ac_mode, this->state.ac_fan_speed, this->state.ac_temperature); diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp index c7305a2..45fd00a 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateCard.hpp @@ -1,12 +1,14 @@ #pragma once #include -#include +#include #include #include #include #include #include +#define RMT_TX_CHANNEL RMT_CHANNEL_0 + #define CARD_TYPE_CLIMATE 0x03 #define AC_SENSOR_TYPE_NONE 0x00 @@ -68,7 +70,6 @@ class ClimateCard : public ExpansionCard { void updateSensor(); void updateAirConditioner(); // IR variables - IRsend irsender; uint8_t ir_pin; // Air conditioner variables AirConditioner ac; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp index 52ff878..613dc76 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ClimateIoT.cpp @@ -129,4 +129,12 @@ bool ClimateIoT::processRequestStateMessage(char *topic, char *payload, uint8_t return true; } return false; +} + +void ClimateIoT::handleSensorUpdate(float temperature, float humidity) { + this->publishSensor(); +} + +void ClimateIoT::handleAirConditionerUpdate(uint8_t mode, uint8_t fan_speed, uint8_t temperature) { + this->publishClimate(); } \ No newline at end of file diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp index fb27a09..4d34d30 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.cpp @@ -115,15 +115,15 @@ void ESPMegaIoT::registerCard(uint8_t card_id) components[card_id]->publishReport(); } break; - // case CARD_TYPE_CLIMATE: - // components[card_id] = new ClimateIoT(); - // components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); - // if (mqtt_connected) - // { - // components[card_id]->subscribe(); - // components[card_id]->publishReport(); - // } - // break; + case CARD_TYPE_CLIMATE: + components[card_id] = new ClimateIoT(); + components[card_id]->begin(card_id, cards[card_id], &mqtt, base_topic); + if (mqtt_connected) + { + components[card_id]->subscribe(); + components[card_id]->publishReport(); + } + break; default: Serial.println("Unsupported card type"); return; diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.hpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.hpp index 4c7a971..b6902b4 100644 --- a/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.hpp +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/ESPMegaIoT.hpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp b/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp new file mode 100644 index 0000000..ab7d635 --- /dev/null +++ b/ESPMegaPRO-firmware/lib/ESPMegaPRO/Untitled-1.cpp @@ -0,0 +1,32 @@ +#include "driver/rmt.h" + +#define RMT_TX_CHANNEL RMT_CHANNEL_0 +#define RMT_TX_GPIO_NUM GPIO_NUM_4 + +void app_main() { + // Configure RMT transmitter + rmt_config_t config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO_NUM, RMT_TX_CHANNEL); + config.clk_div = 80; // Set clock divider for desired frequency (80MHz / 80 = 1MHz) + rmt_config(&config); + rmt_driver_install(config.channel, 0, 0); + + // Define NEC IR protocol parameters + uint16_t irTimingArray[] = { /* Your IR timing array here */ }; + size_t itemCount = sizeof(irTimingArray) / sizeof(irTimingArray[0]); + rmt_item32_t items[itemCount]; + + // Convert IR timing array to RMT items + for (size_t i = 0; i < itemCount; i++) { + items[i].level0 = 1; + items[i].duration0 = irTimingArray[i]; + items[i].level1 = 0; + items[i].duration1 = irTimingArray[i]; + } + + // Send IR signal + rmt_write_items(config.channel, items, itemCount, true); + rmt_wait_tx_done(config.channel, portMAX_DELAY); + + // Cleanup RMT driver + rmt_driver_uninstall(config.channel); +} diff --git a/ESPMegaPRO-firmware/platformio.ini b/ESPMegaPRO-firmware/platformio.ini index 5b848e1..ebbab18 100644 --- a/ESPMegaPRO-firmware/platformio.ini +++ b/ESPMegaPRO-firmware/platformio.ini @@ -25,7 +25,6 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1 paulstoffregen/DS1307RTC@0.0.0-alpha+sha.c2590c0033 knolleary/pubsubclient@^2.8.0 seithan/Easy Nextion Library@^1.0.6 - z3t0/IRremote@^4.2.0 bblanchon/ArduinoJson@^6.21.4 robtillaart/DS18B20@^0.2.1 robtillaart/DHTNEW@^0.4.18 diff --git a/ESPMegaPRO-firmware/src/iot_framdemo.cpp b/ESPMegaPRO-firmware/src/iot_framdemo.cpp index c08813b..f7a85d0 100644 --- a/ESPMegaPRO-firmware/src/iot_framdemo.cpp +++ b/ESPMegaPRO-firmware/src/iot_framdemo.cpp @@ -1,6 +1,7 @@ #include #include #include +#include ESPMegaPRO espmega = ESPMegaPRO();