Skip to content

Commit 610f6ba

Browse files
committed
Fix configuration
1 parent 8754ea5 commit 610f6ba

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

html/template.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
setInterval(function() {
2222
$("[data-ajaxurl]").each(function(index) {
2323
var ele = $(this);
24-
$.ajax(ele.data("ajaxurl")).complete(function (data, textStatus, jqXHR) {
24+
$.ajax(ele.data("ajaxurl")).success(function (data) {
2525
ele.html(data);
2626
});
2727
});
@@ -218,7 +218,7 @@ <h3>{title}</h3><hr/>
218218
<div class="form-group">
219219
<label for="{key}" class="col-sm-2 control-label">{key}</label>
220220
<div class="col-sm-8">
221-
<input type="{type}" class="form-control" id="{key}" placeholder="" value="{value}">
221+
<input type="{type}" class="form-control" name="{key}" placeholder="" value="{value}">
222222
</div>
223223
</div>
224224
<!-- split:config_entry -->

src/ConfigEntry.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ConfigEntry : public MqttEntry {
2222
return value;
2323
}
2424
}
25-
25+
2626
int getInt(String key, int defaultValue) {
2727
return getString(key, String(defaultValue)).toInt();
2828
}
@@ -33,6 +33,7 @@ class ConfigEntry : public MqttEntry {
3333

3434
void reset() {
3535
SPIFFS.remove("/config.cfg");
36+
// ToDo: Remove all entries
3637
}
3738

3839
protected:
@@ -44,6 +45,7 @@ class ConfigEntry : public MqttEntry {
4445

4546
private:
4647
void load() {
48+
Serial.println("Load config");
4749
File f = SPIFFS.open("/config.cfg", "r");
4850
if (f) {
4951
while(f.available()) {
@@ -52,6 +54,10 @@ class ConfigEntry : public MqttEntry {
5254
String key = line.substring(0, pos);
5355
String value = line.substring(pos+1, line.length()-1);
5456
set(key, value);
57+
Serial.print(" ");
58+
Serial.print(key);
59+
Serial.print(" = ");
60+
Serial.println(value);
5561
}
5662
f.close();
5763
}

src/EasyMqtt.h

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ class EasyMqtt : public MqttEntry {
2626
if(WiFi.status() == WL_DISCONNECTED) {
2727
debug("Connecting to wifi: " + config().getString("wifi.ssid", ""));
2828
WiFi.mode(WIFI_STA);
29-
WiFi.begin(config().getString("wifi.ssid", "").c_str(), config().getString("wifi.password", "").c_str());
29+
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);
3039

3140
#ifdef DEBUG
3241
WiFi.printDiag(Serial);
3342
#endif
3443

3544
int timer = 0;
36-
while ((WiFi.status() == WL_DISCONNECTED) && timer < 50) {
45+
while ((WiFi.status() == WL_DISCONNECTED) && timer < 60) {
3746
debug("Wifi Status", String(WiFi.status()));
3847
delay(500);
3948
timer++;
4049
}
41-
if(timer < 50 && WiFi.status() == WL_CONNECTED) {
50+
if(timer < 60 && WiFi.status() == WL_CONNECTED) {
4251
debug("WiFi connected");
4352
debug("IP address", WiFi.localIP().toString());
4453
} else {
@@ -54,17 +63,31 @@ class EasyMqtt : public MqttEntry {
5463

5564
void connectMqtt() {
5665
if (!mqttClient.connected() && mqttDelay < millis()) {
57-
debug("Configure MQTT");
66+
debug("Connecting to MQTT");
5867
mqttClient.setClient(wifiClient);
5968
mqttClient.setCallback([&](const char* topic, uint8_t* payload, unsigned int length) {
6069
each([=](MqttEntry* entry){
6170
entry->callback(topic, payload, length);
6271
});
6372
});
64-
mqttClient.setServer(config().getString("mqtt.host", "").c_str(), config().getInt("mqtt.port", 1883));
73+
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);
6589

66-
debug("Connecting to MQTT");
67-
if (mqttClient.connect(deviceId.c_str(), config().getString("mqtt.username", "").c_str(), config().getString("mqtt.password", "").c_str())) {
90+
if (mqttClient.connect(deviceId.c_str(), user, pass)) {
6891
debug("Connected to MQTT");
6992

7093
setPublishFunction([&](MqttEntry* entry, String message){
@@ -82,7 +105,7 @@ class EasyMqtt : public MqttEntry {
82105
} else {
83106
debug("Connection to MQTT failed, rc", String(mqttClient.state()));
84107

85-
mqttDelay = millis() + 1000;
108+
mqttDelay = millis() + 5000;
86109
}
87110
}
88111
}
@@ -102,6 +125,9 @@ class EasyMqtt : public MqttEntry {
102125

103126
public:
104127
EasyMqtt() : MqttEntry("easyMqtt", mqttClient) {
128+
#ifdef DEBUG
129+
Serial.begin(115200);
130+
#endif
105131
deviceId = String(ESP.getChipId());
106132

107133
debug("test");

src/MqttEntry.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ class MqttEntry {
209209
* Create or get the sub topic with the name {name}
210210
*/
211211
MqttEntry & get(String name) {
212-
name.replace('.', '/');
213212
int pos = name.indexOf('/');
214213
if(pos < 0) {
215214
return operator[](name.c_str());

src/WebPortal.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class WebPortal {
106106
mqtt->each([&](MqttEntry* entry) {
107107
if(entry->isOut() || entry->isIn()) {
108108
page += FPSTR(HTML_API_DOC);
109-
page.replace("{path}", getName(entry));
109+
page.replace("{path}", getRestPath(entry));
110110
}
111111
});
112112
page += FPSTR(HTML_MAIN6);
@@ -117,20 +117,24 @@ class WebPortal {
117117
}
118118

119119
void handleSaveConfig() {
120+
Serial.println("Save");
120121
mqtt->get("config").each([&](MqttEntry* entry) {
121122
String name = getName(entry);
122-
name = name.substring(7);
123+
name = name.substring(8);
123124
name.replace("/", ".");
124125
// webServer->hasArg(name.c_str());
125126
entry->setValue(webServer->arg(name.c_str()));
127+
Serial.print(name);
128+
Serial.print(" = ");
129+
Serial.println(webServer->arg(name.c_str()));
126130
});
127131
webServer->sendHeader("Location", String("/"), true);
128132
webServer->send(302, "text/plain", "");
129133
ESP.restart();
130134
}
131135

132136
void handleRest() {
133-
MqttEntry* entry = &mqtt->get(webServer->uri().substring(5));
137+
MqttEntry* entry = &mqtt->get(webServer->uri().substring(6));
134138
if(webServer->method() == HTTP_GET && entry->isIn()) {
135139
webServer->send(200, "text/plain", entry->getValue());
136140
} else if(webServer->method() == HTTP_POST && entry->isOut()) {

src/html.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#ifndef html_h
22
#define html_h
33

4-
const char HTML_MAIN1[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Device Info - {device_id}</title><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp\" crossorigin=\"anonymous\"></head><body><script type=\"application/javascript\">function sendValue(input) {$.ajax({url: input.dataset.url, method: \"POST\", data: $(input).closest(\"form\").find(\"input\").val()}).complete(function (data, textStatus, jqXHR) {alert(textStatus);});}setInterval(function() {$(\"[data-ajaxurl]\").each(function(index) {var ele = $(this);$.ajax(ele.data(\"ajaxurl\")).complete(function (data, textStatus, jqXHR) {ele.html(data);});});}, 1000);</script><div class=\"container\"><h1>Device Info - {device_id}</h1><ul class=\"nav nav-tabs\" role=\"tablist\"><li role=\"presentation\" class=\"active\"><a href=\"#devices\" aria-controls=\"devices\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-dashboard\" aria-hidden=\"true\"></span> Devices</a></li><li role=\"presentation\"><a href=\"#input\" aria-controls=\"input\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-pencil\" aria-hidden=\"true\"></span> Input</a></li><li role=\"presentation\"><a href=\"#config\" aria-controls=\"config\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-cog\" aria-hidden=\"true\"></span> Config</a></li><li role=\"presentation\"><a href=\"#about\" aria-controls=\"about\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-book\" aria-hidden=\"true\"></span> About</a></li></ul><div class=\"tab-content\" style=\"border-style: solid; border-color: #ddd; border-width: 0px 1px 1px 1px\"><div role=\"tabpanel\" class=\"tab-pane active\" id=\"devices\"><br /><div class=\"container-fluid\"><div class=\"row\">";
4+
const char HTML_MAIN1[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><title>Device Info - {device_id}</title><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\"><link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp\" crossorigin=\"anonymous\"></head><body><script type=\"application/javascript\">function sendValue(input) {$.ajax({url: input.dataset.url, method: \"POST\", data: $(input).closest(\"form\").find(\"input\").val()}).complete(function (data, textStatus, jqXHR) {alert(textStatus);});}setInterval(function() {$(\"[data-ajaxurl]\").each(function(index) {var ele = $(this);$.ajax(ele.data(\"ajaxurl\")).success(function (data) {ele.html(data);});});}, 1000);</script><div class=\"container\"><h1>Device Info - {device_id}</h1><ul class=\"nav nav-tabs\" role=\"tablist\"><li role=\"presentation\" class=\"active\"><a href=\"#devices\" aria-controls=\"devices\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-dashboard\" aria-hidden=\"true\"></span> Devices</a></li><li role=\"presentation\"><a href=\"#input\" aria-controls=\"input\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-pencil\" aria-hidden=\"true\"></span> Input</a></li><li role=\"presentation\"><a href=\"#config\" aria-controls=\"config\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-cog\" aria-hidden=\"true\"></span> Config</a></li><li role=\"presentation\"><a href=\"#about\" aria-controls=\"about\" role=\"tab\" data-toggle=\"tab\"><span class=\"glyphicon glyphicon-book\" aria-hidden=\"true\"></span> About</a></li></ul><div class=\"tab-content\" style=\"border-style: solid; border-color: #ddd; border-width: 0px 1px 1px 1px\"><div role=\"tabpanel\" class=\"tab-pane active\" id=\"devices\"><br /><div class=\"container-fluid\"><div class=\"row\">";
55
const char HTML_SENSOR[] PROGMEM = "<div class=\"col-md-4\"><div class=\"panel panel-info\"><div class=\"panel-heading\"><strong>{name}</strong></div><div class=\"panel-body\" data-ajaxurl=\"{path}\" style=\"font-size: 24px;\">{value}</div></div></div>";
66
const char HTML_VALUE_ON[] PROGMEM = "<span class=\"label label-success\">{value}</span>";
77
const char HTML_VALUE_OFF[] PROGMEM = "<span class=\"label label-danger\">{value}</span>";
88
const char HTML_MAIN2[] PROGMEM = "</div></div></div><div role=\"tabpanel\" class=\"tab-pane\" id=\"input\"><br /><div class=\"container-fluid\"><div class=\"row\">";
99
const char HTML_INPUT[] PROGMEM = "<div class=\"col-md-4\"><div class=\"panel panel-success\"><div class=\"panel-heading\"><strong>{name}</strong></div><div class=\"panel-body\" style=\"font-size: 24px;\"><form><div class=\"input-group\"><input type=\"text\" class=\"form-control\" placeholder=\"value\"><span class=\"input-group-btn\"><button class=\"btn btn-default\" type=\"button\" data-url=\"{name}\" onclick=\"sendValue(this);\">Send</button></span></div></form></div></div></div>";
1010
const char HTML_MAIN3[] PROGMEM = "</div></div></div><div role=\"tabpanel\" class=\"tab-pane\" id=\"config\"><div class=\"container-fluid\"><div class=\"row\"><div class=\"col-md-12\"><form class=\"form-horizontal\" action=\"/save\" method=\"POST\">";
1111
const char HTML_CONFIG_HEADER[] PROGMEM = "<br /><h3>{title}</h3><hr/>";
12-
const char HTML_CONFIG_ENTRY[] PROGMEM = "<div class=\"form-group\"><label for=\"{key}\" class=\"col-sm-2 control-label\">{key}</label><div class=\"col-sm-8\"><input type=\"{type}\" class=\"form-control\" id=\"{key}\" placeholder=\"\" value=\"{value}\"></div></div>";
12+
const char HTML_CONFIG_ENTRY[] PROGMEM = "<div class=\"form-group\"><label for=\"{key}\" class=\"col-sm-2 control-label\">{key}</label><div class=\"col-sm-8\"><input type=\"{type}\" class=\"form-control\" name=\"{key}\" placeholder=\"\" value=\"{value}\"></div></div>";
1313
const char HTML_MAIN4[] PROGMEM = "<hr/><div class=\"form-group\"><div class=\"col-sm-offset-9 col-sm-2\"><button type=\"submit\" class=\"btn btn-default\">Save</button></div></div></form></div></div></div></div><div role=\"tabpanel\" class=\"tab-pane\" id=\"about\"><br /><div class=\"container-fluid\"><div class=\"row\"><div class=\"col-md-12\"><h1>About</h1><p>Device is using EasyMqtt version 0.0.1</p><dl class=\"dl-horizontal\"><dt>Device Id</dt><dd>{device_id}</dd><dt>Topic</dt><dd>{topic}</dd></dl><hr /><h3>MQTT API:</h3><ul class=\"list-group\">";
1414
const char HTML_MAIN5[] PROGMEM = "</ul><hr /><h3>Rest API:</h3><ul class=\"list-group\">";
1515
const char HTML_API_DOC[] PROGMEM = "<li class=\"list-group-item\">{path}</li>";

0 commit comments

Comments
 (0)