Skip to content

Commit 5538615

Browse files
committed
Fix WiFi, still problem with MQTT
1 parent cf25f4d commit 5538615

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

src/ConfigEntry.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ class ConfigEntry : public MqttEntry {
2424
}
2525
}
2626

27-
char* getCString(String key, String defaultValue) {
28-
String value = getString(key, defaultValue);
29-
char cValue[value.length() + 1];
30-
memset(cValue, 0, value.length() + 1);
31-
for(int i=0; i < value.length(); i++) {
32-
cValue[i] = value.charAt(i);
33-
}
34-
return cValue;
27+
const char* getCString(String key, String defaultValue) {
28+
return getString(key, defaultValue).c_str();
3529
}
3630

3731
int getInt(String key, int defaultValue) {
@@ -55,10 +49,6 @@ class ConfigEntry : public MqttEntry {
5549
return key;
5650
}
5751

58-
String toString(MqttEntry* entry) {
59-
return getKey(entry) + "=" + entry->getValue();
60-
}
61-
6252
private:
6353
void load() {
6454
File f = SPIFFS.open("/config.cfg", "r");
@@ -67,7 +57,7 @@ class ConfigEntry : public MqttEntry {
6757
String line = f.readStringUntil('\n');
6858
int pos = line.indexOf("=");
6959
String key = line.substring(0, pos);
70-
String value = line.substring(pos+1);
60+
String value = line.substring(pos+1, line.length()-1);
7161
set(key, value);
7262
}
7363
f.close();
@@ -79,7 +69,9 @@ class ConfigEntry : public MqttEntry {
7969
if (f) {
8070
each([&](MqttEntry* entry) {
8171
if(entry->getTopic() != getTopic()) {
82-
f.println(toString(entry).c_str());
72+
f.print(getKey(entry));
73+
f.print("=");
74+
f.println(entry->getValue());
8375
}
8476
});
8577
f.close();

src/EasyMqtt.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,47 @@ class EasyMqtt : public MqttEntry {
2222
Handle connections to mqtt
2323
*/
2424
void connect() {
25-
if(WiFi.status() != WL_CONNECTED) {
25+
if(WiFi.status() == WL_DISCONNECTED) {
2626
debug("Connecting to wifi: " + config().getString("wifi.ssid", ""));
2727
WiFi.mode(WIFI_STA);
28-
WiFi.begin(config().getCString("wifi.ssid", ""), config().getCString("wifi.password", ""));
28+
WiFi.begin(config().getString("wifi.ssid", "").c_str(), config().getString("wifi.password", "").c_str());
29+
30+
#ifdef DEBUG
31+
WiFi.printDiag(Serial);
32+
#endif
2933

3034
int timer = 0;
31-
while ((WiFi.status() != WL_CONNECTED) && timer < 10) {
35+
while ((WiFi.status() == WL_DISCONNECTED) && timer < 50) {
36+
Serial.println(WiFi.status());
3237
delay(500);
3338
timer++;
3439
// ToDo: handle timeout, and create AP
3540
}
36-
if(timer < 10) {
41+
if(timer < 50 && WiFi.status() == WL_CONNECTED) {
3742
debug("WiFi connected");
43+
debug("IP address", WiFi.localIP().toString());
3844
} else {
3945
debug("WiFi connection timeout - Setup AP");
40-
// ToDo: create AP
4146
WiFi.mode(WIFI_AP);
42-
WiFi.softAP(config().getCString("wifi.ap", "EasyMqtt"), "123456");
47+
WiFi.softAP(config().getString("wifi.ap", "EasyMqtt").c_str(), "123456");
48+
debug("IP address", WiFi.softAPIP().toString());
4349
}
44-
debug("IP address", WiFi.localIP().toString());
4550
debug("devideId", deviceId);
4651
webPortal.setup(*this);
4752
}
4853
if(mqttClient.state() == MQTT_DISCONNECTED) {
49-
// Setup MQTT
54+
debug("Configure MQTT");
5055
mqttClient.setClient(wifiClient);
5156
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
5257
each([=](MqttEntry* entry){
5358
entry->callback(topic, payload, length);
5459
});
5560
});
56-
mqttClient.setServer(config().getCString("mqtt.host", ""), config().getInt("mqtt.port", 1883));
61+
mqttClient.setServer(config().getString("mqtt.host", "").c_str(), config().getInt("mqtt.port", 1883));
5762
}
5863
if (!mqttClient.connected()) {
5964
debug("Connecting to MQTT");
60-
if (mqttClient.connect(deviceId.c_str(), config().getCString("mqtt.username", ""), config().getCString("mqtt.password", ""))) {
65+
if (mqttClient.connect(deviceId.c_str(), config().getString("mqtt.username", "").c_str(), config().getString("mqtt.password", "").c_str())) {
6166
debug("Connected to MQTT");
6267

6368
setPublishFunction([&](MqttEntry* entry, String message){

src/WebPortal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class WebPortal {
8484
} else {
8585
page.replace("{type}", "text");
8686
}
87-
page.replace("{name}", name);
87+
page.replace("{key}", name);
8888
page.replace("{value}", entry->getValue());
8989
});
9090

0 commit comments

Comments
 (0)