Skip to content

Commit d9be872

Browse files
committed
Fix config issue
1 parent 1202ac2 commit d9be872

File tree

7 files changed

+16
-26
lines changed

7 files changed

+16
-26
lines changed

src/Config.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ char * Config::set(const char *key, const char *value){
8484
struct element * ele = elements;
8585
while(ele) {
8686
if(strcmp(key, ele->key) == 0) {
87-
free(ele->value);
87+
if(ele->value) {
88+
free(ele->value);
89+
}
8890
ele->value = (char*)malloc(strlen(value)+1);
8991
strcpy(ele->value, value);
9092
return ele->value;

src/EasyMqtt.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void EasyMqtt::connectWiFi() {
1616
debug("Connecting to wifi");
1717
WiFi.mode(WIFI_STA);
1818

19-
WiFi.begin(wifi_ssid, wifi_password);
19+
WiFi.begin(config().get("wifi.ssid", ""), config().get("wifi.password", ""));
2020

2121
#ifdef DEBUG
2222
WiFi.printDiag(Serial);
@@ -38,7 +38,7 @@ void EasyMqtt::connectWiFi() {
3838
debug("IP address", WiFi.softAPIP().toString());
3939
}
4040
debug("devideId", deviceId);
41-
webPortal.setup(*this, config(), ntp());
41+
webPortal.setup(this, &config(), &ntp());
4242
}
4343
}
4444

@@ -64,9 +64,9 @@ void EasyMqtt::connectMqtt() {
6464
});
6565
});
6666

67-
mqttClient.setServer(mqtt_host, mqtt_port);
67+
mqttClient.setServer(config().get("mqtt.host", ""), config().getInt("mqtt.port", 1883));
6868

69-
if (mqttClient.connect(deviceId.c_str(), mqtt_username, mqtt_password), get("$system")["online"].getTopic().c_str(), 1, 1, "OFF") {
69+
if (mqttClient.connect(deviceId.c_str(), config().get("mqtt.username", ""), config().get("mqtt.password", ""), get("$system")["online"].getTopic().c_str(), 1, 1, "OFF")) {
7070
debug("Connected to MQTT");
7171

7272
setPublishFunction([&](Entry* entry, String message){
@@ -186,8 +186,6 @@ NTPClient & EasyMqtt::ntp() {
186186
void EasyMqtt::wifi(const char* ssid, const char* password) {
187187
config().get("wifi.ssid", ssid);
188188
config().get("wifi.password", password);
189-
wifi_ssid = ssid;
190-
wifi_password = password;
191189
}
192190

193191
/**
@@ -199,10 +197,6 @@ void EasyMqtt::mqtt(const char* host, int port, const char* username, const char
199197
config().get("mqtt.port", String(port).c_str());
200198
config().get("mqtt.username", username);
201199
config().get("mqtt.password", password);
202-
mqtt_host = host;
203-
mqtt_port = port;
204-
mqtt_username = username;
205-
mqtt_password = password;
206200
}
207201

208202
/**

src/EasyMqtt.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ class EasyMqtt : public Entry {
2020
String deviceId = "deviceId";
2121
long mqttDelay = 0;
2222

23-
const char* wifi_ssid = "N/A";
24-
const char* wifi_password = "N/A";
25-
26-
const char* mqtt_host = "N/A";
27-
int mqtt_port = 1883;
28-
const char* mqtt_username = "N/A";
29-
const char* mqtt_password = "N/A";
30-
3123
protected:
3224
/**
3325
Handle connections to wifi

src/Entry.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ char *Entry::getValue() {
168168

169169
void Entry::setValue(const char *value) {
170170
lastUpdate = millis();
171-
free(lastValue);
171+
if(lastValue) {
172+
free(lastValue);
173+
}
172174
lastValue = (char*)malloc(strlen(value)+1);
173175
strcpy(lastValue, value);
174176
publish(value);

src/Entry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Entry {
1515
int interval = -1;
1616
int forceUpdate = -1;
1717
unsigned long lastUpdate = 0;
18-
char* lastValue = "";
18+
char* lastValue = NULL;
1919

2020
Entry* parent = NULL;
2121
Entry* next = NULL;

src/WebPortal.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ String WebPortal::getRestPath(Entry* entry) {
1818
WebPortal::WebPortal() {
1919
}
2020

21-
void WebPortal::setup(Entry& mqttEntry, Config& config, NTPClient& ntpClient) {
22-
mqtt = &mqttEntry;
23-
ntp = &ntpClient;
24-
config = config;
21+
void WebPortal::setup(Entry *mqttEntry, Config *config, NTPClient *ntpClient) {
22+
WebPortal::mqtt = mqttEntry;
23+
WebPortal::config = config;
24+
WebPortal::ntp = ntpClient;
2525
mqtt->debug("Setup Web Portal");
2626
webServer.reset(new ESP8266WebServer(80));
2727
webServer->on("/", std::bind(&WebPortal::handleRoot, this));

src/WebPortal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class WebPortal {
3030
public:
3131
WebPortal();
3232

33-
void setup(Entry& mqttEntry, Config& config, NTPClient& ntp);
33+
void setup(Entry *mqttEntry, Config *config, NTPClient *ntp);
3434

3535
void handleRoot();
3636
void handleRest();

0 commit comments

Comments
 (0)