Skip to content

Commit ca0576b

Browse files
committed
Merge cahnges
2 parents 1d3bbfb + 73aee63 commit ca0576b

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
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 & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +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 name = config().getString("device.name", deviceId);
35-
//WiFi.hostname("WebPool"); // Set the name of the device
36-
37-
String sPass = config().getString("wifi.password", "");
38-
char pass[sPass.length() + 1];
39-
strcpy(pass, sPass.c_str());
40-
41-
WiFi.begin(ssid, pass);
30+
WiFi.begin(config().getCString("wifi.ssid", ""), config().getCString("wifi.password", ""));
4231

4332
#ifdef DEBUG
4433
WiFi.printDiag(Serial);
@@ -56,7 +45,7 @@ class EasyMqtt : public MqttEntry {
5645
} else {
5746
debug("WiFi connection timeout - Setup AP");
5847
WiFi.mode(WIFI_AP);
59-
WiFi.softAP(config().getString("wifi.ap", "EasyMqtt").c_str(), "123456");
48+
WiFi.softAP(config().getCString("wifi.ap", "EasyMqtt"), "123456");
6049
debug("IP address", WiFi.softAPIP().toString());
6150
}
6251
debug("devideId", deviceId);
@@ -74,23 +63,9 @@ class EasyMqtt : public MqttEntry {
7463
});
7564
});
7665

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

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

9671
setPublishFunction([&](MqttEntry* entry, String message){
@@ -178,6 +153,19 @@ class EasyMqtt : public MqttEntry {
178153
return *configEntry;
179154
}
180155

156+
void debug(String msg) {
157+
#ifdef DEBUG
158+
Serial.println(msg);
159+
#endif
160+
if(mqttClient.connected()) {
161+
get("system/debug").publish(msg);
162+
}
163+
}
164+
165+
void debug(String key, String value) {
166+
debug(key + " = " + value);
167+
}
168+
181169
String getDeviceId() {
182170
return deviceId;
183171
}

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)