microcontroller mqtt auth storage

This commit is contained in:
Siwat Sirichai 2023-11-07 12:19:35 +07:00
parent d0f2c82ee5
commit a90805b171
3 changed files with 53 additions and 20 deletions

View File

@ -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];
@ -203,6 +204,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;
@ -1077,4 +1081,26 @@ uint8_t ac_get_mode()
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_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);
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 eeprom_mqtt_useauth_retrieve() {
MQTT_USE_AUTH = ESPMega_FRAM.read8(EEPROM_ADDRESS_MQTT_USEAUTH);
}
void set_mqtt_useauth(bool use_auth) {
MQTT_USE_AUTH = use_auth;
ESPMega_FRAM.write8(EEPROM_ADDRESS_MQTT_USEAUTH, MQTT_USE_AUTH);
}

View File

@ -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,10 @@ 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();
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);

View File

@ -17,7 +17,7 @@
<p class="config_title">BMS Server - Port</p>
<input type="text" id="bms_port" name="bms_port" class="conf_txt" value="1883"><br>
<label class="container">Authentication
<input type="checkbox" id="bms_useauth" checked="checked">
<input type="checkbox" name="bms_useauth" id="bms_useauth" checked="checked" value="yes">
<span class="checkmark"></span>
</label>
<p class="config_title">BMS Server - Username</p>