diff --git a/platformio.ini b/platformio.ini index 74c99c5..760a3ed 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,4 +18,5 @@ lib_deps = siwats/espmegapror3@^1.0.2 arduino-libraries/Arduino_BuiltIn@^1.0.0 dersimn/PubSubClientTools@^0.6 z3t0/IRremote@^4.2.0 + robtillaart/DHTNEW@^0.4.18 monitor_speed = 115200 \ No newline at end of file diff --git a/src/daikin_ir.hpp b/src/daikin_ir.hpp index 2ccf596..d3ae69a 100644 --- a/src/daikin_ir.hpp +++ b/src/daikin_ir.hpp @@ -4,22 +4,61 @@ #include #endif -extern const uint16_t daikin_ir_code_cool[3][10][750] = { +extern const uint16_t ir_code_cool[3][13][750] = { + // Fan Speed High + { + {0}, // 18C + {0}, // 19C + {0}, // 20C + {0}, // 21C + {0}, // 22C + {0}, // 23C + {0}, // 24C + {0}, // 25C + {0}, // 26C + {0}, // 27C + {0}, // 28C + {0}, // 29C + {0} // 30C + + }, + // Fan Speed Med + { + {0}, // 18C + {0}, // 19C + {0}, // 20C + {0}, // 21C + {0}, // 22C + {0}, // 23C + {0}, // 24C + {0}, // 25C + {0}, // 26C + {0}, // 27C + {0}, // 28C + {0}, // 29C + {0} // 30C + }, // Fan Speed Low { - {0}, // 16C - {0} // 17C - }, - // Fan Speed MED - { - {0}, // 16C - {0} // 17C + {0}, // 18C + {0}, // 19C + {0}, // 20C + {0}, // 21C + {0}, // 22C + {0}, // 23C + {0}, // 24C + {0}, // 25C + {0}, // 26C + {0}, // 27C + {0}, // 28C + {0}, // 29C + {0} // 30C }}; -extern const uint16_t daikin_ir_code_fan[3][750] = { +extern const uint16_t ir_code_fan[3][750] = { {0}, // LOW {0}, // MED {0} // HIGH }; -extern const uint16_t daikin_ir_code_off[750] = {0}; +extern const uint16_t ir_code_off[750] = {0}; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e5bca59..b9113c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include // Network Connectivity #define HOSTNAME "espmega-pro-r3" @@ -17,6 +18,7 @@ const IPAddress DNS(10, 192, 1, 1); const IPAddress MQTT_SERVER(192, 168, 0, 26); const int MQTT_PORT = 1883; #define MQTT_BASE_TOPIC "/espmega/ProR3" +char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate"; // #define MQTT_USE_AUTH #ifdef MQTT_USE_AUTH const char MQTT_USERNAME[] = "username"; @@ -39,12 +41,34 @@ const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, const float pwm_linear_scaling_c[PWM_COUNT] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; #define PWM_CYCLE_VALUES_COUNT 3 const int PWM_CYCLE_VALUES[PWM_CYCLE_VALUES_COUNT] = {50, 125, 255}; +char PWM_SET_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/set/state"; +char PWM_SET_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/set/value"; // Infrared Transciever -#define IR_RECIEVER_PIN 4 -#define IR_TRANSMITTER_PIN 15 +#define IR_RECIEVE_PIN 32 +#define IR_SEND_PIN 15 #define MARK_EXCESS_MICROS 20 -#define RAW_BUFFER_LENGTH 750 +#define RAW_BUFFER_LENGTH 750 + +// Air Conditioner Control +/* +Mode 0: Off, 1: Cool, 2: Fan +Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low +*/ +#define DHT22_PIN 17 +int ac_mode = 0; +int ac_fan_speed = 0; +int ac_temperature = 25; +#define AC_MAX_TEMPERATURE 30 +#define AC_MIN_TEMPERATURE 18 +char AC_SET_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/mode"; +char AC_SET_FAN_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/fan_speed"; +char AC_SET_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/set/temperature"; +char AC_MODE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/mode"; +char AC_FAN_TOPIC[75] = MQTT_BASE_TOPIC "/ac/fan_speed"; +char AC_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/temperature"; +char AC_ROOM_TEMPERATURE_TOPIC[75] = MQTT_BASE_TOPIC "/ac/room_temperature"; +char AC_HUMIDITY_TOPIC[75] = MQTT_BASE_TOPIC "/ac/humidity"; // Forward declaration void virtual_interrupt_loop(); @@ -68,6 +92,11 @@ void pwm_toggle(int id1, int id2); void pwm_cycle_value(int id); boolean pwm_group_state(int id1, int id2); +void publish_ac_state(); +void publish_env_state(); +void ac_state_callback(String topic, String message); +void ac_set_state(int mode, int temperature, int fan_speed); + void publish_input_states(); void publish_input_state(int id); void publish_input_state(int id, int state); @@ -80,8 +109,11 @@ WiFiClient eth; PubSubClient mqtt_client(MQTT_SERVER, 1883, eth); PubSubClientTools mqtt(mqtt_client); +DHTNEW env_sensor(DHT22_PIN); + Thread mqtt_reconnector = Thread(); -StaticThreadController<1> thread_controller(&mqtt_reconnector); +Thread environment_reporter = Thread(); +StaticThreadController<2> thread_controller(&mqtt_reconnector, &environment_reporter); void setup() { @@ -90,8 +122,8 @@ void setup() ESPMega_begin(); io_begin(); Serial.println("Initializing Infrared . . ."); - IrReceiver.begin(IR_RECIEVER_PIN); - IrSender.begin(IR_TRANSMITTER_PIN); + IrReceiver.begin(IR_RECIEVE_PIN); + IrSender.begin(IR_SEND_PIN); network_begin(); Serial.println("Initializing MQTT . . ."); mqtt_connect(); @@ -112,8 +144,11 @@ void loop() void io_begin() { Serial.println("Initializing I/O . . ."); + pinMode(IR_RECIEVE_PIN, INPUT_PULLUP); + pinMode(IR_SEND_PIN, OUTPUT); memset(pwm_states, 0, PWM_COUNT); - for(int i=0;i= AC_MIN_TEMPERATURE && new_temp <= AC_MAX_TEMPERATURE) + { + ac_set_state(ac_mode, new_temp, ac_fan_speed); } + } + else if (topic.compareTo(String(AC_SET_MODE_TOPIC)) == 0) + { + if (message.compareTo("off") == 0) + { + ac_set_state(0, ac_temperature, ac_fan_speed); + } + else if (message.compareTo("cool") == 0) + { + ac_set_state(1, ac_temperature, ac_fan_speed); + } + else if (message.compareTo("fan") == 0) + { + ac_set_state(2, ac_temperature, ac_fan_speed); + } + } + else if (topic.compareTo(String(AC_SET_FAN_TOPIC)) == 0) + { + if (message.compareTo("auto") == 0) + { + ac_set_state(ac_mode, ac_temperature, 0); + } + else if (message.compareTo("low") == 0) + { + ac_set_state(ac_mode, ac_temperature, 1); + } + else if (message.compareTo("med") == 0) + { + ac_set_state(ac_mode, ac_temperature, 2); + } + else if (message.compareTo("high") == 0) + { + ac_set_state(ac_mode, ac_temperature, 3); + } + } +} + +void ac_set_state(int mode, int temperature, int fan_speed) +{ + ac_mode = mode; + ac_temperature = temperature; + ac_fan_speed = fan_speed; + temperature -= AC_MIN_TEMPERATURE; + publish_ac_state(); + switch (mode) + { + case 0: + IrSender.sendRaw(ir_code_off, sizeof(ir_code_off) / sizeof(ir_code_off[0]), NEC_KHZ); + break; + case 1: + IrSender.sendRaw(ir_code_cool[fan_speed][temperature], sizeof(ir_code_cool[fan_speed][temperature]) / sizeof(ir_code_cool[fan_speed][0]), NEC_KHZ); + break; + case 2: + IrSender.sendRaw(ir_code_fan[fan_speed], sizeof(ir_code_fan[fan_speed]) / sizeof(ir_code_fan[fan_speed][0]), NEC_KHZ); + break; + } + Serial.println("IR SENT."); +} + +void publish_env_state() +{ + int errorCode = env_sensor.read(); + yield(); + switch (errorCode) + { + case DHTLIB_OK: + mqtt.publish(String(AC_ROOM_TEMPERATURE_TOPIC), String(env_sensor.getTemperature())); + mqtt_client.loop(); + mqtt.publish(String(AC_HUMIDITY_TOPIC), String(env_sensor.getHumidity())); + mqtt_client.loop(); + break; + default: + mqtt.publish(String(AC_ROOM_TEMPERATURE_TOPIC), "ERROR"); + mqtt_client.loop(); + mqtt.publish(String(AC_HUMIDITY_TOPIC), "ERROR"); + mqtt_client.loop(); + } } \ No newline at end of file