Skip to content

Commit

Permalink
Support for long MQTT username and password
Browse files Browse the repository at this point in the history
  • Loading branch information
gskjold committed Sep 23, 2021
1 parent 7627d6c commit dce7b7e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
33 changes: 32 additions & 1 deletion src/AmsConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,17 @@ bool AmsConfiguration::hasConfig() {
return false;
}
break;
case 86:
configVersion = -1; // Prevent loop
if(relocateConfig86()) {
configVersion = EEPROM_CHECK_SUM;
return true;
} else {
configVersion = 0;
return false;
}
break;
break;
case EEPROM_CHECK_SUM:
return true;
default:
Expand All @@ -552,7 +563,7 @@ void AmsConfiguration::loadTempSensors() {
int address = EEPROM_TEMP_CONFIG_ADDRESS;
int c = 0;
int storedCount = EEPROM.read(address++);
Serial.print("SEnsors: ");
Serial.print("Sensors: ");
Serial.println(storedCount);
if(storedCount > 0 && storedCount <= 32) {
for(int i = 0; i < storedCount; i++) {
Expand Down Expand Up @@ -792,6 +803,26 @@ bool AmsConfiguration::loadConfig83(int address) {
return ret;
}

bool AmsConfiguration::relocateConfig86() {
MqttConfig86 mqtt86;
MqttConfig mqtt;
EEPROM.begin(EEPROM_SIZE);
EEPROM.get(CONFIG_MQTT_START_86, mqtt86);
strcpy(mqtt.host, mqtt86.host);
mqtt.port = mqtt86.port;
strcpy(mqtt.clientId, mqtt86.clientId);
strcpy(mqtt.publishTopic, mqtt86.publishTopic);
strcpy(mqtt.subscribeTopic, mqtt86.subscribeTopic);
strcpy(mqtt.username, mqtt86.username);
strcpy(mqtt.password, mqtt86.password);
mqtt.payloadFormat = mqtt86.payloadFormat;
mqtt.ssl = mqtt86.ssl;
EEPROM.put(CONFIG_MQTT_START, mqtt);
bool ret = EEPROM.commit();
EEPROM.end();
return ret;
}

bool AmsConfiguration::save() {
EEPROM.begin(EEPROM_SIZE);
EEPROM.put(EEPROM_CONFIG_ADDRESS, EEPROM_CHECK_SUM);
Expand Down
22 changes: 19 additions & 3 deletions src/AmsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
#include "Arduino.h"

#define EEPROM_SIZE 1024 * 3
#define EEPROM_CHECK_SUM 86 // Used to check if config is stored. Change if structure changes
#define EEPROM_CHECK_SUM 87 // Used to check if config is stored. Change if structure changes
#define EEPROM_CONFIG_ADDRESS 0
#define EEPROM_TEMP_CONFIG_ADDRESS 2048

#define CONFIG_SYSTEM_START 8
#define CONFIG_WIFI_START 16
#define CONFIG_MQTT_START 224
#define CONFIG_WEB_START 648
#define CONFIG_METER_START 784
#define CONFIG_DEBUG_START 824
#define CONFIG_GPIO_START 832
#define CONFIG_DOMOTICZ_START 856
#define CONFIG_NTP_START 872
#define CONFIG_ENTSOE_START 944
#define CONFIG_MQTT_START 1004

#define CONFIG_MQTT_START_86 224


struct SystemConfig {
uint8_t boardType;
Expand All @@ -35,7 +38,7 @@ struct WiFiConfig {
bool mdns;
}; // 204

struct MqttConfig {
struct MqttConfig86 {
char host[128];
uint16_t port;
char clientId[32];
Expand All @@ -47,6 +50,18 @@ struct MqttConfig {
bool ssl;
}; // 420

struct MqttConfig {
char host[128];
uint16_t port;
char clientId[32];
char publishTopic[64];
char subscribeTopic[64];
char username[128];
char password[256];
uint8_t payloadFormat;
bool ssl;
}; // 676

struct WebConfig {
uint8_t security;
char username[64];
Expand Down Expand Up @@ -320,6 +335,7 @@ class AmsConfiguration {

bool loadConfig82(int address);
bool loadConfig83(int address);
bool relocateConfig86();

int readString(int pAddress, char* pString[]);
int readInt(int pAddress, int *pValue);
Expand Down
10 changes: 5 additions & 5 deletions web/mqtt.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h6>MQTT</h6>
<div class="input-group-prepend">
<span class="input-group-text">Host</span>
</div>
<input type="text" class="form-control mc" name="h" value="{h}" maxlength="128"/>
<input type="text" class="form-control mc" name="h" value="{h}" maxlength="127"/>
</div>
</div>
<div class="col-xl-2 col-lg-3 col-md-3 col-sm-4">
Expand All @@ -25,31 +25,31 @@ <h6>MQTT</h6>
<div class="input-group-prepend">
<span class="input-group-text">Client ID</span>
</div>
<input type="text" class="form-control mc" name="i" value="{i}" maxlength="32"/>
<input type="text" class="form-control mc" name="i" value="{i}" maxlength="31"/>
</div>
</div>
<div class="col-xl-3 col-lg-4 col-md-6">
<div class="m-2 input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Publish topic</span>
</div>
<input type="text" class="form-control mc" name="t" value="{t}" maxlength="64"/>
<input type="text" class="form-control mc" name="t" value="{t}" maxlength="63"/>
</div>
</div>
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-6">
<div class="m-2 input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Username</span>
</div>
<input type="text" class="form-control mc" name="u" value="{u}" maxlength="64"/>
<input type="text" class="form-control mc" name="u" value="{u}" maxlength="127"/>
</div>
</div>
<div class="col-xl-3 col-lg-4 col-md-6 col-sm-6">
<div class="m-2 input-group input-group-sm">
<div class="input-group-prepend">
<span class="input-group-text">Password</span>
</div>
<input type="password" class="form-control mc" name="pw" value="{pw}" maxlength="64"/>
<input type="password" class="form-control mc" name="pw" value="{pw}" maxlength="255"/>
</div>
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
Expand Down

0 comments on commit dce7b7e

Please sign in to comment.