ac card implementation

This commit is contained in:
Siwat Sirichai 2023-12-30 13:49:37 +07:00
parent f17b220a38
commit 1ee4209e3c
9 changed files with 217 additions and 11 deletions

View File

@ -49,7 +49,9 @@
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"bitset": "cpp",
"regex": "cpp"
}, },
"cmake.configureOnOpen": true, "cmake.configureOnOpen": true,
"cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO", "cmake.sourceDirectory": "D:/Git/ESPMegaPRO-v3-SDK/Template Project/.pio/libdeps/wt32-eth01/Adafruit BusIO",

View File

@ -0,0 +1,174 @@
#include <ClimateCard.hpp>
ClimateCard::ClimateCard(uint8_t ir_pin) : irsender(ir_pin)
{
this->ir_pin = ir_pin;
irsender.begin(ir_pin);
// Initialize Pointers
this->dht = nullptr;
this->ds18b20 = nullptr;
this->fram = nullptr;
// Initialize Variables
this->fram_address = 0;
this->fram_auto_save = false;
this->state.ac_temperature = 0;
this->state.ac_mode = 0;
this->state.ac_fan_speed = 0;
this->humidity = 0;
this->room_temperature = 0;
// Initialize state
this->state.ac_temperature = 25;
this->state.ac_mode = 0;
this->state.ac_fan_speed = 0;
}
ClimateCard::~ClimateCard()
{
delete dht;
delete ds18b20;
}
bool ClimateCard::begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin)
{
this->ac = ac;
this->sensor_type = sensor_type;
this->sensor_pin = sensor_pin;
switch (sensor_type)
{
case AC_SENSOR_TYPE_DHT22:
dht = new DHTNEW(sensor_pin);
break;
case AC_SENSOR_TYPE_DS18B20:
OneWire oneWire(sensor_pin);
ds18b20 = new DS18B20(&oneWire);
break;
}
updateAirConditioner();
}
bool ClimateCard::begin(AirConditioner ac)
{
this->begin(ac, AC_SENSOR_TYPE_NONE, 0);
}
void ClimateCard::loop()
{
static uint32_t last_sensor_update = 0;
if (millis() - last_sensor_update >= AC_SENSOR_READ_INTERVAL)
{
last_sensor_update = millis();
updateSensor();
}
}
void ClimateCard::bindFRAM(FRAM *fram, uint16_t fram_address)
{
this->fram = fram;
this->fram_address = fram_address;
}
void ClimateCard::setFRAMAutoSave(bool autoSave)
{
this->fram_auto_save = autoSave;
}
void ClimateCard::saveStateToFRAM()
{
fram->writeObject(fram_address, this->state);
}
void ClimateCard::loadStateFromFRAM()
{
fram->readObject(fram_address, this->state);
}
void ClimateCard::setTemperature(uint8_t temperature)
{
this->state.ac_temperature = temperature;
updateAirConditioner();
}
void ClimateCard::setMode(uint8_t mode)
{
this->state.ac_mode = mode;
updateAirConditioner();
}
void ClimateCard::setFanSpeed(uint8_t fan_speed)
{
this->state.ac_fan_speed = fan_speed;
updateAirConditioner();
}
void ClimateCard::registerCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback)
{
callbacks.push_back(callback);
}
uint8_t ClimateCard::getType()
{
return CARD_TYPE_CLIMATE;
}
void ClimateCard::updateSensor()
{
// Read sensor data and update variables
switch (sensor_type)
{
case AC_SENSOR_TYPE_DHT22:
if (millis() - dht->lastRead() < AC_SENSOR_READ_INTERVAL)
return;
dht->read();
room_temperature = dht->getTemperature();
humidity = dht->getHumidity();
break;
case AC_SENSOR_TYPE_DS18B20:
ds18b20->requestTemperatures();
uint32_t start = millis();
while (!ds18b20->isConversionComplete())
{
if (millis() - start >= AC_SENSOR_READ_TIMEOUT)
{
return;
}
}
room_temperature = ds18b20->getTempC();
break;
}
}
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);
for (uint8_t i = 0; i < callbacks.size(); i++)
{
callbacks[i](this->state.ac_temperature, this->state.ac_mode, this->state.ac_fan_speed);
}
}
float ClimateCard::getRoomTemperature()
{
return room_temperature;
}
float ClimateCard::getHumidity()
{
return humidity;
}
uint8_t ClimateCard::getTemperature()
{
return state.ac_temperature;
}
uint8_t ClimateCard::getMode()
{
return state.ac_mode;
}
uint8_t ClimateCard::getFanSpeed()
{
return state.ac_fan_speed;
}

View File

