diff --git a/.vscode/settings.json b/.vscode/settings.json index c66a10b..69aa100 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,8 @@ "type_traits": "cpp", "utility": "cpp", "*.d": "cpp", - "*.html": "cpp" + "*.html": "cpp", + "*.tcc": "cpp", + "regex": "cpp" } } \ No newline at end of file diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 5714960..d6253ac 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -18,11 +18,9 @@ bool standalone = true; char MQTT_BASE_TOPIC[20]; uint8_t base_topic_length = 0; char STATE_REQUEST_TOPIC[40]; -// #define MQTT_USE_AUTH -#ifdef MQTT_USE_AUTH -const char MQTT_USERNAME[] = "username"; -const char MQTT_PASSWORD[] = "password"; -#endif +bool MQTT_USE_AUTH = false; +char MQTT_USERNAME[32]; +char MQTT_PASSWORD[32]; uint8_t utc_offset = 7; float current_room_temp = 0; float current_room_humid = 0; @@ -73,19 +71,22 @@ char AC_ROOM_TEMPERATURE_TOPIC[75]; char AC_HUMIDITY_TOPIC[75]; // EEPROM ADDRESS -#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes -#define EEPROM_ADDRESS_AC_TEMPERATURE 1 // 01bytes -#define EEPROM_ADDRESS_AC_FAN_SPEED 2 // 01bytes -#define EEPROM_ADDRESS_PWM_STATE 3 // 16bytes, thru 18 -#define EEPROM_ADDRESS_PWM_VALUE 19 // 32bytes, thru 50 -#define EEPROM_ADDRESS_HOSTNAME 65 // 15bytes, thru 79 -#define EEPROM_ADDRESS_TOPIC 80 // 20bytes, thru 99 -#define EEPROM_ADDRESS_IP 100 // 04bytes, thru 103 -#define EEPROM_ADDRESS_SUBNET 104 // 04bytes, thru 107 -#define EEPROM_ADDRESS_GATEWAY 108 // 04bytes, thru 111 -#define EEPROM_ADDRESS_DNS 112 // 04bytes, thru 115 -#define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119 -#define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121 +#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes +#define EEPROM_ADDRESS_AC_TEMPERATURE 1 // 01bytes +#define EEPROM_ADDRESS_AC_FAN_SPEED 2 // 01bytes +#define EEPROM_ADDRESS_PWM_STATE 3 // 16bytes, thru 18 +#define EEPROM_ADDRESS_PWM_VALUE 19 // 32bytes, thru 50 +#define EEPROM_ADDRESS_HOSTNAME 65 // 15bytes, thru 79 +#define EEPROM_ADDRESS_TOPIC 80 // 20bytes, thru 99 +#define EEPROM_ADDRESS_IP 100 // 04bytes, thru 103 +#define EEPROM_ADDRESS_SUBNET 104 // 04bytes, thru 107 +#define EEPROM_ADDRESS_GATEWAY 108 // 04bytes, thru 111 +#define EEPROM_ADDRESS_DNS 112 // 04bytes, thru 115 +#define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119 +#define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121 +#define EEPROM_ADDRESS_MQTT_USERNAME 122 // 32bytes, thru 153 +#define EEPROM_ADDRESS_MQTT_PASSWORD 154 // 32bytes, thru 185 +#define EEPROM_ADDRESS_MQTT_USEAUTH 186 // 1bytes char PWM_STATE_TOPIC[75]; char PWM_VALUE_TOPIC[75]; @@ -122,6 +123,7 @@ void setup() lcd_send_stop_bit(); lcd_init(); lcd_begin(); + check_boot_reset(); #ifdef ENABLE_EXTERNAL_LCD Serial2.print("rest"); Serial2.write(0xFF); @@ -136,6 +138,8 @@ void setup() lcd_send_command("boot_state.txt=\"Network Initializing . . .\""); network_begin(); lcd_send_command("boot_state.txt=\"IoT Core Initializing . . .\""); + mqtt_client.setSocketTimeout(1000); + eth.setTimeout(1); mqtt_connect(); lcd_send_command("boot_state.txt=\"Threads Initializing . . .\""); thread_initialization(); @@ -203,6 +207,9 @@ void eeprom_retrieve_init() MQTT_SERVER = eeprom_ip_retrieve(EEPROM_ADDRESS_MQTT_SERVER); eeprom_hostname_retrieve(); eeprom_mqtt_port_retrieve(); + eeprom_mqtt_useauth_retrieve(); + eeprom_mqtt_username_retrieve(); + eeprom_mqtt_password_retrieve(); mqtt_client.setServer(MQTT_SERVER, MQTT_PORT); eeprom_basetopic_retrieve(); base_topic_length = strlen(MQTT_BASE_TOPIC) + 1; @@ -241,13 +248,81 @@ void ota_begin() otaserver.on("/", HTTP_GET, []() { otaserver.sendHeader("Connection", "close"); - String otabuffer = ota_part1+HOSTNAME+ota_part2+IP.toString()+ota_part3+ETH.macAddress()+ota_part4+ESPMEGA_REV+ota_part5+MQTT_SERVER.toString()+ota_part6+String(MQTT_BASE_TOPIC)+ota_part7; + String otabuffer = ota_part1; + otabuffer+=ota_part2_1+"Hostname"+ota_part2_2+String(HOSTNAME)+ota_part2_3; + otabuffer+=ota_part2_1+"IP Address"+ota_part2_2+IP.toString()+ota_part2_3; + otabuffer+=ota_part2_1+"MAC Address"+ota_part2_2+ETH.macAddress()+ota_part2_3; + otabuffer+=ota_part2_1+"Device"+ota_part2_2+ESPMEGA_REV+ota_part2_3; + otabuffer+=ota_part2_1+"BMS Server"+ota_part2_2+MQTT_SERVER.toString()+ota_part2_3; + otabuffer+=ota_part2_1+"BMS Endpoint"+ota_part2_2+String(MQTT_BASE_TOPIC)+ota_part2_3; + otabuffer+=ota_part2_1+"Centrally Managed"+ota_part2_2; if(standalone) otabuffer+=String("No"); else otabuffer+=String("Yes"); - otabuffer+=ota_part8; + otabuffer+=ota_part2_3+ota_part3; otaserver.send(200, "text/html", otabuffer); }); + otaserver.on("/config", HTTP_GET, []() + { + otaserver.sendHeader("Connection", "close"); + String configbuffer = config_part1; + configbuffer+=config_txt_part1+"IP Address"+config_txt_part2+"text"+config_txt_part3+"dev_ip"+config_txt_part4+"dev_ip"+config_txt_part5+IP.toString()+config_txt_part6; + configbuffer+=config_txt_part1+"Network Mask"+config_txt_part2+"text"+config_txt_part3+"netmask"+config_txt_part4+"netmask"+config_txt_part5+SUBNET.toString()+config_txt_part6; + configbuffer+=config_txt_part1+"Gateway"+config_txt_part2+"text"+config_txt_part3+"gateway"+config_txt_part4+"gateway"+config_txt_part5+GATEWAY.toString()+config_txt_part6; + configbuffer+=config_txt_part1+"DNS Server"+config_txt_part2+"text"+config_txt_part3+"dns"+config_txt_part4+"dns"+config_txt_part5+DNS.toString()+config_txt_part6; + configbuffer+=config_txt_part1+"Hostname"+config_txt_part2+"text"+config_txt_part3+"hostname"+config_txt_part4+"hostname"+config_txt_part5+String(HOSTNAME)+config_txt_part6; + configbuffer+=config_txt_part1+"BMS Server - IP Address"+config_txt_part2+"text"+config_txt_part3+"bms_ip"+config_txt_part4+"bms_ip"+config_txt_part5+MQTT_SERVER.toString()+config_txt_part6; + configbuffer+=config_txt_part1+"BMS Server - Port"+config_txt_part2+"text"+config_txt_part3+"bms_port"+config_txt_part4+"bms_port"+config_txt_part5+String(MQTT_PORT)+config_txt_part6; + configbuffer+=config_auth_part1+(MQTT_USE_AUTH?"checked=\"checked\"":"")+config_auth_part2; + configbuffer+=config_txt_part1+"BMS Server - Username"+config_txt_part2+"text"+config_txt_part3+"bms_username"+config_txt_part4+"bms_username"+config_txt_part5+String(MQTT_USERNAME)+config_txt_part6; + configbuffer+=config_txt_part1+"BMS Server - Password"+config_txt_part2+"password"+config_txt_part3+"bms_password"+config_txt_part4+"bms_password"+config_txt_part5+String(MQTT_PASSWORD)+config_txt_part6; + configbuffer+=config_txt_part1+"BMS Server - Endpoint"+config_txt_part2+"text"+config_txt_part3+"bms_endpoint"+config_txt_part4+"bms_endpoint"+config_txt_part5+String(MQTT_BASE_TOPIC)+config_txt_part6; + configbuffer+=config_part2; + otaserver.send(200, "text/html", configbuffer); }); + + otaserver.on("/save_config", HTTP_GET, []() + { + otaserver.sendHeader("Connection", "close"); + String configbuffer = "Configuration Saved. Rebooting . . ."; + otaserver.send(200, "text/html", configbuffer); + bool use_auth = false; + for(int i=0;i0;i--) { + if(digitalRead(2)==HIGH) { + lcd_send_command("boot_state.txt=\"Factory Reset Canceled, Restarting\""); + delay(5000); + ESP.restart(); + } + Serial.printf("boot_state.txt=\"Factory Reset in %d\"",i); + lcd_send_stop_bit(); + delay(1000); + } + lcd_send_command("boot_state.txt=\"Factory Reseting . . .\""); + // Format FRAM + for(int i=0;i<32768;i++) { + ESPMega_FRAM.write8(i,0); + } + // Load Default Values + set_ip("192.168.0.10"); + set_gw("192.168.0.1"); + // Reboot + lcd_send_stop_bit(); + lcd_send_command("boot_state.txt=\"Factory Reset OK. Release Button.\""); + delay(3000); + ESP.restart(); +} +void check_boot_reset() { + pinMode(2, INPUT_PULLUP); + if(digitalRead(2)==LOW) { + factory_reset(); + } } \ No newline at end of file diff --git a/src/espmega_iot_core.hpp b/src/espmega_iot_core.hpp index 6acc508..23c32eb 100644 --- a/src/espmega_iot_core.hpp +++ b/src/espmega_iot_core.hpp @@ -78,6 +78,7 @@ void trigger11(); void trigger12(); void trigger13(); void trigger14(); +void trigger15(); void eeprom_retrieve_init(); void eeprom_pwm_update(); @@ -97,4 +98,13 @@ void eeprom_hostname_retrieve(); void set_basetopic(String topic); void eeprom_basetopic_retrieve(); void mqtt_port_set(uint16_t port); -void eeprom_mqtt_port_retrieve(); \ No newline at end of file +void eeprom_mqtt_port_retrieve(); +void eeprom_mqtt_username_retrieve(); +void eeprom_mqtt_password_retrieve(); +void set_mqtt_username(String username); +void set_mqtt_password(String password); +void eeprom_mqtt_useauth_retrieve(); +void set_mqtt_useauth(bool use_auth); + +void factory_reset(); +void check_boot_reset(); \ No newline at end of file diff --git a/src/espmega_iot_ota.cpp b/src/espmega_iot_ota.cpp index 282ffd5..3c67154 100644 --- a/src/espmega_iot_ota.cpp +++ b/src/espmega_iot_ota.cpp @@ -3,24 +3,46 @@ const String ota_part1 = { #include "ota_html/ota-part1.html" }; -const String ota_part2 = { -#include "ota_html/ota-part2.html" +const String ota_part2_1 = { +#include "ota_html/ota-part2-1_itr.html" +}; +const String ota_part2_2 = { +#include "ota_html/ota-part2-2_itr.html" +}; +const String ota_part2_3 = { +#include "ota_html/ota-part2-3_itr.html" }; const String ota_part3 = { #include "ota_html/ota-part3.html" }; -const String ota_part4 = { -#include "ota_html/ota-part4.html" + +const String config_part1 = { +#include "ota_html/config-part1.html" }; -const String ota_part5 = { -#include "ota_html/ota-part5.html" +const String config_part2 = { +#include "ota_html/config-part2.html" }; -const String ota_part6 = { -#include "ota_html/ota-part6.html" +const String config_txt_part1 = { +#include "ota_html/config-textfield-part1.html" }; -const String ota_part7 = { -#include "ota_html/ota-part7.html" +const String config_txt_part2 = { +#include "ota_html/config-textfield-part2.html" }; -const String ota_part8 = { -#include "ota_html/ota-part8.html" +const String config_txt_part3 = { +#include "ota_html/config-textfield-part3.html" +}; +const String config_txt_part4 = { +#include "ota_html/config-textfield-part4.html" +}; +const String config_txt_part5 = { +#include "ota_html/config-textfield-part5.html" +}; +const String config_txt_part6 = { +#include "ota_html/config-textfield-part6.html" +}; +const String config_auth_part1 = { +#include "ota_html/config-auth-part1.html" +}; +const String config_auth_part2 = { +#include "ota_html/config-auth-part2.html" }; \ No newline at end of file diff --git a/src/espmega_iot_ota.hpp b/src/espmega_iot_ota.hpp index 8ca414b..2bd7c4c 100644 --- a/src/espmega_iot_ota.hpp +++ b/src/espmega_iot_ota.hpp @@ -1,10 +1,17 @@ #pragma once #include extern const String ota_part1; -extern const String ota_part2; +extern const String ota_part2_1; +extern const String ota_part2_2; +extern const String ota_part2_3; extern const String ota_part3; -extern const String ota_part4; -extern const String ota_part5; -extern const String ota_part6; -extern const String ota_part7; -extern const String ota_part8; \ No newline at end of file +extern const String config_part1; +extern const String config_part2; +extern const String config_txt_part1; +extern const String config_txt_part2; +extern const String config_txt_part3; +extern const String config_txt_part4; +extern const String config_txt_part5; +extern const String config_txt_part6; +extern const String config_auth_part1; +extern const String config_auth_part2; diff --git a/src/ota_html/config-auth-part1.html b/src/ota_html/config-auth-part1.html new file mode 100644 index 0000000..ab970c0 --- /dev/null +++ b/src/ota_html/config-auth-part1.html @@ -0,0 +1,2 @@ +R"=====()=====" \ No newline at end of file diff --git a/src/ota_html/config-part1.html b/src/ota_html/config-part1.html new file mode 100644 index 0000000..f15e642 --- /dev/null +++ b/src/ota_html/config-part1.html @@ -0,0 +1,4 @@ +R"=====( +
+

