Skip to content

Commit 79b588e

Browse files
author
Bjarne Loft
committed
Reduce memory of the web page, and fix ArduinoOTA
1 parent b9ab729 commit 79b588e

File tree

3 files changed

+87
-57
lines changed

3 files changed

+87
-57
lines changed

src/EasyMqtt.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "Entry.h"
66
#include "ConfigEntry.h"
77
#include "WebPortal.h"
8+
#include <ArduinoOTA.h>
9+
810

911
/**
1012
Handle connections to mqtt
@@ -112,7 +114,7 @@ EasyMqtt::EasyMqtt() : Entry("easyMqtt") {
112114
if(strlen(password) > 0) {
113115
ArduinoOTA.setPassword(password);
114116
}
115-
ArduinoOTA.setHostname(deviceId);
117+
ArduinoOTA.setHostname(deviceId.c_str());
116118
ArduinoOTA.begin();
117119

118120
get("$system").setInterval(300); // every 5 min

src/WebPortal.cpp

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,75 +37,98 @@ void WebPortal::setup(Entry& mqttEntry, ConfigEntry& config, NTPClient& ntpClien
3737

3838
void WebPortal::handleRoot() {
3939
if(!auth()) return;
40-
String page = "";
41-
page += FPSTR(HTML_MAIN1);
42-
// Sensors
40+
webServer->send(200, "text/html", "");
41+
42+
webServer->sendContent_P(HTML_MAIN1);
43+
4344
mqtt->each([&](Entry* entry) {
44-
if(!entry->isInternal() || webServer->arg("show").equals("all")) {
45-
String value = entry->getValue();
46-
if(value != NULL) {
47-
page += FPSTR(HTML_SENSOR);
48-
if(getName(entry).endsWith("password")) {
49-
page.replace("{value}", "***");
50-
} else {
51-
value.replace("{value}", entry->getValue());
52-
page.replace("{value}", value);
53-
}
54-
page.replace("{last_updated}", time(entry->getLastUpdate()));
55-
}
56-
if(entry->isOut()) {
57-
page += FPSTR(HTML_INPUT);
58-
}
59-
page.replace("{name}", getName(entry));
60-
page.replace("{path}", getRestPath(entry));
61-
}
45+
if(!entry->isInternal() || webServer->arg("show").equals("all")) {
46+
sendSensor(entry);
47+
}
6248
});
6349

64-
// Config
65-
page += FPSTR(HTML_MAIN2);
66-
page += FPSTR(HTML_CONFIG_HEADER);
67-
page.replace("{title}", "General");
50+
webServer->sendContent_P(HTML_MAIN2);
51+
52+
webServer->sendContent_P(HTML_CONFIG_HEADER);
6853
config->each([&](Entry* entry) {
69-
if(entry == config) return;
70-
page += FPSTR(HTML_CONFIG_ENTRY);
71-
String name = getName(config, entry).substring(1);
72-
page.replace("{key}", name);
73-
if(name.endsWith("password")) {
74-
page.replace("{type}", "password");
75-
page.replace("{value}", "");
76-
} else {
77-
page.replace("{type}", "text");
78-
page.replace("{value}", entry->getValue());
79-
}
54+
if(entry != config) {
55+
sendConfig(entry);
56+
}
8057
});
8158

82-
// About
83-
page += FPSTR(HTML_MAIN3);
59+
webServer->sendContent_P(HTML_MAIN3);
60+
8461
mqtt->each([&](Entry* entry) {
85-
if(entry->isOut() || entry->isIn()) {
86-
page += FPSTR(HTML_API_DOC);
87-
String path = entry->getTopic();
88-
if(entry->isOut()) path += "<span class=\"badge\">Set</span>";
89-
if(entry->isIn()) path += "<span class=\"badge\">Get</span>";
90-
page.replace("{path}", path);
91-
}
62+
if(entry->isOut() || entry->isIn()) {
63+
sendMqttApi(entry);
64+
}
9265
});
9366

94-
page += FPSTR(HTML_MAIN4);
67+
webServer->sendContent_P(HTML_MAIN4);
68+
9569
mqtt->each([&](Entry* entry) {
96-
if(entry->isOut() || entry->isIn()) {
97-
page += FPSTR(HTML_API_DOC);
98-
String path = getRestPath(entry);
99-
if(entry->isOut()) path += "<span class=\"badge\">POST</span>";
100-
if(entry->isIn()) path += "<span class=\"badge\">GET</span>";
101-
page.replace("{path}", path);
102-
}
70+
if(entry->isOut() || entry->isIn()) {
71+
sendRestApi(entry);
72+
}
10373
});
10474

105-
page += FPSTR(HTML_MAIN5);
75+
String page = FPSTR(HTML_MAIN5);
10676
page.replace("{device_id}", mqtt->get("$system")["deviceId"].getValue());
10777
page.replace("{topic}", mqtt->getTopic());
108-
webServer->send(200, "text/html", page);
78+
webServer->sendContent(page);
79+
}
80+
81+
void WebPortal::sendSensor(Entry* entry) {
82+
String page = "";
83+
String value = entry->getValue();
84+
if(value != NULL) {
85+
page += FPSTR(HTML_SENSOR);
86+
if(getName(entry).endsWith("password")) {
87+
page.replace("{value}", "***");
88+
} else {
89+
value.replace("{value}", entry->getValue());
90+
page.replace("{value}", value);
91+
}
92+
page.replace("{last_updated}", time(entry->getLastUpdate()));
93+
}
94+
if(entry->isOut()) {
95+
page += FPSTR(HTML_INPUT);
96+
}
97+
page.replace("{name}", getName(entry));
98+
page.replace("{path}", getRestPath(entry));
99+
webServer->sendContent(page);
100+
}
101+
102+
void WebPortal::sendConfig(Entry* entry) {
103+
String page = FPSTR(HTML_CONFIG_ENTRY);
104+
String name = getName(config, entry).substring(1);
105+
page.replace("{key}", name);
106+
if(name.endsWith("password")) {
107+
page.replace("{type}", "password");
108+
page.replace("{value}", "");
109+
} else {
110+
page.replace("{type}", "text");
111+
page.replace("{value}", entry->getValue());
112+
}
113+
webServer->sendContent(page);
114+
}
115+
116+
void WebPortal::sendMqttApi(Entry* entry) {
117+
String page = FPSTR(HTML_API_DOC);
118+
String path = entry->getTopic();
119+
if(entry->isOut()) path += "<span class=\"badge\">Set</span>";
120+
if(entry->isIn()) path += "<span class=\"badge\">Get</span>";
121+
page.replace("{path}", path);
122+
webServer->sendContent(page);
123+
}
124+
125+
void WebPortal::sendRestApi(Entry* entry) {
126+
String page = FPSTR(HTML_API_DOC);
127+
String path = getRestPath(entry);
128+
if(entry->isOut()) path += "<span class=\"badge\">POST</span>";
129+
if(entry->isIn()) path += "<span class=\"badge\">GET</span>";
130+
page.replace("{path}", path);
131+
webServer->sendContent(page);
109132
}
110133

111134
void WebPortal::handleRest() {
@@ -170,7 +193,7 @@ String WebPortal::time(long time) {
170193
bool WebPortal::auth() {
171194
char pass[32];
172195
config->getCString("password", "", pass);
173-
if (stelen(pass) > 0 && !webServer->authenticate("admin", pass)) {
196+
if (strlen(pass) > 0 && !webServer->authenticate("admin", pass)) {
174197
webServer->requestAuthentication();
175198
return false;
176199
}

src/WebPortal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class WebPortal {
2222
String time(long time);
2323
bool auth();
2424

25+
void sendSensor(Entry* entry);
26+
void sendConfig(Entry* entry);
27+
void sendMqttApi(Entry* entry);
28+
void sendRestApi(Entry* entry);
29+
2530
public:
2631
WebPortal();
2732

0 commit comments

Comments
 (0)