|
@@ -1,6 +1,6 @@
|
|
|
//extern ESP8266WebServer httpServer;
|
|
|
|
|
|
-static const char httpRoot[] PROGMEM =
|
|
|
+static const char httpMainPage[] PROGMEM =
|
|
|
R"(<html><body>
|
|
|
<h1><span id='devname'></span></h1>
|
|
|
<h3>WiFi Thermostat</h3>
|
|
@@ -54,7 +54,11 @@ static const char httpRoot[] PROGMEM =
|
|
|
<a href='/conf'>Base configuration</a><br>
|
|
|
<a href='/conf2'>Extended configuration</a><br>
|
|
|
<a href='/update'>Firmware update</a><br>
|
|
|
- <a href='/restart'>Restart</a>
|
|
|
+ <br>
|
|
|
+ <form id='restartForm' onsubmit='return res()'>
|
|
|
+ <input type='hidden' name='restart' value='1'>
|
|
|
+ <input type='submit' value='Restart'/>
|
|
|
+ </form>
|
|
|
<script>
|
|
|
function g(i) { return document.getElementById(i) };
|
|
|
var xhttp, updateTime, reqTime, reqFin;
|
|
@@ -87,6 +91,14 @@ static const char httpRoot[] PROGMEM =
|
|
|
var form = document.getElementById('offBtnFrm');
|
|
|
return transmit(form);
|
|
|
}
|
|
|
+ function res() {
|
|
|
+ rxhttp = new XMLHttpRequest();
|
|
|
+ rxhttp.timeout = 1000;
|
|
|
+ rxhttp.open('POST', 'restart');
|
|
|
+ rxhttp.send('');
|
|
|
+ rxhttp = null;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
function transmit(f) {
|
|
|
if (!xhttp) {
|
|
@@ -143,7 +155,9 @@ static const char httpConfPage[] PROGMEM =
|
|
|
<a href='/'>Home</a><br><br>
|
|
|
<input type='button' value='reload' onclick='return transmit()'/><br><br>
|
|
|
<form id='form1' onsubmit='return transmit(this)'>
|
|
|
-Device Name: <input type='text' name='devName' id='devName'/><br><br>
|
|
|
+Device Name: <input type='text' name='devName' id='devName'/> min 4 ch., also used as SSID in WiFi AP Mode<br><br>
|
|
|
+Host Name: <input type='text' name='hostName' id='hostName'/><br><br>
|
|
|
+WiFi AP-Mode Password: <input type='text' name='wifiappw' id='wifiappw'/> min 8 ch<br><br>
|
|
|
HTTP User *: <input type='text' name='httpUser' id='httpUser'/><br>
|
|
|
HTTP Password *: <input type='text' name='httpPass' id='httpPass'/><br><br>
|
|
|
HTTP set token: <input type='text' name='httpToken' id='httpToken'/><br>
|
|
@@ -215,7 +229,9 @@ Domoticz Out Topic *: <input type='text' name='domOutTop' id='domOutTop'/><br>
|
|
|
g('httpUser').value = data.httpUser;
|
|
|
g('httpPass').value = data.httpPass;
|
|
|
g('httpToken').value = data.httpToken;
|
|
|
- g('devName').value = data.devName
|
|
|
+ g('devName').value = data.devName;
|
|
|
+ g('hostName').value = data.hostName;
|
|
|
+ g('wifiappw').value = data.wifiappw;
|
|
|
g('mqttHost').value = data.mqttHost;
|
|
|
g('mqttPort').value = data.mqttPort;
|
|
|
g('mqttUser').value = data.mqttUser;
|
|
@@ -413,8 +429,8 @@ toggling I/A-Temp and Hum display*: <input type='checkbox' name='togTHdisp' id='
|
|
|
</script>
|
|
|
</body></html>)";
|
|
|
|
|
|
-void httpServerHandleRoot() {
|
|
|
- httpServer.send_P(200, "text/html", httpRoot);
|
|
|
+void httpServerHandleMainPage() {
|
|
|
+ httpServer.send_P(200, "text/html", httpMainPage);
|
|
|
}
|
|
|
|
|
|
void httpServerHandleConfPage() {
|
|
@@ -425,53 +441,49 @@ void httpServerHandleConf2Page() {
|
|
|
httpServer.send_P(200, "text/html", httpConf2Page);
|
|
|
}
|
|
|
|
|
|
-//void httpServerHandleNotFound() {
|
|
|
-// String message = "File Not Found\n\n";
|
|
|
-// message += "URI: ";
|
|
|
-// message += httpServer.uri();
|
|
|
-// message += "\nMethod: ";
|
|
|
-// message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
|
|
|
-// message += "\nArguments: ";
|
|
|
-// message += httpServer.args();
|
|
|
-// message += "\n";
|
|
|
-// for (uint8_t i = 0; i < httpServer.args(); i++) {
|
|
|
-// message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n";
|
|
|
-// }
|
|
|
-// httpServer.send(404, "text / plain", message);
|
|
|
-//}
|
|
|
-
|
|
|
void httpServerHandleNotFound() {
|
|
|
- // if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- // if (!httpServer.authenticate(http_user, http_pass))
|
|
|
- // return httpServer.requestAuthentication();
|
|
|
httpServer.send(404, "text/plain", "");
|
|
|
- //}
|
|
|
}
|
|
|
|
|
|
-void httpServerInit() {
|
|
|
- httpServer.on("/delconf", []() {
|
|
|
- Serial.println("httpServer.on /delconf");
|
|
|
+boolean httpIsAuthenticated() {
|
|
|
+ if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
+ if (!httpServer.authenticate(http_user, http_pass)) return false;
|
|
|
+ else return true;
|
|
|
+ }
|
|
|
+ else return true;
|
|
|
+}
|
|
|
+
|
|
|
+void httpSendUnauthorized() {
|
|
|
+ httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
+}
|
|
|
+
|
|
|
+void httpSend200OK() {
|
|
|
+ httpServer.send (200, "text/plain", "OK");
|
|
|
+}
|
|
|
+
|
|
|
+boolean httpCheckToken() {
|
|
|
+ if (http_token[0] != '\0') { // dont accept empty token
|
|
|
if (httpServer.hasArg("token")) {
|
|
|
char buf[20];
|
|
|
httpServer.arg("token").toCharArray(buf, 20);
|
|
|
- if (strcmp(buf, CLEARCONF_TOKEN) == 0) {
|
|
|
- // httpServer.send(200, "text / plain", "Token OK - deleting config");
|
|
|
- deleteConfig();
|
|
|
- }
|
|
|
- } //if
|
|
|
- // else {
|
|
|
- // httpServer.send(200, "text / plain", "not allowed");
|
|
|
- // }
|
|
|
+ if (strcmp(buf, http_token) == 0) return true;
|
|
|
+ else return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void httpServerInit() {
|
|
|
+ httpServer.on("/delconf", []() {
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("httpServer.on /delconf");
|
|
|
+ deleteConfig();
|
|
|
+ }
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
httpServer.on("/api", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
+ else {
|
|
|
DEBUG_PRINT("httpServer.on /api");
|
|
|
if (httpServer.hasArg("plusBtn")) {
|
|
|
setTempStepUp();
|
|
@@ -529,102 +541,72 @@ void httpServerInit() {
|
|
|
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
|
|
|
httpServer.send(200, "application/json", jsonchar);
|
|
|
}
|
|
|
- else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
- }
|
|
|
}); //httpServer.on /api
|
|
|
|
|
|
httpServer.on("/restart", []() {
|
|
|
- if (httpServer.hasArg("token")) {
|
|
|
- Serial.println("web triggered restart");
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("web triggered restart");
|
|
|
ESP.restart();
|
|
|
}
|
|
|
- else {
|
|
|
- httpServer.send (403, "text/plain", "FORBIDDEN");
|
|
|
- }
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
httpServer.on("/mqttReconnect", []() {
|
|
|
- if (httpServer.hasArg("token")) {
|
|
|
- Serial.println("web triggered mqttReconnect");
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("web triggered mqttReconnect");
|
|
|
mqttReconnect();
|
|
|
- httpServer.sendHeader("Location", String("/"), true);
|
|
|
- httpServer.send (302, "text/plain", "OK");
|
|
|
- }
|
|
|
- else {
|
|
|
- httpServer.send (403, "text/plain", "FORBIDDEN");
|
|
|
+ httpServer.sendHeader("Location", "/", true);
|
|
|
+ httpServer.send(303);
|
|
|
}
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
- httpServer.on("/setPoint", []() {
|
|
|
- if (httpServer.hasArg("token")) {
|
|
|
- char buf[20];
|
|
|
- httpServer.arg("token").toCharArray(buf, 20);
|
|
|
- if (strcmp(buf, http_token) == 0) {
|
|
|
- Serial.println("web triggered setPoint");
|
|
|
- if (httpServer.hasArg("value")) {
|
|
|
- char bufVal[20];
|
|
|
- httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
- float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
|
|
|
- setTempTo(valueFloat);
|
|
|
- httpServer.send (200, "text/plain", "OK");
|
|
|
- }
|
|
|
+ httpServer.on("/setTemp", []() {
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("web triggered setTemp");
|
|
|
+ if (httpServer.hasArg("value")) {
|
|
|
+ char bufVal[20];
|
|
|
+ httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
+ float valueFloat = round(atof(bufVal) * 2.0) / 2.0;
|
|
|
+ setTempTo(valueFloat);
|
|
|
+ httpSend200OK();
|
|
|
}
|
|
|
} //if
|
|
|
- else {
|
|
|
- httpServer.send (403, "text/plain", "FORBIDDEN");
|
|
|
- }
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
httpServer.on("/setMode", []() {
|
|
|
- if (httpServer.hasArg("token")) {
|
|
|
- char buf[20];
|
|
|
- httpServer.arg("token").toCharArray(buf, 20);
|
|
|
- if (strcmp(buf, http_token) == 0) {
|
|
|
- Serial.println("web triggered setMode");
|
|
|
- if (httpServer.hasArg("value")) {
|
|
|
- char bufVal[20];
|
|
|
- httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
- int valueInt = atoi(bufVal);
|
|
|
- if (valueInt >= 0 && valueInt <= 1) setHeatingmodeTo(valueInt);
|
|
|
- httpServer.send (200, "text/plain", "OK");
|
|
|
- }
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("web triggered setMode");
|
|
|
+ if (httpServer.hasArg("value")) {
|
|
|
+ char bufVal[20];
|
|
|
+ httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
+ int valueInt = atoi(bufVal);
|
|
|
+ if (valueInt >= 0 && valueInt <= 1) setHeatingmodeTo(valueInt);
|
|
|
+ httpSend200OK();
|
|
|
}
|
|
|
} //if
|
|
|
- else {
|
|
|
- httpServer.send (403, "text/plain", "FORBIDDEN");
|
|
|
- }
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
httpServer.on("/setPreset", []() {
|
|
|
- if (httpServer.hasArg("token")) {
|
|
|
- char buf[20];
|
|
|
- httpServer.arg("token").toCharArray(buf, 20);
|
|
|
- if (strcmp(buf, http_token) == 0) {
|
|
|
- Serial.println("web triggered setPreset");
|
|
|
- if (httpServer.hasArg("value")) {
|
|
|
- char bufVal[20];
|
|
|
- httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
- int valueInt = atoi(bufVal);
|
|
|
- if (valueInt >= 0 && valueInt <= 2) setPresetTo(valueInt);
|
|
|
- httpServer.send (200, "text/plain", "OK");
|
|
|
- }
|
|
|
+ if ( httpIsAuthenticated() || (!httpIsAuthenticated() && httpCheckToken()) ) {
|
|
|
+ //Serial.println("web triggered setPreset");
|
|
|
+ if (httpServer.hasArg("value")) {
|
|
|
+ char bufVal[20];
|
|
|
+ httpServer.arg("value").toCharArray(bufVal, 20);
|
|
|
+ int valueInt = atoi(bufVal);
|
|
|
+ if (valueInt >= 0 && valueInt <= 2) setPresetTo(valueInt);
|
|
|
+ httpSend200OK();
|
|
|
}
|
|
|
} //if
|
|
|
- else {
|
|
|
- httpServer.send (403, "text/plain", "FORBIDDEN");
|
|
|
- }
|
|
|
+ else httpSendUnauthorized();
|
|
|
});
|
|
|
|
|
|
httpServer.on("/confdata", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
- Serial.println("httpServer.on /confdata");
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
+ else {
|
|
|
+ //Serial.println("httpServer.on /confdata");
|
|
|
|
|
|
for (int i = 0; i < httpServer.args(); i++) {
|
|
|
char bufName[20];
|
|
@@ -633,14 +615,14 @@ void httpServerInit() {
|
|
|
httpServer.arg(i).toCharArray(bufValue, 101);
|
|
|
|
|
|
if (strlen(bufName) > 0) {
|
|
|
- Serial.print("web update ");
|
|
|
- Serial.print(bufName);
|
|
|
- Serial.print(" = ");
|
|
|
- Serial.println(bufValue);
|
|
|
+ //Serial.print("web update ");
|
|
|
+ //Serial.print(bufName);
|
|
|
+ //Serial.print(" = ");
|
|
|
+ //Serial.println(bufValue);
|
|
|
setConfig(bufName, bufValue);
|
|
|
}
|
|
|
saveConfigToFlash = true; // will be saved in next loop()
|
|
|
- Serial.println("web triggered saveConfigToFlash");
|
|
|
+ //Serial.println("web triggered saveConfigToFlash");
|
|
|
}
|
|
|
yield();
|
|
|
|
|
@@ -648,6 +630,8 @@ void httpServerInit() {
|
|
|
StaticJsonBuffer<1000> jsonBuffer;
|
|
|
JsonObject &json = jsonBuffer.createObject();
|
|
|
json["devName"] = deviceName;
|
|
|
+ json["hostName"] = hostName;
|
|
|
+ json["wifiappw"] = wifiAPModePassword;
|
|
|
json["httpUser"] = http_user;
|
|
|
json["httpPass"] = http_pass;
|
|
|
json["httpToken"] = http_token;
|
|
@@ -671,20 +655,12 @@ void httpServerInit() {
|
|
|
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
|
|
|
httpServer.send(200, "application/json", jsonchar);
|
|
|
}
|
|
|
- else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
- }
|
|
|
}); //httpServer.on /confdata
|
|
|
|
|
|
httpServer.on("/confdata2", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
- Serial.println("httpServer.on /confdata2");
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
+ else {
|
|
|
+ //Serial.println("httpServer.on /confdata2");
|
|
|
|
|
|
for (int i = 0; i < httpServer.args(); i++) {
|
|
|
char bufName[20];
|
|
@@ -693,14 +669,14 @@ void httpServerInit() {
|
|
|
httpServer.arg(i).toCharArray(bufValue, 101);
|
|
|
|
|
|
if (strlen(bufName) > 0) {
|
|
|
- Serial.print("web update ");
|
|
|
- Serial.print(bufName);
|
|
|
- Serial.print(" = ");
|
|
|
- Serial.println(bufValue);
|
|
|
+ //Serial.print("web update ");
|
|
|
+ //Serial.print(bufName);
|
|
|
+ //Serial.print(" = ");
|
|
|
+ //Serial.println(bufValue);
|
|
|
setConfig(bufName, bufValue);
|
|
|
}
|
|
|
saveConfig2ToFlash = true;
|
|
|
- Serial.println("web triggered saveConfig2ToFlash");
|
|
|
+ //Serial.println("web triggered saveConfig2ToFlash");
|
|
|
}
|
|
|
yield();
|
|
|
|
|
@@ -747,9 +723,6 @@ void httpServerInit() {
|
|
|
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
|
|
|
httpServer.send(200, "application/json", jsonchar);
|
|
|
}
|
|
|
- else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
- }
|
|
|
}); //httpServer.on /confdata2
|
|
|
|
|
|
|
|
@@ -757,13 +730,8 @@ void httpServerInit() {
|
|
|
|
|
|
//get heap status, analog input value and all GPIO statuses in one json call
|
|
|
httpServer.on("/info", HTTP_GET, []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
+ else {
|
|
|
String json = " {";
|
|
|
json += "\"wifissid\":\"" + WiFi.SSID() + "\"";
|
|
|
json += "\"heap\":" + String(ESP.getFreeHeap());
|
|
@@ -771,53 +739,37 @@ void httpServerInit() {
|
|
|
httpServer.send(200, "text/json", json);
|
|
|
json = String();
|
|
|
}
|
|
|
- else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
- }
|
|
|
}); //httpServer.on /info
|
|
|
|
|
|
httpServer.on("/", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
- httpServerHandleRoot();
|
|
|
+ if ( WifiInApMode ) {
|
|
|
+ httpServer.sendHeader("Location", "/wifi.htm", true);
|
|
|
+ httpServer.send(302);
|
|
|
}
|
|
|
else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
+ httpServer.sendHeader("Location", "/main", true);
|
|
|
+ httpServer.send(302);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
+ httpServer.on("/main", []() {
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
+ else {
|
|
|
+ httpServerHandleMainPage();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
httpServer.on("/conf", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
- httpServerHandleConfPage();
|
|
|
- }
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
+ httpServerHandleConfPage();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
httpServer.on("/conf2", []() {
|
|
|
- boolean isAuthenticated = false;
|
|
|
- if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
|
|
|
- if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
|
|
|
- isAuthenticated = true;
|
|
|
- }
|
|
|
- else isAuthenticated = true;
|
|
|
- if (isAuthenticated) {
|
|
|
- httpServerHandleConf2Page();
|
|
|
- }
|
|
|
+ if (!httpIsAuthenticated()) return httpServer.requestAuthentication();
|
|
|
else {
|
|
|
- httpServer.send (401, "text/plain", "UNAUTHORIZED");
|
|
|
+ httpServerHandleConf2Page();
|
|
|
}
|
|
|
});
|
|
|
|