Compare commits

..

3 Commits

5 changed files with 176 additions and 36 deletions

View File

@ -2,7 +2,10 @@
// OS Configuration // OS Configuration
// #define FASTBOOT // #define FASTBOOT
#ifndef ESPMEGA_REV
#define ESPMEGA_REV "ESPMega PRO R3.3b" #define ESPMEGA_REV "ESPMega PRO R3.3b"
#endif
// Network Connectivity // Network Connectivity
char HOSTNAME[15]; char HOSTNAME[15];
@ -12,7 +15,9 @@ IPAddress GATEWAY(0, 0, 0, 0);
IPAddress DNS(0, 0, 0, 0); IPAddress DNS(0, 0, 0, 0);
IPAddress MQTT_SERVER(0, 0, 0, 0); IPAddress MQTT_SERVER(0, 0, 0, 0);
uint16_t MQTT_PORT = 0; uint16_t MQTT_PORT = 0;
#ifdef ENABLE_WEBUI
WebServer otaserver(80); WebServer otaserver(80);
#endif
bool standalone = true; bool standalone = true;
// #define MQTT_BASE_TOPIC "/espmega/ProR3" // #define MQTT_BASE_TOPIC "/espmega/ProR3"
char MQTT_BASE_TOPIC[20]; char MQTT_BASE_TOPIC[20];
@ -22,8 +27,10 @@ bool MQTT_USE_AUTH = false;
char MQTT_USERNAME[32]; char MQTT_USERNAME[32];
char MQTT_PASSWORD[32]; char MQTT_PASSWORD[32];
uint8_t utc_offset = 7; uint8_t utc_offset = 7;
#ifdef ENABLE_CLIMATE_MODULE
float current_room_temp = 0; float current_room_temp = 0;
float current_room_humid = 0; float current_room_humid = 0;
#endif
// Inputs // Inputs
#define VINT_COUNT 16 #define VINT_COUNT 16
@ -47,16 +54,19 @@ const int PWM_CYCLE_VALUES[PWM_CYCLE_VALUES_COUNT] = {50, 125, 255};
char PWM_SET_STATE_TOPIC[70]; char PWM_SET_STATE_TOPIC[70];
char PWM_SET_VALUE_TOPIC[70]; char PWM_SET_VALUE_TOPIC[70];
#ifdef ENABLE_INTERNAL_LCD
// LCD // LCD
int lcd_current_page = 1; int lcd_current_page = 1;
int lcd_pwmAdj_id = 0; int lcd_pwmAdj_id = 0;
EasyNex panel(Serial); EasyNex panel(Serial);
#endif
// Air Conditioner Control // Air Conditioner Control
/* /*
Mode 0: Off, 1: Cool, 2: Fan Mode 0: Off, 1: Cool, 2: Fan
Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low Fan Speed 0: Auto, 1: High, 2: Mid, 3: Low
*/ */
#ifdef ENABLE_CLIMATE_MODULE
#define DHT22_PIN 32 #define DHT22_PIN 32
uint8_t ac_mode = 0; uint8_t ac_mode = 0;
uint8_t ac_fan_speed = 0; uint8_t ac_fan_speed = 0;
@ -69,6 +79,7 @@ char AC_FAN_TOPIC[75];
char AC_TEMPERATURE_TOPIC[75]; char AC_TEMPERATURE_TOPIC[75];
char AC_ROOM_TEMPERATURE_TOPIC[75]; char AC_ROOM_TEMPERATURE_TOPIC[75];
char AC_HUMIDITY_TOPIC[75]; char AC_HUMIDITY_TOPIC[75];
#endif
// EEPROM ADDRESS // EEPROM ADDRESS
#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes #define EEPROM_ADDRESS_AC_MODE 0 // 01bytes
@ -96,7 +107,9 @@ WiFiClient eth;
PubSubClient mqtt_client(MQTT_SERVER, 1883, eth); PubSubClient mqtt_client(MQTT_SERVER, 1883, eth);
PubSubClientTools mqtt(mqtt_client); PubSubClientTools mqtt(mqtt_client);
#ifdef ENABLE_CLIMATE_MODULE
DHTNEW env_sensor(DHT22_PIN); DHTNEW env_sensor(DHT22_PIN);
#endif
Thread mqtt_reconnector = Thread(); Thread mqtt_reconnector = Thread();
Thread environment_reporter = Thread(); Thread environment_reporter = Thread();
@ -104,9 +117,11 @@ Thread eeprom_pwm_updater = Thread();
Thread user_timer_tick = Thread(); Thread user_timer_tick = Thread();
StaticThreadController<4> thread_controller(&mqtt_reconnector, &environment_reporter, &eeprom_pwm_updater, &user_timer_tick); StaticThreadController<4> thread_controller(&mqtt_reconnector, &environment_reporter, &eeprom_pwm_updater, &user_timer_tick);
#ifdef ENABLE_INTERNAL_LCD
Thread top_bar_updater = Thread(); Thread top_bar_updater = Thread();
Thread page_updater = Thread(); Thread page_updater = Thread();
StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater); StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater);
#endif
void setup() void setup()
{ {
@ -114,15 +129,19 @@ void setup()
#ifdef ENABLE_EXTERNAL_LCD #ifdef ENABLE_EXTERNAL_LCD
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2); Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
#endif #endif
#ifdef ENABLE_INTERNAL_LCD
panel.begin(115200); panel.begin(115200);
#endif
Serial.println("ESPMega R3 Initializing"); Serial.println("ESPMega R3 Initializing");
ESPMega_begin(); ESPMega_begin();
io_begin(); io_begin();
eeprom_retrieve_init(); eeprom_retrieve_init();
user_pre_init(); user_pre_init();
#ifdef ENABLE_INTERNAL_LCD
lcd_send_stop_bit(); lcd_send_stop_bit();
lcd_init(); lcd_init();
lcd_begin(); lcd_begin();
#endif
check_boot_reset(); check_boot_reset();
#ifdef ENABLE_EXTERNAL_LCD #ifdef ENABLE_EXTERNAL_LCD
Serial2.print("rest"); Serial2.print("rest");
@ -132,9 +151,11 @@ void setup()
#endif #endif
lcd_send_command("boot_state.txt=\"Core Initializing . . .\""); lcd_send_command("boot_state.txt=\"Core Initializing . . .\"");
Serial.println("Initializing Infrared . . ."); Serial.println("Initializing Infrared . . .");
#ifdef ENABLE_IR_MODULE
lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\""); lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\"");
IrReceiver.begin(IR_RECIEVE_PIN); IrReceiver.begin(IR_RECIEVE_PIN);
IrSender.begin(IR_SEND_PIN); IrSender.begin(IR_SEND_PIN);
#endif
lcd_send_command("boot_state.txt=\"Network Initializing . . .\""); lcd_send_command("boot_state.txt=\"Network Initializing . . .\"");
network_begin(); network_begin();
lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\""); lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\"");
@ -143,7 +164,9 @@ void setup()
mqtt_connect(); mqtt_connect();
lcd_send_command("boot_state.txt=\"Threads Initializing . . .\""); lcd_send_command("boot_state.txt=\"Threads Initializing . . .\"");
thread_initialization(); thread_initialization();
#ifdef ENABLE_WEBUI
ota_begin(); ota_begin();
#endif
Serial.println("Initialization Completed."); Serial.println("Initialization Completed.");
Serial.println("Jumping to User Code."); Serial.println("Jumping to User Code.");
user_init(); user_init();
@ -155,20 +178,29 @@ void loop()
virtual_interrupt_loop(); virtual_interrupt_loop();
mqtt_client.loop(); mqtt_client.loop();
ESPMega_loop(); ESPMega_loop();
#ifdef ENABLE_IR_MODULE
ir_loop(); ir_loop();
#endif
thread_controller.run(); thread_controller.run();
#ifdef ENABLE_INTERNAL_LCD
lcd_loop(); lcd_loop();
#endif
user_loop(); user_loop();
#ifdef ENABLE_WEBUI
otaserver.handleClient(); otaserver.handleClient();
#endif
} }
void eeprom_retrieve_init() void eeprom_retrieve_init()
{ {
// EEPROM Data Retrival // EEPROM Data Retrival
#ifdef ENABLE_CLIMATE_MODULE
ac_mode = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_MODE); ac_mode = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_MODE);
ac_temperature = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_TEMPERATURE); ac_temperature = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_TEMPERATURE);
ac_fan_speed = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_FAN_SPEED); ac_fan_speed = ESPMega_FRAM.read8(EEPROM_ADDRESS_AC_FAN_SPEED);
#endif
// EEPROM Data Retrival Validation // EEPROM Data Retrival Validation
#ifdef ENABLE_CLIMATE_MODULE
if (ac_mode > 2) if (ac_mode > 2)
{ {
ac_mode = 0; ac_mode = 0;
@ -185,6 +217,7 @@ void eeprom_retrieve_init()
ESPMega_FRAM.write8(EEPROM_ADDRESS_AC_TEMPERATURE, ac_fan_speed); ESPMega_FRAM.write8(EEPROM_ADDRESS_AC_TEMPERATURE, ac_fan_speed);
} }
ac_set_state(ac_mode, ac_temperature, ac_fan_speed); ac_set_state(ac_mode, ac_temperature, ac_fan_speed);
#endif
ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_STATE, pwm_states_eeprom, 16); ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_STATE, pwm_states_eeprom, 16);
memcpy(pwm_states, pwm_states_eeprom, 16); memcpy(pwm_states, pwm_states_eeprom, 16);
ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_VALUE, pwm_values_eeprom, 32); ESPMega_FRAM.read(EEPROM_ADDRESS_PWM_VALUE, pwm_values_eeprom, 32);
@ -219,6 +252,7 @@ void eeprom_retrieve_init()
strcat(PWM_SET_STATE_TOPIC, "/pwm/00/set/state"); strcat(PWM_SET_STATE_TOPIC, "/pwm/00/set/state");
memcpy(PWM_SET_VALUE_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(PWM_SET_VALUE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_SET_VALUE_TOPIC, "/pwm/00/set/value"); strcat(PWM_SET_VALUE_TOPIC, "/pwm/00/set/value");
#ifdef ENABLE_CLIMATE_MODULE
memcpy(AC_SET_MODE_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(AC_SET_MODE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_SET_MODE_TOPIC, "/ac/set/mode"); strcat(AC_SET_MODE_TOPIC, "/ac/set/mode");
memcpy(AC_SET_FAN_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(AC_SET_FAN_TOPIC, MQTT_BASE_TOPIC, 20);
@ -235,6 +269,7 @@ void eeprom_retrieve_init()
strcat(AC_ROOM_TEMPERATURE_TOPIC, "/ac/room_temperature"); strcat(AC_ROOM_TEMPERATURE_TOPIC, "/ac/room_temperature");
memcpy(AC_HUMIDITY_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(AC_HUMIDITY_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(AC_HUMIDITY_TOPIC, "/ac/humidity"); strcat(AC_HUMIDITY_TOPIC, "/ac/humidity");
#endif
memcpy(PWM_STATE_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(PWM_STATE_TOPIC, MQTT_BASE_TOPIC, 20);
strcat(PWM_STATE_TOPIC, "/pwm/00/state"); strcat(PWM_STATE_TOPIC, "/pwm/00/state");
memcpy(PWM_VALUE_TOPIC, MQTT_BASE_TOPIC, 20); memcpy(PWM_VALUE_TOPIC, MQTT_BASE_TOPIC, 20);
@ -243,6 +278,7 @@ void eeprom_retrieve_init()
strcat(INPUTS_TOPIC, "/input/00"); strcat(INPUTS_TOPIC, "/input/00");
} }
#ifdef ENABLE_WEBUI
void ota_begin() void ota_begin()
{ {
otaserver.on("/", HTTP_GET, []() otaserver.on("/", HTTP_GET, []()
@ -319,9 +355,7 @@ void ota_begin()
set_mqtt_useauth(use_auth); set_mqtt_useauth(use_auth);
otaserver.send(200, "text/html", configbuffer); otaserver.send(200, "text/html", configbuffer);
delay(500); delay(500);
ESP.restart(); ESP.restart(); });
});
otaserver.on( otaserver.on(
"/update", HTTP_POST, []() "/update", HTTP_POST, []()
@ -338,12 +372,16 @@ void ota_begin()
Serial.println(upload.currentSize); Serial.println(upload.currentSize);
String otafiletxt = "Downloading File : " + upload.filename; String otafiletxt = "Downloading File : " + upload.filename;
#ifdef ENABLE_INTERNAL_LCD
lcd_send_stop_bit(); lcd_send_stop_bit();
panel.writeStr("otatxt.txt", otafiletxt); panel.writeStr("otatxt.txt", otafiletxt);
#endif
Serial.printf("Update: %s\n", upload.filename.c_str()); Serial.printf("Update: %s\n", upload.filename.c_str());
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) if (!Update.begin(UPDATE_SIZE_UNKNOWN))
{ {
#ifdef ENABLE_INTERNAL_LCD
panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . ."); panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . .");
#endif
Update.printError(Serial); Update.printError(Serial);
} }
} }
@ -351,7 +389,9 @@ void ota_begin()
{ {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
{ {
#ifdef ENABLE_INTERNAL_LCD
panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . ."); panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . .");
#endif
Update.printError(Serial); Update.printError(Serial);
} }
if (upload.currentSize != 0 && upload.totalSize != 0) if (upload.currentSize != 0 && upload.totalSize != 0)
@ -360,32 +400,41 @@ void ota_begin()
uint32_t totalsize_kb = upload.totalSize / 1000; uint32_t totalsize_kb = upload.totalSize / 1000;
uint32_t upload_pct = 100 * upload.totalSize / 1000000; uint32_t upload_pct = 100 * upload.totalSize / 1000000;
String otafiletxt = "Downloading File : " + upload.filename + " (" + String(totalsize_kb) + "KB)"; String otafiletxt = "Downloading File : " + upload.filename + " (" + String(totalsize_kb) + "KB)";
#ifdef ENABLE_INTERNAL_LCD
panel.writeNum("prog.val", upload_pct); panel.writeNum("prog.val", upload_pct);
panel.writeStr("otatxt.txt", otafiletxt); panel.writeStr("otatxt.txt", otafiletxt);
#endif
} }
} }
else if (upload.status == UPLOAD_FILE_END) else if (upload.status == UPLOAD_FILE_END)
{ {
if (Update.end(true)) if (Update.end(true))
{ {
#ifdef ENABLE_INTERNAL_LCD
panel.writeStr("otatxt.txt", "Update Completed, Rebooting . . ."); panel.writeStr("otatxt.txt", "Update Completed, Rebooting . . .");
#endif
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize); Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
} }
else else
{ {
#ifdef ENABLE_INTERNAL_LCD
panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . ."); panel.writeStr("otatxt.txt", "Update Failed, Rebooting . . .");
#endif
Update.printError(Serial); Update.printError(Serial);
} }
} }
}); });
otaserver.begin(); otaserver.begin();
} }
#endif
void io_begin() void io_begin()
{ {
Serial.println("Initializing I/O . . ."); Serial.println("Initializing I/O . . .");
#ifdef ENABLE_IR_MODULE
pinMode(IR_RECIEVE_PIN, INPUT_PULLUP); pinMode(IR_RECIEVE_PIN, INPUT_PULLUP);
pinMode(IR_SEND_PIN, OUTPUT); pinMode(IR_SEND_PIN, OUTPUT);
#endif
} }
void network_begin() void network_begin()
@ -431,7 +480,9 @@ void mqtt_connect()
lcd_send_stop_bit(); lcd_send_stop_bit();
publish_pwm_states(); publish_pwm_states();
publish_input_states(); publish_input_states();
#ifdef ENABLE_CLIMATE_MODULE
publish_ac_state(); publish_ac_state();
#endif
mqtt_connected_user_callback(); mqtt_connected_user_callback();
standalone = false; standalone = false;
ESPMega_updateTimeFromNTP(); ESPMega_updateTimeFromNTP();
@ -443,8 +494,10 @@ void mqtt_connect()
lcd_send_stop_bit(); lcd_send_stop_bit();
} }
lcd_send_stop_bit(); lcd_send_stop_bit();
#ifdef ENABLE_INTERNAL_LCD
lcd_refresh(); lcd_refresh();
lcd_top_bar_update(); lcd_top_bar_update();
#endif
} }
} }
@ -459,9 +512,11 @@ void mqtt_subscribe()
mqtt.subscribe(PWM_SET_STATE_TOPIC, pwm_state_callback); mqtt.subscribe(PWM_SET_STATE_TOPIC, pwm_state_callback);
mqtt.subscribe(PWM_SET_VALUE_TOPIC, pwm_value_callback); mqtt.subscribe(PWM_SET_VALUE_TOPIC, pwm_value_callback);
} }
#ifdef ENABLE_CLIMATE_MODULE
mqtt.subscribe(AC_SET_FAN_TOPIC, ac_state_callback); mqtt.subscribe(AC_SET_FAN_TOPIC, ac_state_callback);
mqtt.subscribe(AC_SET_TEMPERATURE_TOPIC, ac_state_callback); mqtt.subscribe(AC_SET_TEMPERATURE_TOPIC, ac_state_callback);
mqtt.subscribe(AC_SET_MODE_TOPIC, ac_state_callback); mqtt.subscribe(AC_SET_MODE_TOPIC, ac_state_callback);
#endif
mqtt.subscribe(STATE_REQUEST_TOPIC, state_request_callback); mqtt.subscribe(STATE_REQUEST_TOPIC, state_request_callback);
} }
@ -471,8 +526,10 @@ void thread_initialization()
Serial.println("Initializing MQTT Thread . . ."); Serial.println("Initializing MQTT Thread . . .");
mqtt_reconnector.onRun(mqtt_connect); mqtt_reconnector.onRun(mqtt_connect);
mqtt_reconnector.setInterval(15000); mqtt_reconnector.setInterval(15000);
#ifdef ENABLE_CLIMATE_MODULE
environment_reporter.onRun(publish_env_state); environment_reporter.onRun(publish_env_state);
environment_reporter.setInterval(5000); environment_reporter.setInterval(5000);
#endif
eeprom_pwm_updater.onRun(eeprom_pwm_update); eeprom_pwm_updater.onRun(eeprom_pwm_update);
eeprom_pwm_updater.setInterval(1000); eeprom_pwm_updater.setInterval(1000);
user_timer_tick.onRun(timer_tick_callback); user_timer_tick.onRun(timer_tick_callback);
@ -507,9 +564,10 @@ void virtual_interrupt_callback(int pin, int state)
{ {
publish_input_state(pin, state); publish_input_state(pin, state);
// Serial.printf("Pin %d changed to %d\n", pin, state); #ifdef ENABLE_INTERNAL_LCD
if (lcd_current_page == 2) if (lcd_current_page == 2)
panel.writeNum("I" + String(pin) + ".val", state); panel.writeNum("I" + String(pin) + ".val", state);
#endif
virtual_interrupt_user_callback(pin, state); virtual_interrupt_user_callback(pin, state);
} }
@ -568,10 +626,12 @@ void pwm_set_state(int id, int state)
pwm_states[id] = state; pwm_states[id] = state;
int pwm_value = pwm_values[id]; 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])); ESPMega_analogWrite(pwm_pins[id], state * (int)(pwm_linear_scaling_m[id] * pwm_value + pwm_linear_scaling_c[id]));
#ifdef ENABLE_INTERNAL_LCD
if (lcd_current_page == 3) if (lcd_current_page == 3)
panel.writeNum("j" + String(id) + ".ppic", pwm_states[id] ? 33 : 48); panel.writeNum("j" + String(id) + ".ppic", pwm_states[id] ? 33 : 48);
else if (lcd_current_page == 5 && id == lcd_pwmAdj_id) else if (lcd_current_page == 5 && id == lcd_pwmAdj_id)
panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF"); panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF");
#endif
publish_pwm_state(id); publish_pwm_state(id);
pwm_changed_user_callback(id); pwm_changed_user_callback(id);
} }
@ -582,10 +642,12 @@ void pwm_set_value(int id, int value)
pwm_values[id] = value; pwm_values[id] = value;
int pwm_state = pwm_states[id]; 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])); ESPMega_analogWrite(pwm_pins[id], pwm_state * (int)(pwm_linear_scaling_m[id] * value + pwm_linear_scaling_c[id]));
#ifdef ENABLE_INTERNAL_LCD
if (lcd_current_page == 3) if (lcd_current_page == 3)
panel.writeNum("j" + String(id) + ".val", int(value / 4095.0 * 100.0)); panel.writeNum("j" + String(id) + ".val", int(value / 4095.0 * 100.0));
else if (lcd_current_page == 5 && id == lcd_pwmAdj_id) else if (lcd_current_page == 5 && id == lcd_pwmAdj_id)
panel.writeNum("pwm_value.val", pwm_values[lcd_pwmAdj_id]); panel.writeNum("pwm_value.val", pwm_values[lcd_pwmAdj_id]);
#endif
publish_pwm_state(id); publish_pwm_state(id);
pwm_changed_user_callback(id); pwm_changed_user_callback(id);
} }
@ -668,22 +730,23 @@ void state_request_callback(String topic, String message)
{ {
publish_input_states(); publish_input_states();
publish_pwm_states(); publish_pwm_states();
#ifdef ENABLE_CLIMATE_MODULE
publish_ac_state(); publish_ac_state();
#endif
user_state_request_callback(); user_state_request_callback();
} }
#ifdef ENABLE_IR_MODULE
void ir_loop() void ir_loop()
{ {
if (IrReceiver.decode()) if (IrReceiver.decode())
{ {
// Serial.println();
// IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, false);
// Serial.println();
// Serial.println();
IrReceiver.resume(); IrReceiver.resume();
} }
} }
#endif
#ifdef ENABLE_CLIMATE_MODULE
void publish_ac_state() void publish_ac_state()
{ {
String temp = ""; String temp = "";
@ -776,7 +839,7 @@ void ac_set_state(int mode, int temperature, int fan_speed)
ac_temperature = AC_MAX_TEMPERATURE; ac_temperature = AC_MAX_TEMPERATURE;
ac_fan_speed = fan_speed; ac_fan_speed = fan_speed;
temperature -= AC_MIN_TEMPERATURE; temperature -= AC_MIN_TEMPERATURE;
#ifdef ENABLE_INTERNAL_LCD
if (lcd_current_page == 4) if (lcd_current_page == 4)
{ {
lcd_ac_refresh_fan(); lcd_ac_refresh_fan();
@ -786,6 +849,7 @@ void ac_set_state(int mode, int temperature, int fan_speed)
else else
panel.writeStr("temp.txt", "--C"); panel.writeStr("temp.txt", "--C");
} }
#endif
publish_ac_state(); publish_ac_state();
uint8_t ac_datablock[3] = {ac_mode, ac_temperature, ac_fan_speed}; uint8_t ac_datablock[3] = {ac_mode, ac_temperature, ac_fan_speed};
ESPMega_FRAM.write(0, ac_datablock, 3); ESPMega_FRAM.write(0, ac_datablock, 3);
@ -817,6 +881,7 @@ void publish_env_state()
mqtt_client.loop(); mqtt_client.loop();
mqtt.publish(String(AC_HUMIDITY_TOPIC), String(current_room_humid)); mqtt.publish(String(AC_HUMIDITY_TOPIC), String(current_room_humid));
mqtt_client.loop(); mqtt_client.loop();
#ifdef ENABLE_INTERNAL_LCD
if (lcd_current_page == 4) if (lcd_current_page == 4)
{ {
Serial.printf("roomtemp.txt=\"%.01fC\"", current_room_temp); Serial.printf("roomtemp.txt=\"%.01fC\"", current_room_temp);
@ -824,6 +889,7 @@ void publish_env_state()
Serial.printf("roomhumid.txt=\"%d%%\"", (int)current_room_humid); Serial.printf("roomhumid.txt=\"%d%%\"", (int)current_room_humid);
lcd_send_stop_bit(); lcd_send_stop_bit();
} }
#endif
break; break;
default: default:
mqtt.publish(String(AC_ROOM_TEMPERATURE_TOPIC), "ERROR"); mqtt.publish(String(AC_ROOM_TEMPERATURE_TOPIC), "ERROR");
@ -832,7 +898,9 @@ void publish_env_state()
mqtt_client.loop(); mqtt_client.loop();
} }
} }
#endif
#ifdef ENABLE_INTERNAL_LCD
void lcd_begin() void lcd_begin()
{ {
top_bar_updater.onRun(lcd_top_bar_update); top_bar_updater.onRun(lcd_top_bar_update);
@ -886,6 +954,7 @@ void lcd_refresh()
} }
break; break;
case 4: case 4:
#ifdef ENABLE_CLIMATE_MODULE
Serial.printf("roomtemp.txt=\"%.01fC\"", current_room_temp); Serial.printf("roomtemp.txt=\"%.01fC\"", current_room_temp);
lcd_send_stop_bit(); lcd_send_stop_bit();
Serial.printf("roomhumid.txt=\"%d%%\"", (int)current_room_humid); Serial.printf("roomhumid.txt=\"%d%%\"", (int)current_room_humid);
@ -896,6 +965,9 @@ void lcd_refresh()
panel.writeStr("temp.txt", "--C"); panel.writeStr("temp.txt", "--C");
lcd_ac_refresh_fan(); lcd_ac_refresh_fan();
lcd_ac_refresh_mode(); lcd_ac_refresh_mode();
#else
lcd_send_command("page climate_nomod");
#endif
case 5: case 5:
panel.writeStr("pwm_id.txt", String("P") + String(lcd_pwmAdj_id)); panel.writeStr("pwm_id.txt", String("P") + String(lcd_pwmAdj_id));
panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF"); panel.writeStr("pwm_state.txt", pwm_states[lcd_pwmAdj_id] ? "ON" : "OFF");
@ -929,7 +1001,7 @@ void lcd_top_bar_update()
panel.writeNum("server.pic", standalone ? 4 : 5); panel.writeNum("server.pic", standalone ? 4 : 5);
panel.writeNum("lan.pic", ETH.linkUp() ? 3 : 2); panel.writeNum("lan.pic", ETH.linkUp() ? 3 : 2);
} }
#ifdef ENABLE_CLIMATE_MODULE
void lcd_ac_refresh_mode() void lcd_ac_refresh_mode()
{ {
// auto high mid low // auto high mid low
@ -945,6 +1017,7 @@ void lcd_ac_refresh_fan()
panel.writeNum("fan_mid.pic", ac_fan_speed == 2 ? 20 : 21); panel.writeNum("fan_mid.pic", ac_fan_speed == 2 ? 20 : 21);
panel.writeNum("fan_high.pic", ac_fan_speed == 1 ? 16 : 17); panel.writeNum("fan_high.pic", ac_fan_speed == 1 ? 16 : 17);
} }
#endif
void trigger0() void trigger0()
{ {
@ -975,54 +1048,70 @@ void trigger3()
lcd_send_stop_bit(); lcd_send_stop_bit();
pwm_set_value(lcd_pwmAdj_id, value); pwm_set_value(lcd_pwmAdj_id, value);
} }
void trigger4() void trigger4()
{ {
#ifdef ENABLE_CLIMATE_MODULE
if (ac_temperature < AC_MAX_TEMPERATURE && ac_mode != 2) if (ac_temperature < AC_MAX_TEMPERATURE && ac_mode != 2)
ac_set_state(ac_mode, ac_temperature + 1, ac_fan_speed); ac_set_state(ac_mode, ac_temperature + 1, ac_fan_speed);
#endif
} }
void trigger5() void trigger5()
{ {
#ifdef ENABLE_CLIMATE_MODULE
if (ac_temperature > AC_MIN_TEMPERATURE && ac_mode != 2) if (ac_temperature > AC_MIN_TEMPERATURE && ac_mode != 2)
ac_set_state(ac_mode, ac_temperature - 1, ac_fan_speed); ac_set_state(ac_mode, ac_temperature - 1, ac_fan_speed);
#endif
} }
void trigger6() void trigger6()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(ac_mode, ac_temperature, 0); ac_set_state(ac_mode, ac_temperature, 0);
#endif
} }
void trigger7() void trigger7()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(ac_mode, ac_temperature, 3); ac_set_state(ac_mode, ac_temperature, 3);
#endif
} }
void trigger8() void trigger8()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(ac_mode, ac_temperature, 2); ac_set_state(ac_mode, ac_temperature, 2);
#endif
} }
void trigger9() void trigger9()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(ac_mode, ac_temperature, 1); ac_set_state(ac_mode, ac_temperature, 1);
#endif
} }
void trigger10() void trigger10()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(1, ac_temperature, ac_fan_speed); ac_set_state(1, ac_temperature, ac_fan_speed);
#endif
} }
void trigger11() void trigger11()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(2, ac_temperature, ac_fan_speed); ac_set_state(2, ac_temperature, ac_fan_speed);
#endif
} }
void trigger12() void trigger12()
{ {
#ifdef ENABLE_CLIMATE_MODULE
ac_set_state(0, ac_temperature, ac_fan_speed); ac_set_state(0, ac_temperature, ac_fan_speed);
#endif
} }
void trigger13() void trigger13()
{ {
set_ip(panel.readStr("ip_set.txt")); set_ip(panel.readStr("ip_set.txt"));
@ -1052,6 +1141,7 @@ void trigger15()
delay(100); delay(100);
ESP.restart(); ESP.restart();
} }
#endif // End Internal LCD Code Block
void eeprom_pwm_update() void eeprom_pwm_update()
{ {
@ -1156,6 +1246,7 @@ boolean input_get_state(int id)
return virtual_interupt_state[id]; return virtual_interupt_state[id];
} }
#ifdef ENABLE_CLIMATE_MODULE
uint8_t ac_get_temperature() uint8_t ac_get_temperature()
{ {
return ac_temperature; return ac_temperature;
@ -1168,6 +1259,7 @@ uint8_t ac_get_fan_speed()
{ {
return ac_fan_speed; return ac_fan_speed;
} }
#endif
void eeprom_mqtt_username_retrieve() void eeprom_mqtt_username_retrieve()
{ {
@ -1197,34 +1289,47 @@ void set_mqtt_useauth(bool use_auth)
ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH); ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH);
} }
void factory_reset() { void factory_reset()
for(int i=5;i>0;i--) { {
if(digitalRead(2)==HIGH) { for (int i = 5; i > 0; i--)
{
if (digitalRead(2) == HIGH)
{
lcd_send_command("boot_state.txt=\"Factory Reset Canceled, Restarting\""); lcd_send_command("boot_state.txt=\"Factory Reset Canceled, Restarting\"");
delay(5000); delay(5000);
ESP.restart(); ESP.restart();
} }
#ifdef ENABLE_INTERNAL_LCD
Serial.printf("boot_state.txt=\"Factory Reset in %d\"", i); Serial.printf("boot_state.txt=\"Factory Reset in %d\"", i);
lcd_send_stop_bit(); lcd_send_stop_bit();
#endif
delay(1000); delay(1000);
} }
#ifdef ENABLE_INTERNAL_LCD
lcd_send_command("boot_state.txt=\"Factory Reseting . . .\""); lcd_send_command("boot_state.txt=\"Factory Reseting . . .\"");
#endif
// Format FRAM // Format FRAM
for(int i=0;i<32768;i++) { for (int i = 0; i < 32768; i++)
{
ESPMega_FRAM.write8(i, 0); ESPMega_FRAM.write8(i, 0);
} }
// Load Default Values // Load Default Values
set_ip("192.168.0.10"); set_ip("192.168.0.10");
set_gw("192.168.0.1"); set_gw("192.168.0.1");
set_netmask("255.255.255.0");
// Reboot // Reboot
#ifdef ENABLE_INTERNAL_LCD
lcd_send_stop_bit(); lcd_send_stop_bit();
lcd_send_command("boot_state.txt=\"Factory Reset OK. Release Button.\""); lcd_send_command("boot_state.txt=\"Factory Reset OK. Release Button.\"");
delay(3000); delay(3000);
#endif
ESP.restart(); ESP.restart();
} }
void check_boot_reset() { void check_boot_reset()
{
pinMode(2, INPUT_PULLUP); pinMode(2, INPUT_PULLUP);
if(digitalRead(2)==LOW) { if (digitalRead(2) == LOW)
{
factory_reset(); factory_reset();
} }
} }