ESPMega PRO

+

Device Configurations

)=====" \ No newline at end of file diff --git a/src/ota_html/config-part2.html b/src/ota_html/config-part2.html new file mode 100644 index 0000000..8b5a3db --- /dev/null +++ b/src/ota_html/config-part2.html @@ -0,0 +1,167 @@ +R"=====( +

+ SIWAT SYSTEM 2023 +
+)=====" \ No newline at end of file diff --git a/src/ota_html/config-textfield-part1.html b/src/ota_html/config-textfield-part1.html new file mode 100644 index 0000000..3c4da03 --- /dev/null +++ b/src/ota_html/config-textfield-part1.html @@ -0,0 +1 @@ +R"=====(

)=====" \ No newline at end of file diff --git a/src/ota_html/config-textfield-part2.html b/src/ota_html/config-textfield-part2.html new file mode 100644 index 0000000..69eaeed --- /dev/null +++ b/src/ota_html/config-textfield-part2.html @@ -0,0 +1,2 @@ +R"=====(

+
)=====" \ No newline at end of file diff --git a/src/ota_html/config.html b/src/ota_html/config.html new file mode 100644 index 0000000..703e086 --- /dev/null +++ b/src/ota_html/config.html @@ -0,0 +1,198 @@ + +
+

ESPMega PRO

