mqtt authentication

This commit is contained in:
Siwat Sirichai 2023-11-07 15:49:11 +07:00
parent a90805b171
commit 897d8694e1
1 changed files with 57 additions and 37 deletions

View File

@ -71,22 +71,22 @@ char AC_ROOM_TEMPERATURE_TOPIC[75];
char AC_HUMIDITY_TOPIC[75]; char AC_HUMIDITY_TOPIC[75];
// EEPROM ADDRESS // EEPROM ADDRESS
#define EEPROM_ADDRESS_AC_MODE 0 // 01bytes #define EEPROM_ADDRESS_AC_MODE 0 // 01bytes
#define EEPROM_ADDRESS_AC_TEMPERATURE 1 // 01bytes #define EEPROM_ADDRESS_AC_TEMPERATURE 1 // 01bytes
#define EEPROM_ADDRESS_AC_FAN_SPEED 2 // 01bytes #define EEPROM_ADDRESS_AC_FAN_SPEED 2 // 01bytes
#define EEPROM_ADDRESS_PWM_STATE 3 // 16bytes, thru 18 #define EEPROM_ADDRESS_PWM_STATE 3 // 16bytes, thru 18
#define EEPROM_ADDRESS_PWM_VALUE 19 // 32bytes, thru 50 #define EEPROM_ADDRESS_PWM_VALUE 19 // 32bytes, thru 50
#define EEPROM_ADDRESS_HOSTNAME 65 // 15bytes, thru 79 #define EEPROM_ADDRESS_HOSTNAME 65 // 15bytes, thru 79
#define EEPROM_ADDRESS_TOPIC 80 // 20bytes, thru 99 #define EEPROM_ADDRESS_TOPIC 80 // 20bytes, thru 99
#define EEPROM_ADDRESS_IP 100 // 04bytes, thru 103 #define EEPROM_ADDRESS_IP 100 // 04bytes, thru 103
#define EEPROM_ADDRESS_SUBNET 104 // 04bytes, thru 107 #define EEPROM_ADDRESS_SUBNET 104 // 04bytes, thru 107
#define EEPROM_ADDRESS_GATEWAY 108 // 04bytes, thru 111 #define EEPROM_ADDRESS_GATEWAY 108 // 04bytes, thru 111
#define EEPROM_ADDRESS_DNS 112 // 04bytes, thru 115 #define EEPROM_ADDRESS_DNS 112 // 04bytes, thru 115
#define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119 #define EEPROM_ADDRESS_MQTT_SERVER 116 // 04bytes, thru 119
#define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121 #define EEPROM_ADDRESS_MQTT_PORT 120 // 02bytes, thru 121
#define EEPROM_ADDRESS_MQTT_USERNAME 122 // 32bytes, thru 153 #define EEPROM_ADDRESS_MQTT_USERNAME 122 // 32bytes, thru 153
#define EEPROM_ADDRESS_MQTT_PASSWORD 154 // 32bytes, thru 185 #define EEPROM_ADDRESS_MQTT_PASSWORD 154 // 32bytes, thru 185
#define EEPROM_ADDRESS_MQTT_USEAUTH 186 // 1bytes #define EEPROM_ADDRESS_MQTT_USEAUTH 186 // 1bytes
char PWM_STATE_TOPIC[75]; char PWM_STATE_TOPIC[75];
char PWM_VALUE_TOPIC[75]; char PWM_VALUE_TOPIC[75];
@ -349,11 +349,10 @@ void mqtt_connect()
{ {
Serial.print("MQTT not connected, connecting . . .\n"); Serial.print("MQTT not connected, connecting . . .\n");
lcd_send_stop_bit(); lcd_send_stop_bit();
#ifdef MQTT_USE_AUTH if (MQTT_USE_AUTH)
mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD); mqtt_client.connect(HOSTNAME, MQTT_USERNAME, MQTT_PASSWORD);
#else else
mqtt_client.connect(HOSTNAME); mqtt_client.connect(HOSTNAME);
#endif
if (mqtt_client.connected()) if (mqtt_client.connected())
{ {
mqtt_subscribe(); mqtt_subscribe();
@ -835,10 +834,15 @@ void lcd_refresh()
panel.writeStr("netmask_set.txt", SUBNET.toString()); panel.writeStr("netmask_set.txt", SUBNET.toString());
panel.writeStr("gateway_set.txt", GATEWAY.toString()); panel.writeStr("gateway_set.txt", GATEWAY.toString());
panel.writeStr("dns_set.txt", DNS.toString()); panel.writeStr("dns_set.txt", DNS.toString());
panel.writeStr("mqttsv_set.txt", MQTT_SERVER.toString());
panel.writeStr("host_set.txt", HOSTNAME); 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.writeNum("port_set.val", MQTT_PORT);
panel.writeStr("topic_set.txt", MQTT_BASE_TOPIC); 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; break;
default: default:
break; break;
@ -953,20 +957,30 @@ void trigger13()
set_netmask(panel.readStr("netmask_set.txt")); set_netmask(panel.readStr("netmask_set.txt"));
set_gw(panel.readStr("gateway_set.txt")); set_gw(panel.readStr("gateway_set.txt"));
set_dns(panel.readStr("dns_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_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); delay(100);
ESP.restart(); ESP.restart();
} }
void trigger14() { void trigger14()
{
Serial.print("page dashboard"); Serial.print("page dashboard");
lcd_send_stop_bit(); 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() void eeprom_pwm_update()
{ {
if (memcmp(pwm_states, pwm_states_eeprom, 16)) if (memcmp(pwm_states, pwm_states_eeprom, 16))
@ -1083,24 +1097,30 @@ uint8_t ac_get_fan_speed()
return ac_fan_speed; return ac_fan_speed;
} }
void eeprom_mqtt_username_retrieve() { void eeprom_mqtt_username_retrieve()
ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 32); {
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); ESPMega_FRAM.read(EEPROM_ADDRESS_MQTT_PASSWORD, (uint8_t *)MQTT_PASSWORD, 32);
} }
void set_mqtt_username(String username) { void set_mqtt_username(String username)
username.toCharArray(MQTT_USERNAME,32); {
username.toCharArray(MQTT_USERNAME, 32);
ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 20); ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_USERNAME, (uint8_t *)MQTT_USERNAME, 20);
} }
void set_mqtt_password(String password) { void set_mqtt_password(String password)
password.toCharArray(MQTT_PASSWORD,32); {
ESPMega_FRAM.write(EEPROM_ADDRESS_MQTT_PASSWORD, (uint8_t *)MQTT_PASSWORD, 20); 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); 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; MQTT_USE_AUTH = use_auth;
ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH); ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH);
} }