2023-08-25 18:38:17 +00:00
|
|
|
// #define USE_INTERRUPT
|
|
|
|
#include <ESPMegaPRO.h>
|
|
|
|
#include <ETH.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <PubSubClientTools.h>
|
|
|
|
#include <Thread.h>
|
|
|
|
#include <StaticThreadController.h>
|
2023-08-27 19:07:04 +00:00
|
|
|
#include <IRremote.hpp>
|
|
|
|
#include <daikin_ir.hpp>
|
2023-08-28 17:49:11 +00:00
|
|
|
#include <dhtnew.h>
|
2023-08-25 18:38:17 +00:00
|
|
|
|
|
|
|
// Network Connectivity
|
|
|
|
#define HOSTNAME "espmega-pro-r3"
|
|
|
|
const IPAddress IP(192, 168, 0, 210);
|
|
|
|
const IPAddress SUBNET(255, 255, 255, 0);
|
|
|
|
const IPAddress GATEWAY(192, 168, 0, 1);
|
|
|
|
const IPAddress DNS(10, 192, 1, 1);
|
|
|
|
const IPAddress MQTT_SERVER(192, 168, 0, 26);
|
|
|
|
const int MQTT_PORT = 1883;
|
2023-08-26 08:22:59 +00:00
|
|
|
#define MQTT_BASE_TOPIC "/espmega/ProR3"
|
2023-08-28 17:49:11 +00:00
|
|
|
char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate";
|
2023-08-25 18:38:17 +00:00
|
|
|
// #define MQTT_USE_AUTH
|
|
|
|
#ifdef MQTT_USE_AUTH
|
|
|
|
const char MQTT_USERNAME[] = "username";
|
|
|
|
const char MQTT_PASSWORD[] = "password";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Inputs
|
2023-08-26 08:22:59 +00:00
|
|
|
#define VINT_COUNT 16
|
2023-08-25 18:38:17 +00:00
|
|
|
const int DEBOUNCE_TIME_MS = 50;
|
2023-08-26 08:22:59 +00:00
|
|
|
const int virtual_interrupt_pins[VINT_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
|
|
|
int virtual_interupt_state[VINT_COUNT];
|
|
|
|
unsigned long virtual_interupt_timer[VINT_COUNT];
|
2023-08-25 18:38:17 +00:00
|
|
|
|
|
|
|
// Outputs
|
|
|
|
#define PWM_COUNT 16
|
|
|
|
const int pwm_pins[PWM_COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
|
|
|
int pwm_states[PWM_COUNT];
|
|
|
|
int pwm_values[PWM_COUNT];
|
|
|
|
const float pwm_linear_scaling_m[PWM_COUNT] = {1, 1, 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};
|
2023-08-28 17:49:11 +00:00
|
|
|
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";
|
2023-08-25 18:38:17 +00:00
|
|
|
|
2023-08-27 19:07:04 +00:00
|
|
|
// Infrared Transciever
|
2023-08-28 17:49:11 +00:00
|
|
|
#define IR_RECIEVE_PIN 32
|
|
|
|
#define IR_SEND_PIN 15
|
2023-08-27 19:07:04 +00:00
|
|
|
#define MARK_EXCESS_MICROS 20
|
2023-08-28 17:49:11 +00:00
|
|
|
#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";
|
2023-08-27 19:07:04 +00:00
|
|
|
|
2023-08-25 18:38:17 +00:00
|
|
|
// Forward declaration
|
|
|
|
void virtual_interrupt_loop();
|
|
|
|
void virtual_interrupt_callback(int pin, int state);
|
|
|
|
void network_begin();
|
|
|
|
void mqtt_connect();
|
|
|
|
void mqtt_subscribe();
|
|
|
|
void thread_initialization();
|
2023-08-26 08:22:59 +00:00
|
|
|
void pwm_state_callback(String topic, String message);
|
|
|
|
void pwm_value_callback(String topic, String message);
|
|
|
|
void state_request_callback(String topic, String message);
|
2023-08-26 05:53:25 +00:00
|
|
|
void io_begin();
|
2023-08-27 19:07:04 +00:00
|
|
|
void ir_loop();
|
2023-08-25 18:38:17 +00:00
|
|
|
|
|
|
|
void publish_pwm_states();
|
|
|
|
void publish_pwm_state(int id);
|
|
|
|
void pwm_set_state(int id, int state);
|
|
|
|
void pwm_set_value(int id, int value);
|
|
|
|
void pwm_toggle(int id);
|
|
|
|
void pwm_toggle(int id1, int id2);
|
|
|
|
void pwm_cycle_value(int id);
|
|
|
|
boolean pwm_group_state(int id1, int id2);
|
|
|
|
|
2023-08-28 17:49:11 +00:00
|
|
|
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);
|
|
|
|
|
2023-08-26 08:22:59 +00:00
|
|
|
void publish_input_states();
|
|
|
|
void publish_input_state(int id);
|
|
|
|
void publish_input_state(int id, int state);
|
|
|
|
|
|
|
|
char PWM_STATE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/state";
|
|
|
|
char PWM_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/value";
|
|
|
|
char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00";
|
|
|
|
|
2023-08-25 18:38:17 +00:00
|
|
|
WiFiClient eth;
|
|
|
|
PubSubClient mqtt_client(MQTT_SERVER, 1883, eth);
|
|
|
|
PubSubClientTools mqtt(mqtt_client);
|
|
|
|
|
2023-08-28 17:49:11 +00:00
|
|
|
DHTNEW env_sensor(DHT22_PIN);
|
|
|
|
|
2023-08-25 18:38:17 +00:00
|
|
|
Thread mqtt_reconnector = Thread();
|
2023-08-28 17:49:11 +00:00
|
|
|
Thread environment_reporter = Thread();
|
|
|
|
StaticThreadController<2> thread_controller(&mqtt_reconnector, &environment_reporter);
|
2023-08-25 18:38:17 +00:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println("ESPMega R3 Initializing . . .");
|
2023-08-26 05:53:25 +00:00
|
|
|
ESPMega_begin();
|
2023-08-26 10:28:21 +00:00
|
|
|
io_begin();
|
2023-08-27 19:07:04 +00:00
|
|
|
Serial.println("Initializing Infrared . . .");
|
2023-08-28 17:49:11 +00:00
|
|
|
IrReceiver.begin(IR_RECIEVE_PIN);
|
|
|
|
IrSender.begin(IR_SEND_PIN);
|
2023-08-25 18:38:17 +00:00
|
|
|
network_begin();
|
|
|
|
Serial.println("Initializing MQTT . . .");
|
|
|
|
mqtt_connect();
|
|
|
|
thread_initialization();
|
2023-08-26 08:22:59 +00:00
|
|
|
Serial.println("Initialization Completed.");
|
|
|
|
Serial.println("Jumping to User Code.");
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
virtual_interrupt_loop();
|
|
|
|
mqtt_client.loop();
|
|
|
|
ESPMega_loop();
|
2023-08-27 19:07:04 +00:00
|
|
|
ir_loop();
|
2023-08-26 05:53:25 +00:00
|
|
|
thread_controller.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void io_begin()
|
|
|
|
{
|
|
|
|
Serial.println("Initializing I/O . . .");
|
2023-08-28 17:49:11 +00:00
|
|
|
pinMode(IR_RECIEVE_PIN, INPUT_PULLUP);
|
|
|
|
pinMode(IR_SEND_PIN, OUTPUT);
|
2023-08-26 05:53:25 +00:00
|
|
|
memset(pwm_states, 0, PWM_COUNT);
|
2023-08-28 17:49:11 +00:00
|
|
|
for (int i = 0; i < PWM_COUNT; i++)
|
|
|
|
{
|
2023-08-26 10:28:21 +00:00
|
|
|
pwm_values[i] = 4095;
|
|
|
|
}
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void network_begin()
|
|
|
|
{
|
2023-08-26 05:53:25 +00:00
|
|
|
Serial.print("Initializing Network ");
|
2023-08-25 18:38:17 +00:00
|
|
|
ETH.begin();
|
2023-08-26 05:53:25 +00:00
|
|
|
ETH.setHostname(HOSTNAME);
|
|
|
|
ETH.config(IP, GATEWAY, SUBNET, DNS, DNS);
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
delay(2500);
|
|
|
|
Serial.print(" .");
|
|
|
|
}
|
|
|
|
Serial.println();
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_connect()
|
|
|
|
{
|
|
|
|
if (!mqtt_client.connected())
|
|
|
|
{
|
|
|
|
Serial.print("MQTT not connected, connecting . . .\n");
|
|
|
|
#ifdef MQTT_USE_AUTH
|
|
|
|
mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD);
|
|
|
|
#else
|
|
|
|
mqtt_client.connect(HOSTNAME);
|
|
|
|
#endif
|
|
|
|
if (mqtt_client.connected())
|
|
|
|
{
|
|
|
|
mqtt_subscribe();
|
|
|
|
Serial.print("MQTT connected\n");
|
2023-08-26 08:22:59 +00:00
|
|
|
publish_pwm_states();
|
|
|
|
publish_input_states();
|
2023-08-28 17:49:11 +00:00
|
|
|
publish_ac_state();
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.print("MQTT not connected, continuing in standalone mode\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_subscribe()
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
for (int i = 0; i < PWM_COUNT; i++)
|
|
|
|
{
|
|
|
|
PWM_SET_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((i - i % 10) / 10) + '0';
|
|
|
|
PWM_SET_VALUE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (i % 10) + '0';
|
|
|
|
PWM_SET_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((i - i % 10) / 10) + '0';
|
|
|
|
PWM_SET_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (i % 10) + '0';
|
|
|
|
mqtt.subscribe(PWM_SET_STATE_TOPIC, pwm_state_callback);
|
|
|
|
mqtt.subscribe(PWM_SET_VALUE_TOPIC, pwm_value_callback);
|
|
|
|
}
|
2023-08-28 17:49:11 +00:00
|
|
|
mqtt.subscribe(AC_SET_FAN_TOPIC, ac_state_callback);
|
|
|
|
mqtt.subscribe(AC_SET_TEMPERATURE_TOPIC, ac_state_callback);
|
|
|
|
mqtt.subscribe(AC_SET_MODE_TOPIC, ac_state_callback);
|
2023-08-26 08:22:59 +00:00
|
|
|
mqtt.subscribe(STATE_REQUEST_TOPIC, state_request_callback);
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void thread_initialization()
|
|
|
|
{
|
2023-08-26 05:53:25 +00:00
|
|
|
Serial.println("Initializing Threads . . .");
|
|
|
|
Serial.println("Initializing MQTT Thread . . .");
|
2023-08-25 18:38:17 +00:00
|
|
|
mqtt_reconnector.onRun(mqtt_connect);
|
|
|
|
mqtt_reconnector.setInterval(30000);
|
2023-08-28 17:49:11 +00:00
|
|
|
environment_reporter.onRun(publish_env_state);
|
|
|
|
environment_reporter.setInterval(5000);
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 08:22:59 +00:00
|
|
|
void pwm_state_callback(String topic, String message)
|
2023-08-25 18:38:17 +00:00
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
Serial.println("Recieved PWM State Message");
|
|
|
|
Serial.println(topic);
|
|
|
|
Serial.println(message);
|
2023-08-28 17:49:11 +00:00
|
|
|
int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0';
|
|
|
|
int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0';
|
2023-08-26 08:22:59 +00:00
|
|
|
int id = 10 * a + b;
|
2023-08-28 17:49:11 +00:00
|
|
|
if (message.compareTo("on") == 0)
|
2023-08-26 08:22:59 +00:00
|
|
|
{
|
|
|
|
Serial.printf("Setting pwm %d to on\n", id);
|
|
|
|
pwm_set_state(id, true);
|
|
|
|
}
|
2023-08-28 17:49:11 +00:00
|
|
|
else if (message.compareTo("off") == 0)
|
2023-08-26 08:22:59 +00:00
|
|
|
{
|
|
|
|
Serial.printf("Setting pwm %d to off\n", id);
|
|
|
|
pwm_set_state(id, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_value_callback(String topic, String message)
|
|
|
|
{
|
|
|
|
Serial.println("Recieved PWM Value Message");
|
|
|
|
Serial.println(topic);
|
|
|
|
Serial.println(message);
|
2023-08-28 17:49:11 +00:00
|
|
|
int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0';
|
|
|
|
int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0';
|
2023-08-26 08:22:59 +00:00
|
|
|
int id = 10 * a + b;
|
|
|
|
int value = message.toInt();
|
|
|
|
pwm_set_value(id, value);
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtual_interrupt_callback(int pin, int state)
|
|
|
|
{
|
2023-08-28 17:49:11 +00:00
|
|
|
publish_input_state(pin, state);
|
2023-08-26 05:53:25 +00:00
|
|
|
Serial.printf("Pin %d changed to %d\n", pin, state);
|
2023-08-28 17:49:11 +00:00
|
|
|
if (pin == 15)
|
|
|
|
{
|
2023-08-26 10:28:21 +00:00
|
|
|
pwm_toggle(4);
|
|
|
|
}
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void virtual_interrupt_loop()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
int current_pin_value = ESPMega_digitalRead(virtual_interrupt_pins[i]);
|
|
|
|
if (virtual_interupt_state[i] != current_pin_value)
|
|
|
|
{
|
|
|
|
if (millis() - virtual_interupt_timer[i] > DEBOUNCE_TIME_MS)
|
|
|
|
{
|
|
|
|
virtual_interupt_state[i] = current_pin_value;
|
|
|
|
virtual_interrupt_callback(i, current_pin_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
virtual_interupt_timer[i] = millis();
|
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void publish_pwm_states()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < PWM_COUNT; i++)
|
|
|
|
{
|
|
|
|
publish_pwm_state(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void publish_pwm_state(int id)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
int state = pwm_states[id];
|
|
|
|
int value = pwm_values[id];
|
|
|
|
PWM_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 4] = ((id - id % 10) / 10) + '0';
|
|
|
|
PWM_STATE_TOPIC[sizeof(MQTT_BASE_TOPIC) + 5] = (id % 10) + '0';
|
2023-08-25 18:38:17 +00:00
|
|
|
if (state == 1)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
mqtt_client.publish(PWM_STATE_TOPIC, "on");
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
else if (state == 0)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
mqtt_client.publish(PWM_STATE_TOPIC, "off");
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
2023-08-26 08:22:59 +00:00
|
|
|
mqtt.publish(String(PWM_VALUE_TOPIC), String(value));
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_set_state(int id, int state)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
if (state != pwm_states[id])
|
2023-08-25 18:38:17 +00:00
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
pwm_states[id] = state;
|
|
|
|
int pwm_value = pwm_values[id];
|
|
|
|
ESPMega_analogWrite(pwm_pins[id], state * (int)(pwm_linear_scaling_m[id] * pwm_value + pwm_linear_scaling_c[id]));
|
2023-08-25 18:38:17 +00:00
|
|
|
publish_pwm_state(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_set_value(int id, int value)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
pwm_values[id] = value;
|
|
|
|
int pwm_state = pwm_states[id];
|
|
|
|
ESPMega_analogWrite(pwm_pins[id], pwm_state * (int)(pwm_linear_scaling_m[id] * value + pwm_linear_scaling_c[id]));
|
2023-08-25 18:38:17 +00:00
|
|
|
publish_pwm_state(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_toggle(int id)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
int state = !pwm_states[id];
|
2023-08-25 18:38:17 +00:00
|
|
|
pwm_set_state(id, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_toggle(int id1, int id2)
|
|
|
|
{
|
|
|
|
boolean state = pwm_group_state(id1, id2);
|
|
|
|
if (state)
|
|
|
|
{
|
|
|
|
pwm_set_state(id1, 0);
|
|
|
|
pwm_set_state(id2, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwm_set_state(id1, 1);
|
|
|
|
pwm_set_state(id2, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean pwm_group_state(int id1, int id2)
|
|
|
|
{
|
|
|
|
int state1 = pwm_states[id1 - 1], state2 = pwm_states[id2 - 1];
|
|
|
|
if (state1 || state2)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pwm_cycle_value(int id)
|
|
|
|
{
|
2023-08-26 08:22:59 +00:00
|
|
|
int state = pwm_states[id];
|
|
|
|
int value = pwm_values[id];
|
2023-08-25 18:38:17 +00:00
|
|
|
if (state == 1)
|
|
|
|
for (int i = 0; i < PWM_CYCLE_VALUES_COUNT; i++)
|
|
|
|
{
|
|
|
|
if (PWM_CYCLE_VALUES[i] == value)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
pwm_set_value(id, PWM_CYCLE_VALUES[i - 1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwm_set_state(id, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pwm_set_state(id, 1);
|
|
|
|
pwm_set_value(id, PWM_CYCLE_VALUES[PWM_CYCLE_VALUES_COUNT - 1]);
|
2023-08-26 08:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void publish_input_states()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < VINT_COUNT; i++)
|
|
|
|
{
|
|
|
|
publish_input_state(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void publish_input_state(int id)
|
|
|
|
{
|
|
|
|
int state = ESPMega_digitalRead(virtual_interrupt_pins[id]);
|
|
|
|
publish_input_state(id, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void publish_input_state(int id, int state)
|
|
|
|
{
|
|
|
|
char INPUTS_TOPIC[75] = MQTT_BASE_TOPIC "/input/00";
|
|
|
|
INPUTS_TOPIC[sizeof(MQTT_BASE_TOPIC) + 6] = ((id - id % 10) / 10) + '0';
|
|
|
|
INPUTS_TOPIC[sizeof(MQTT_BASE_TOPIC) + 7] = (id % 10) + '0';
|
|
|
|
mqtt.publish(String(INPUTS_TOPIC), state ? "1" : "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
void state_request_callback(String topic, String message)
|
|
|
|
{
|
|
|
|
publish_input_states();
|
|
|
|
publish_pwm_states();
|
2023-08-28 17:49:11 +00:00
|
|
|
publish_ac_state();
|
2023-08-27 19:07:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 17:49:11 +00:00
|
|
|
void ir_loop()
|
|
|
|
{
|
|
|
|
if (IrReceiver.decode())
|
|
|
|
{
|
|
|
|
Serial.println();
|
|
|
|
IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, false);
|
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
IrReceiver.resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void publish_ac_state()
|
|
|
|
{
|
|
|
|
String temp = "";
|
|
|
|
switch (ac_mode)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
temp = "off";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
temp = "cool";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
temp = "fan";
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mqtt.publish(String(AC_MODE_TOPIC), temp);
|
|
|
|
mqtt.publish(String(AC_TEMPERATURE_TOPIC), String(ac_temperature));
|
|
|
|
switch (ac_fan_speed)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
temp = "auto";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
temp = "high";
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
temp = "med";
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
temp = "low";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mqtt.publish(String(AC_FAN_TOPIC), temp);
|
|
|
|
Serial.println("AC State Published.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ac_state_callback(String topic, String message)
|
|
|
|
{
|
|
|
|
if (topic.compareTo(String(AC_SET_TEMPERATURE_TOPIC)) == 0)
|
|
|
|
{
|
|
|
|
int new_temp = message.toInt();
|
|
|
|
if (new_temp >= 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);
|
2023-08-27 19:07:04 +00:00
|
|
|
}
|
2023-08-28 17:49:11 +00:00
|
|
|
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();
|
|
|
|
}
|
2023-08-25 18:38:17 +00:00
|
|
|
}
|