From 897d8694e164415e4a301ba398570afa119c1bae Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 7 Nov 2023 15:49:11 +0700 Subject: [PATCH] mqtt authentication --- src/espmega_iot_core.cpp | 94 ++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/src/espmega_iot_core.cpp b/src/espmega_iot_core.cpp index 60fe25f..d336ada 100644 --- a/src/espmega_iot_core.cpp +++ b/src/espmega_iot_core.cpp @@ -71,22 +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_MQTT_USERNAME 122 // 32bytes, thru 153 -#define EEPROM_ADDRESS_MQTT_PASSWORD 154 // 32bytes, thru 185 -#define EEPROM_ADDRESS_MQTT_USEAUTH 186 // 1bytes +#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]; @@ -349,11 +349,10 @@ void mqtt_connect() { Serial.print("MQTT not connected, connecting . . .\n"); lcd_send_stop_bit(); -#ifdef MQTT_USE_AUTH - mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD); -#else - mqtt_client.connect(HOSTNAME); -#endif + if (MQTT_USE_AUTH) + mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD); + else + mqtt_client.connect(HOSTNAME); if (mqtt_client.connected()) { mqtt_subscribe(); @@ -835,10 +834,15 @@ void lcd_refresh() panel.writeStr("netmask_set.txt", SUBNET.toString()); panel.writeStr("gateway_set.txt", GATEWAY.toString()); panel.writeStr("dns_set.txt", DNS.toString()); - panel.writeStr("mqttsv_set.txt", MQTT_SERVER.toString()); panel.writeStr("host_set.txt", HOSTNAME); + break; + case 11: + panel.writeStr("mqttsv_set.txt", MQTT_SERVER.toString()); panel.writeNum("port_set.val", MQTT_PORT); panel.writeStr("topic_set.txt", MQTT_BASE_TOPIC); + panel.writeStr("user_set.txt", MQTT_USERNAME); + panel.writeStr("password_set.txt",MQTT_PASSWORD); + panel.writeNum("use_auth.val",MQTT_USE_AUTH); break; default: break; @@ -953,20 +957,30 @@ void trigger13() set_netmask(panel.readStr("netmask_set.txt")); set_gw(panel.readStr("gateway_set.txt")); set_dns(panel.readStr("dns_set.txt")); - set_mqtt_server(panel.readStr("mqttsv_set.txt")); set_hostname(panel.readStr("host_set.txt")); - set_basetopic(panel.readStr("topic_set.txt")); - uint16_t port = panel.readNumber("port_set.val"); - mqtt_port_set(port); delay(100); ESP.restart(); } -void trigger14() { +void trigger14() +{ Serial.print("page dashboard"); lcd_send_stop_bit(); } +void trigger15() +{ + set_mqtt_server(panel.readStr("mqttsv_set.txt")); + set_basetopic(panel.readStr("topic_set.txt")); + uint16_t port = panel.readNumber("port_set.val"); + mqtt_port_set(port); + set_mqtt_useauth(panel.readNumber("use_auth.val")); + set_mqtt_username(panel.readStr("user_set.txt")); + set_mqtt_password(panel.readStr("password_set.txt")); + delay(100); + ESP.restart(); +} + void eeprom_pwm_update() { if (memcmp(pwm_states, pwm_states_eeprom, 16)) @@ -1083,24 +1097,30 @@ uint8_t ac_get_fan_speed() return ac_fan_speed; } -void eeprom_mqtt_username_retrieve() { - ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 32); +void eeprom_mqtt_username_retrieve() +{ + ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 32); } -void eeprom_mqtt_password_retrieve() { +void eeprom_mqtt_password_retrieve() +{ ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PASSWORD, (uint8_t *)MQTT_PASSWORD, 32); } -void set_mqtt_username(String username) { - username.toCharArray(MQTT_USERNAME,32); +void set_mqtt_username(String username) +{ + username.toCharArray(MQTT_USERNAME, 32); ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 20); } -void set_mqtt_password(String password) { - password.toCharArray(MQTT_PASSWORD,32); - ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_PASSWORD, (uint8_t *)MQTT_PASSWORD, 20); +void set_mqtt_password(String password) +{ + password.toCharArray(MQTT_PASSWORD, 32); + ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_PASSWORD, (uint8_t *)MQTT_PASSWORD, 20); } -void eeprom_mqtt_useauth_retrieve() { +void eeprom_mqtt_useauth_retrieve() +{ MQTT_USE_AUTH = ESPMega_FRAM.read8(EEPROM_ADDRESS_MQTT_USEAUTH); } -void set_mqtt_useauth(bool use_auth) { +void set_mqtt_useauth(bool use_auth) +{ MQTT_USE_AUTH = use_auth; ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH); } \ No newline at end of file