diff --git a/platformio.ini b/platformio.ini index 760a3ed..5e158bc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,4 +19,5 @@ lib_deps = siwats/espmegapror3@^1.0.2 dersimn/PubSubClientTools@^0.6 z3t0/IRremote@^4.2.0 robtillaart/DHTNEW@^0.4.18 + seithan/Easy Nextion Library@^1.0.6 monitor_speed = 115200 \ No newline at end of file diff --git a/src/lcd.cpp b/src/lcd.cpp new file mode 100644 index 0000000..db62070 --- /dev/null +++ b/src/lcd.cpp @@ -0,0 +1,47 @@ +#include + +void lcd_send_stop_bit() { + Serial.write(0xFF); + Serial.write(0xFF); + Serial.write(0xFF); +} +int lcd_get_int() { + return 0; +} + +float lcd_get_float() { + +} + +String lcd_get_str() { + +} + +String lcd_set_var(String var, String value) { + +} + +void lcd_send_command(String command) { + lcd_send_stop_bit(); + Serial.print(command); + lcd_send_stop_bit(); +} + +String lcd_wait_response() { + +} + +void lcd_touch_callback(int page, int id) { + +} + +void lcd_reset() { + lcd_send_stop_bit(); + lcd_send_command("rest"); + lcd_send_stop_bit(); +} + +void lcd_init() { + lcd_reset(); + delay(750); +} \ No newline at end of file diff --git a/src/lcd.hpp b/src/lcd.hpp new file mode 100644 index 0000000..274715b --- /dev/null +++ b/src/lcd.hpp @@ -0,0 +1,16 @@ +#ifndef EMG_LCD_ENABLED +#include + +#define EMG_LCD_ENABLED + +void lcd_send_stop_bit(); +int lcd_get_int(); +float lcd_get_float(); +String lcd_get_str(); +String lcd_set_var(String var, String value); +void lcd_send_command(String command); +String lcd_wait_response(); +void lcd_touch_callback(int page, int id); +void lcd_reset(); +void lcd_init(); +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b9113c2..5c08ce3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,8 @@ #include #include #include +#include "EasyNextionLibrary.h" +#include // Network Connectivity #define HOSTNAME "espmega-pro-r3" @@ -16,6 +18,7 @@ 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); +bool standalone = true; const int MQTT_PORT = 1883; #define MQTT_BASE_TOPIC "/espmega/ProR3" char STATE_REQUEST_TOPIC[75] = MQTT_BASE_TOPIC "/requeststate"; @@ -50,6 +53,10 @@ char PWM_SET_VALUE_TOPIC[75] = MQTT_BASE_TOPIC "/pwm/00/set/value"; #define MARK_EXCESS_MICROS 20 #define RAW_BUFFER_LENGTH 750 +// LCD +int lcd_current_page = 1; +EasyNex panel(Serial); + // Air Conditioner Control /* Mode 0: Off, 1: Cool, 2: Fan @@ -101,6 +108,12 @@ void publish_input_states(); void publish_input_state(int id); void publish_input_state(int id, int state); +void lcd_begin(); +void lcd_loop(); +void lcd_refresh(); +void lcd_refresh_pd(); +void lcd_top_bar_update(); + 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"; @@ -115,21 +128,34 @@ Thread mqtt_reconnector = Thread(); Thread environment_reporter = Thread(); StaticThreadController<2> thread_controller(&mqtt_reconnector, &environment_reporter); +Thread top_bar_updater = Thread(); +Thread page_updater = Thread(); +StaticThreadController<2> lcd_thread_controller(&top_bar_updater, &page_updater); + void setup() { Serial.begin(115200); + panel.begin(115200); + lcd_init(); + lcd_begin(); + lcd_send_command("boot_state.txt=\"Core Initializing . . .\""); Serial.println("ESPMega R3 Initializing . . ."); ESPMega_begin(); io_begin(); Serial.println("Initializing Infrared . . ."); + lcd_send_command("boot_state.txt=\"Infrared Initializing . . .\""); IrReceiver.begin(IR_RECIEVE_PIN); IrSender.begin(IR_SEND_PIN); + lcd_send_command("boot_state.txt=\"Network Initializing . . .\""); network_begin(); Serial.println("Initializing MQTT . . ."); + lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\""); mqtt_connect(); + lcd_send_command("boot_state.txt=\"Threads Initializing . . .\""); thread_initialization(); Serial.println("Initialization Completed."); Serial.println("Jumping to User Code."); + lcd_send_command("page dashboard"); } void loop() @@ -139,6 +165,7 @@ void loop() ESPMega_loop(); ir_loop(); thread_controller.run(); + lcd_loop(); } void io_begin() @@ -172,6 +199,7 @@ void mqtt_connect() if (!mqtt_client.connected()) { Serial.print("MQTT not connected, connecting . . .\n"); + lcd_send_stop_bit(); #ifdef MQTT_USE_AUTH mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD); #else @@ -181,14 +209,21 @@ void mqtt_connect() { mqtt_subscribe(); Serial.print("MQTT connected\n"); + lcd_send_stop_bit(); publish_pwm_states(); publish_input_states(); publish_ac_state(); + standalone = false; } else { + standalone = true; Serial.print("MQTT not connected, continuing in standalone mode\n"); + lcd_send_stop_bit(); } + lcd_send_stop_bit(); + lcd_refresh(); + lcd_top_bar_update(); } } @@ -214,36 +249,28 @@ void thread_initialization() Serial.println("Initializing Threads . . ."); Serial.println("Initializing MQTT Thread . . ."); mqtt_reconnector.onRun(mqtt_connect); - mqtt_reconnector.setInterval(30000); + mqtt_reconnector.setInterval(15000); environment_reporter.onRun(publish_env_state); environment_reporter.setInterval(5000); } void pwm_state_callback(String topic, String message) { - Serial.println("Recieved PWM State Message"); - Serial.println(topic); - Serial.println(message); int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0'; int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0'; int id = 10 * a + b; if (message.compareTo("on") == 0) { - Serial.printf("Setting pwm %d to on\n", id); pwm_set_state(id, true); } else if (message.compareTo("off") == 0) { - 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); int a = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 4) - '0'; int b = topic.charAt(sizeof(MQTT_BASE_TOPIC) + 5) - '0'; int id = 10 * a + b; @@ -253,8 +280,11 @@ void pwm_value_callback(String topic, String message) void virtual_interrupt_callback(int pin, int state) { + publish_input_state(pin, state); - Serial.printf("Pin %d changed to %d\n", pin, state); + // Serial.printf("Pin %d changed to %d\n", pin, state); + if (lcd_current_page == 2) + panel.writeNum("I" + String(pin) + ".val", state); if (pin == 15) { pwm_toggle(4); @@ -314,6 +344,8 @@ void pwm_set_state(int id, int state) 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])); + if (lcd_current_page == 3) + panel.writeNum("j" + String(id) + ".pco", state?47829:12710); publish_pwm_state(id); } } @@ -323,6 +355,8 @@ void pwm_set_value(int id, int value) 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])); + if (lcd_current_page == 3) + panel.writeNum("j" + String(id) + ".val", int(value / 4095.0 * 100.0)); publish_pwm_state(id); } @@ -412,10 +446,10 @@ void ir_loop() { if (IrReceiver.decode()) { - Serial.println(); - IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, false); - Serial.println(); - Serial.println(); + // Serial.println(); + // IrReceiver.compensateAndPrintIRResultAsCArray(&Serial, false); + // Serial.println(); + // Serial.println(); IrReceiver.resume(); } } @@ -522,7 +556,6 @@ void ac_set_state(int mode, int temperature, int fan_speed) 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() @@ -543,4 +576,67 @@ void publish_env_state() mqtt.publish(String(AC_HUMIDITY_TOPIC), "ERROR"); mqtt_client.loop(); } +} + +void lcd_begin() +{ + top_bar_updater.onRun(lcd_top_bar_update); + top_bar_updater.setInterval(10000); + page_updater.onRun(lcd_refresh_pd); + page_updater.setInterval(5000); + lcd_refresh(); +} + +void lcd_loop() +{ + lcd_thread_controller.run(); + panel.NextionListen(); + if (panel.currentPageId != lcd_current_page) + { + lcd_current_page = panel.currentPageId; + lcd_refresh(); + lcd_top_bar_update(); + } +} + +void lcd_refresh_pd() +{ + if (lcd_current_page == 1) + { + lcd_refresh(); + } +} + +void lcd_refresh() +{ + switch (lcd_current_page) + { + case 1: + panel.writeStr("hostname.txt", HOSTNAME); + panel.writeStr("server_address.txt", MQTT_SERVER.toString()); + panel.writeStr("ip_address.txt", IP.toString()); + panel.writeStr("status_txt.txt", standalone ? "Standalone" : "BMS Managed"); + break; + case 2: + for (int i = 0; i <= 15; i++) + { + panel.writeNum("I" + String(i) + ".val", virtual_interupt_state[i]); + } + break; + case 3: + for (int i = 0; i <= 15; i++) + { + panel.writeNum("j" + String(i) + ".val", int(pwm_values[i] / 4095.0 * 100.0)); + panel.writeNum("j" + String(i) + ".pco", pwm_states[i]?47829:12710); + } + break; + default: + break; + } +} +void lcd_top_bar_update() +{ + panel.writeStr("time.txt", "NO RTC!"); + panel.writeNum("server.pic", standalone ? 4 : 5); + panel.writeNum("lan.pic", ETH.linkUp() ? 3 : 2); } \ No newline at end of file