View File

@ -1,23 +1,38 @@
#pragma once #pragma once
#include <ESPMegaPRO.h> #include <ESPMegaPRO.h>
#include "user_code.hpp"
#ifdef ENABLE_CLIMATE_MODULE
#ifndef ENABLE_IR_MODULE
#error "The Climate Module Requires the IR Module to be enabled with #define ENABLE_IR_MODULE"
#endif
#endif
#include <ETH.h> #include <ETH.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <PubSubClientTools.h> #include <PubSubClientTools.h>
#include <Thread.h> #include <Thread.h>
#include <StaticThreadController.h> #include <StaticThreadController.h>
#ifdef ENABLE_IR_MODULE
#include <IRremote.hpp> #include <IRremote.hpp>
#endif
#include <dhtnew.h> #include <dhtnew.h>
#include <time.h> #include <time.h>
#ifdef ENABLE_INTERNAL_LCD
#include <EasyNextionLibrary.h> #include <EasyNextionLibrary.h>
#endif
#include <WiFiClient.h> #include <WiFiClient.h>
#ifdef ENABLE_WEBUI
#include <WebServer.h> #include <WebServer.h>
#include <Update.h> #include <Update.h>
#endif
#include "lcd.hpp" #include "lcd.hpp"
#include "user_code.hpp"
#ifdef ENABLE_CLIMATE_MODULE
#include "ir_codes.hpp" #include "ir_codes.hpp"
#endif
#ifdef ENABLE_WEBUI
#include "espmega_iot_ota.hpp" #include "espmega_iot_ota.hpp"
#endif
#include "espmega_iot_timer.hpp" #include "espmega_iot_timer.hpp"
void virtual_interrupt_loop(); void virtual_interrupt_loop();

