Skip to content

Commit 73aee63

Browse files
committed
Convert lastValue to char*
1 parent 610f6ba commit 73aee63

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

src/ConfigEntry.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ class ConfigEntry : public MqttEntry {
1414
});
1515
}
1616

17-
String getString(String key, String defaultValue) {
18-
String value = get(key).getValue();
17+
const char* getCString(String key, const char* defaultValue) {
18+
const char * value = get(key).getCValue();
1919
if(value == NULL) {
2020
return defaultValue;
2121
} else {
2222
return value;
2323
}
2424
}
2525

26+
String getString(String key, String defaultValue) {
27+
return String(getCString(key, defaultValue.c_str()));
28+
}
29+
2630
int getInt(String key, int defaultValue) {
2731
return getString(key, String(defaultValue)).toInt();
2832
}
@@ -45,7 +49,7 @@ class ConfigEntry : public MqttEntry {
4549

4650
private:
4751
void load() {
48-
Serial.println("Load config");
52+
Serial.println("Load config");
4953
File f = SPIFFS.open("/config.cfg", "r");
5054
if (f) {
5155
while(f.available()) {
@@ -54,10 +58,10 @@ class ConfigEntry : public MqttEntry {
5458
String key = line.substring(0, pos);
5559
String value = line.substring(pos+1, line.length()-1);
5660
set(key, value);
57-
Serial.print(" ");
58-
Serial.print(key);
59-
Serial.print(" = ");
60-
Serial.println(value);
61+
Serial.print(" ");
62+
Serial.print(key);
63+
Serial.print(" = ");
64+
Serial.println(value);
6165
}
6266
f.close();
6367
}

src/EasyMqtt.h

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,7 @@ class EasyMqtt : public MqttEntry {
2727
debug("Connecting to wifi: " + config().getString("wifi.ssid", ""));
2828
WiFi.mode(WIFI_STA);
2929

30-
String sSsid = config().getString("wifi.ssid", "");
31-
char ssid[sSsid.length() + 1];
32-
strcpy(ssid, sSsid.c_str());
33-
34-
String sPass = config().getString("wifi.password", "");
35-
char pass[sPass.length() + 1];
36-
strcpy(pass, sPass.c_str());
37-
38-
WiFi.begin(ssid, pass);
30+
WiFi.begin(config().getCString("wifi.ssid", ""), config().getCString("wifi.password", ""));
3931

4032
#ifdef DEBUG
4133
WiFi.printDiag(Serial);
@@ -53,7 +45,7 @@ class EasyMqtt : public MqttEntry {
5345
} else {
5446
debug("WiFi connection timeout - Setup AP");
5547
WiFi.mode(WIFI_AP);
56-
WiFi.softAP(config().getString("wifi.ap", "EasyMqtt").c_str(), "123456");
48+
WiFi.softAP(config().getCString("wifi.ap", "EasyMqtt"), "123456");
5749
debug("IP address", WiFi.softAPIP().toString());
5850
}
5951
debug("devideId", deviceId);
@@ -71,23 +63,9 @@ class EasyMqtt : public MqttEntry {
7163
});
7264
});
7365

74-
String sHost = config().getString("mqtt.host", "");
75-
char host[sHost.length() + 1];
76-
strcpy(host, sHost.c_str());
77-
78-
int port = config().getInt("mqtt.port", 1883);
79-
80-
String sUser = config().getString("mqtt.username", "");
81-
char user[sUser.length() + 1];
82-
strcpy(user, sUser.c_str());
83-
84-
String sPass = config().getString("mqtt.password", "");
85-
char pass[sPass.length() + 1];
86-
strcpy(pass, sPass.c_str());
87-
88-
mqttClient.setServer(host, port);
66+
mqttClient.setServer(config().getCString("mqtt.host", ""), config().getInt("mqtt.port", 1883));
8967

90-
if (mqttClient.connect(deviceId.c_str(), user, pass)) {
68+
if (mqttClient.connect(deviceId.c_str(), config().getCString("mqtt.username", ""), config().getCString("mqtt.password", ""))) {
9169
debug("Connected to MQTT");
9270

9371
setPublishFunction([&](MqttEntry* entry, String message){
@@ -110,19 +88,6 @@ class EasyMqtt : public MqttEntry {
11088
}
11189
}
11290

113-
void debug(String msg) {
114-
#ifdef DEBUG
115-
Serial.println(msg);
116-
#endif
117-
if(mqttClient.connected()) {
118-
get("system/debug").publish(msg);
119-
}
120-
}
121-
122-
void debug(String key, String value) {
123-
debug(key + " = " + value);
124-
}
125-
12691
public:
12792
EasyMqtt() : MqttEntry("easyMqtt", mqttClient) {
12893
#ifdef DEBUG
@@ -173,6 +138,19 @@ class EasyMqtt : public MqttEntry {
173138
return *configEntry;
174139
}
175140

141+
void debug(String msg) {
142+
#ifdef DEBUG
143+
Serial.println(msg);
144+
#endif
145+
if(mqttClient.connected()) {
146+
get("system/debug").publish(msg);
147+
}
148+
}
149+
150+
void debug(String key, String value) {
151+
debug(key + " = " + value);
152+
}
153+
176154
String getDeviceId() {
177155
return deviceId;
178156
}

src/MqttEntry.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MqttEntry {
1717
int forceUpdate = -1;
1818
bool internal = false;
1919
unsigned long lastUpdate = 0;
20-
String lastValue = "";
20+
const char *lastValue = "";
2121

2222
PubSubClient* client = NULL;
2323

@@ -81,7 +81,7 @@ class MqttEntry {
8181
for (int i = 0; i < length; i++) {
8282
_payload += (char)payload[i];
8383
}
84-
if(!isIn() || _payload != lastValue) {
84+
if(!isIn() || _payload != getValue()) {
8585
update(_payload);
8686
}
8787
}
@@ -98,7 +98,7 @@ class MqttEntry {
9898
lastUpdate = time;
9999
String value = inFunction();
100100
if (value != "") {
101-
if (value != lastValue || force > getForce()) {
101+
if (value != getValue() || force > getForce()) {
102102
setValue(value);
103103
force = 0;
104104
}
@@ -167,12 +167,20 @@ class MqttEntry {
167167
* Get last value
168168
*/
169169
String getValue() {
170+
return String(lastValue);
171+
}
172+
173+
const char* getCValue() {
170174
return lastValue;
171175
}
172176

173177
void setValue(String value) {
174178
lastUpdate = millis();
175-
lastValue = value;
179+
180+
// free(str);
181+
// lastValue = (char *) malloc(value.length());
182+
// strcpy(lastValue, value.c_str());
183+
lastValue = value.c_str();
176184
publish(value);
177185
}
178186

src/WebPortal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "MqttEntry.h"
77
#include "html.h"
88

9+
// http://usemodj.com/2016/08/25/esp8266-arducam-5mp-ov5642-camera-wifi-video-streaming/
10+
911
class WebPortal {
1012
private:
1113
std::unique_ptr<ESP8266WebServer> webServer;
@@ -145,6 +147,14 @@ class WebPortal {
145147
}
146148
}
147149

150+
void handleDebug() {
151+
WiFiClient client = webServer->client();
152+
webServer->sendContent("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
153+
while(client.connected()) {
154+
client.write("this is a test\n");
155+
}
156+
}
157+
148158
void handleNotFound() {
149159
webServer->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
150160
webServer->sendHeader("Pragma", "no-cache");

0 commit comments

Comments
 (0)