+

Device Configurations

+

IP Address

+
+

Network Mask

+
+

Gateway

+
+

DNS Server

+
+

Hostname

+
+

BMS Server - IP Address

+
+

BMS Server - Port

+
+ +

BMS Server - Username

+
+

BMS Server - Password

+
+

BMS Server - Endpoint

+
+ +

+ SIWAT SYSTEM 2023 +
+ \ No newline at end of file diff --git a/src/ota_html/ota-part1.html b/src/ota_html/ota-part1.html index a63d855..1c0e261 100644 --- a/src/ota_html/ota-part1.html +++ b/src/ota_html/ota-part1.html @@ -1,8 +1,4 @@ R"=====(

ESPMega PRO

-

Software Management

- -

- Hostname - )=====" +

Device Information

)=====" diff --git a/src/ota_html/ota-part2-1_itr.html b/src/ota_html/ota-part2-1_itr.html new file mode 100644 index 0000000..dd6f621 --- /dev/null +++ b/src/ota_html/ota-part2-1_itr.html @@ -0,0 +1 @@ +R"=====(

)=====" diff --git a/src/ota_html/ota-part2-2_itr.html b/src/ota_html/ota-part2-2_itr.html new file mode 100644 index 0000000..7ff7dc6 --- /dev/null +++ b/src/ota_html/ota-part2-2_itr.html @@ -0,0 +1 @@ +R"=====()=====" diff --git a/src/ota_html/ota-part2-3_itr.html b/src/ota_html/ota-part2-3_itr.html new file mode 100644 index 0000000..ce17711 --- /dev/null +++ b/src/ota_html/ota-part2-3_itr.html @@ -0,0 +1,2 @@ +R"=====( +

)=====" \ No newline at end of file diff --git a/src/ota_html/ota-part2.html b/src/ota_html/ota-part2.html deleted file mode 100644 index 40f097f..0000000 --- a/src/ota_html/ota-part2.html +++ /dev/null @@ -1,5 +0,0 @@ -R"=====( -

-

- IP Address - )=====" diff --git a/src/ota_html/ota-part3.html b/src/ota_html/ota-part3.html index d92b7e1..4826dbf 100644 --- a/src/ota_html/ota-part3.html +++ b/src/ota_html/ota-part3.html @@ -1,5 +1,144 @@ -R"=====( -

-

- MAC Address - )=====" \ No newline at end of file +R"=====(

+


+

Upload Software Package

+ + +

+
+
+
+
+
+
+ SIWAT SYSTEM 2023 +
+ +)=====" \ No newline at end of file diff --git a/src/ota_html/ota-part4.html b/src/ota_html/ota-part4.html deleted file mode 100644 index 76b974c..0000000 --- a/src/ota_html/ota-part4.html +++ /dev/null @@ -1,5 +0,0 @@ -R"=====( -

-

- Device - )=====" \ No newline at end of file diff --git a/src/ota_html/ota-part5.html b/src/ota_html/ota-part5.html deleted file mode 100644 index d7745f3..0000000 --- a/src/ota_html/ota-part5.html +++ /dev/null @@ -1,5 +0,0 @@ -R"=====( -

-

- API Server - )=====" \ No newline at end of file diff --git a/src/ota_html/ota-part6.html b/src/ota_html/ota-part6.html deleted file mode 100644 index 743ba2a..0000000 --- a/src/ota_html/ota-part6.html +++ /dev/null @@ -1,5 +0,0 @@ -R"=====( -

-

- API Endpoint - )=====" \ No newline at end of file diff --git a/src/ota_html/ota-part7.html b/src/ota_html/ota-part7.html deleted file mode 100644 index 567deb3..0000000 --- a/src/ota_html/ota-part7.html +++ /dev/null @@ -1,5 +0,0 @@ -R"=====( -

-

- Centrally Managed - )=====" \ No newline at end of file diff --git a/src/ota_html/ota-part8.html b/src/ota_html/ota-part8.html deleted file mode 100644 index 4f94bee..0000000 --- a/src/ota_html/ota-part8.html +++ /dev/null @@ -1,125 +0,0 @@ -R"=====( -

- - - -

-
-
-
-
-
-
-SIWAT SYSTEM 2023 - - -)=====" \ No newline at end of file diff --git a/src/ota_html/ota.html b/src/ota_html/ota.html new file mode 100644 index 0000000..9314e55 --- /dev/null +++ b/src/ota_html/ota.html @@ -0,0 +1,176 @@ + +
+

ESPMega PRO

+

Device Information

+

+ Hostname + ESPMegaDemo0 +

+

+ IP Address + 192.168.0.243 +

+

+ MAC Address + B0:A7:32:07:FC:B3 +

+

+ Device + ESPMega PRO R3.3b +

+

+ API Server + 192.168.0.26 +

+

+ API Endpoint + /facescan +

+

+ Centrally Managed + Yes +

+

+
+

Upload Software Package

+ + +

+
+
+
+
+
+
+ SIWAT SYSTEM 2023 +
+ + \ No newline at end of file