View File

@ -1,12 +1,5 @@
#pragma once #pragma once
#include <ESPMegaPRO.h> #include <ESPMegaPRO.h>
// Infrared Transciever
#define IR_RECIEVE_PIN 35
#define IR_SEND_PIN 17
#define MARK_EXCESS_MICROS 20
#define RAW_BUFFER_LENGTH 750
#define AC_MAX_TEMPERATURE 30
#define AC_MIN_TEMPERATURE 15
extern const uint8_t ir_code_cool[3][16][750] = { extern const uint8_t ir_code_cool[3][16][750] = {
// Fan Speed High // Fan Speed High
{ {

View File

@ -1,24 +1,37 @@
#include <user_code.hpp>
#include <lcd.hpp> #include <lcd.hpp>
void lcd_send_stop_bit() { void lcd_send_stop_bit()
{
#ifdef ENABLE_INTERNAL_LCD
Serial.write(0xFF); Serial.write(0xFF);
Serial.write(0xFF); Serial.write(0xFF);
Serial.write(0xFF); Serial.write(0xFF);
#endif
} }
void lcd_send_command(String command) { void lcd_send_command(String command)
{
#ifdef ENABLE_INTERNAL_LCD
lcd_send_stop_bit(); lcd_send_stop_bit();
Serial.print(command); Serial.print(command);
lcd_send_stop_bit(); lcd_send_stop_bit();
#endif
} }
void lcd_reset() { void lcd_reset()
{
#ifdef ENABLE_INTERNAL_LCD
lcd_send_stop_bit(); lcd_send_stop_bit();
lcd_send_command("rest"); lcd_send_command("rest");
lcd_send_stop_bit(); lcd_send_stop_bit();
#endif
} }
void lcd_init() { void lcd_init()
{
#ifdef ENABLE_INTERNAL_LCD
lcd_reset(); lcd_reset();
delay(750); delay(750);
#endif
} }

View File

@ -5,6 +5,20 @@
#include "espmega_iot_timer.hpp" #include "espmega_iot_timer.hpp"
#include "espmega_iot_external_lcd.hpp" #include "espmega_iot_external_lcd.hpp"
// Enable Software Module(s)
#define ENABLE_INTERNAL_LCD
#define ENABLE_IR_MODULE
#define ENABLE_CLIMATE_MODULE // Require IR Module
#define ENABLE_WEBUI
// Infrared Transciever
#define IR_RECIEVE_PIN 35
#define IR_SEND_PIN 17
#define MARK_EXCESS_MICROS 20
#define RAW_BUFFER_LENGTH 750
#define AC_MAX_TEMPERATURE 30
#define AC_MIN_TEMPERATURE 15
// External LCD Configuration // External LCD Configuration
//#define ENABLE_EXTERNAL_LCD //#define ENABLE_EXTERNAL_LCD
//#define TXD2 4 //#define TXD2 4