@ -2,6 +2,9 @@
#include <ExpansionCard.hpp> #include <ExpansionCard.hpp>
#include <IRremote.hpp> #include <IRremote.hpp>
#include <FRAM.h> #include <FRAM.h>
#include <OneWire.h>
#include <DS18B20.h>
#include <dhtnew.h>
#include <vector> #include <vector>
#define CARD_TYPE_CLIMATE 0x03 #define CARD_TYPE_CLIMATE 0x03
@ -10,41 +13,63 @@
#define AC_SENSOR_TYPE_DHT22 0x01 #define AC_SENSOR_TYPE_DHT22 0x01
#define AC_SENSOR_TYPE_DS18B20 0x02 #define AC_SENSOR_TYPE_DS18B20 0x02
#define AC_SENSOR_READ_INTERVAL 5000
#define AC_SENSOR_READ_TIMEOUT 250
struct ClimateCardData { struct ClimateCardData {
uint8_t ac_temperature; uint8_t ac_temperature;
uint8_t ac_mode; uint8_t ac_mode;
uint8_t ac_fan_speed; uint8_t ac_fan_speed;
}; };
struct AirConditioner {
uint8_t max_temperature;
uint8_t min_temperature;
uint8_t modes;
char **mode_names;
uint8_t fan_speeds;
char **fan_speed_names;
uint16_t ****infraredCodes;
};
// This requires 3 bytes of FRAM // This requires 3 bytes of FRAM
class ClimateCard : public ExpansionCard { class ClimateCard : public ExpansionCard {
public: public:
bool begin(uint16_t *infraredCodes, uint8_t sensor_type, uint8_t sensor_pin); ClimateCard(uint8_t ir_pin);
bool begin(uint16_t *infraredCodes); ~ClimateCard();
bool begin(AirConditioner ac, uint8_t sensor_type, uint8_t sensor_pin);
bool begin(AirConditioner ac);
void loop(); void loop();
void bindFRAM(FRAM *fram, uint16_t fram_address); void bindFRAM(FRAM *fram, uint16_t fram_address);
void setFRAMAutoSave(bool autoSave); void setFRAMAutoSave(bool autoSave);
void saveStateToFRAM(); void saveStateToFRAM();
void loadStateFromFRAM(); void loadStateFromFRAM();
void setTemperature(uint8_t temperature); void setTemperature(uint8_t temperature);
uint8_t getTemperature();
void setMode(uint8_t mode); void setMode(uint8_t mode);
uint8_t getMode();
void setFanSpeed(uint8_t fan_speed); void setFanSpeed(uint8_t fan_speed);
uint8_t getFanSpeed();
float getRoomTemperature();
float getHumidity();
void registerCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback); void registerCallback(std::function<void(uint8_t, uint8_t, uint8_t)> callback);
uint8_t getType(); uint8_t getType();
private: private:
// Sensor objects
// We use pointers here because we don't know which sensor will be used
DHTNEW *dht;
DS18B20 *ds18b20;
// Callbacks // Callbacks
std::vector<std::function<void(uint8_t, uint8_t, uint8_t)>> callbacks; std::vector<std::function<void(uint8_t, uint8_t, uint8_t)>> callbacks;
// Update functions // Update functions
void updateSensor(); void updateSensor();
void updateAirConditioner(); void updateAirConditioner();
void updateAirConditioner(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
// IR variables // IR variables
IRsend irsender; IRsend irsender;
uint16_t *infraredCodes; uint8_t ir_pin;
// Air conditioner variables // Air conditioner variables
int ac_temperature; AirConditioner ac;
int ac_mode; ClimateCardData state;
int ac_fan_speed;
// Sensor variables // Sensor variables
uint8_t sensor_type; uint8_t sensor_type;
uint8_t sensor_pin; uint8_t sensor_pin;
@ -53,4 +78,5 @@ class ClimateCard : public ExpansionCard {
// FRAM variables // FRAM variables
FRAM *fram; FRAM *fram;
uint8_t fram_address; uint8_t fram_address;
bool fram_auto_save;
}; };

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <Arduino.h> #include <Arduino.h>
#include <ESPMegaPRO_OOP.hpp>
#define DISPLAY_FETCH_TIMEOUT 100 // ms #define DISPLAY_FETCH_TIMEOUT 100 // ms

View File

@ -10,6 +10,8 @@
#include <DS1307RTC.h> #include <DS1307RTC.h>
#include <time.h> #include <time.h>
#include <TimeStructure.hpp> #include <TimeStructure.hpp>
#include <ESPMegaDisplay.hpp>
#include <InternalDisplay.hpp>
#define FRAM_ADDRESS 0x56 #define FRAM_ADDRESS 0x56
#define INPUT_BANK_A_ADDRESS 0x21 #define INPUT_BANK_A_ADDRESS 0x21

View File

@ -1,3 +1,4 @@
#pragma once
#include <ESPMegaDisplay.hpp> #include <ESPMegaDisplay.hpp>
#include <TimeStructure.hpp> #include <TimeStructure.hpp>
#include <ESPMegaIoT.hpp> #include <ESPMegaIoT.hpp>

View File

@ -26,6 +26,9 @@ lib_deps = adafruit/Adafruit PWM Servo Driver Library@^2.4.1
knolleary/pubsubclient@^2.8.0 knolleary/pubsubclient@^2.8.0
seithan/Easy Nextion Library@^1.0.6 seithan/Easy Nextion Library@^1.0.6
z3t0/IRremote@^4.2.0 z3t0/IRremote@^4.2.0
esphome/ESPAsyncWebServer-esphome@^3.1.0 bblanchon/ArduinoJson@^6.21.4
robtillaart/DS18B20@^0.2.1
robtillaart/DHTNEW@^0.4.18
#esphome/ESPAsyncWebServer-esphome@^3.1.0
monitor_speed = 115200 monitor_speed = 115200

View File

@ -3,7 +3,6 @@
#include <ETH.h> #include <ETH.h>
ESPMegaPRO espmega = ESPMegaPRO(); ESPMegaPRO espmega = ESPMegaPRO();
InternalDisplay display = InternalDisplay(&Serial);
void input_change_callback(uint8_t pin, uint8_t value) { void input_change_callback(uint8_t pin, uint8_t value) {
Serial.print("Input change callback: "); Serial.print("Input change callback: ");