//extern ESP8266WebServer httpServer;
static const char httpRoot[] PROGMEM =
R"(
WiFi Thermostat
℃
Current: ℃ %
Heating
WiFi connected to .
Last update
seconds ago.
WiFi settings
Base configuration
Extended configuration
Firmware update
Restart
)";
static const char httpConfPage[] PROGMEM =
R"(
Base configuration
Home
)";
static const char httpConf2Page[] PROGMEM =
R"(
Extended configuration
Home
)";
void httpServerHandleRoot() {
httpServer.send_P(200, "text/html", httpRoot);
}
void httpServerHandleConfPage() {
httpServer.send_P(200, "text/html", httpConfPage);
}
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");
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");
// }
});
httpServer.on("/api", []() {
DEBUG_PRINT("httpServer.on /api");
if (httpServer.hasArg("plusBtn")) {
setTempStepUp();
DEBUG_PRINT(P("web plusBtn"));
} //if
if (httpServer.hasArg("minusBtn")) {
setTempStepDown();
DEBUG_PRINT(P("web minusBtn"));
} //if
if (httpServer.hasArg("modeBtn")) {
toggleHeatingMode();
DEBUG_PRINT(P("web modeBtn"));
} //if
if (httpServer.hasArg("onoffBtn")) {
toggleThermostatOnOff();
DEBUG_PRINT(P("web onoffBtn"));
} //if
//build json object of program data
StaticJsonBuffer<200> jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["devname"] = deviceName;
json["ssid"] = WiFi.SSID();
json["setTemp"] = setTemp;
json["temp"] = currTemp;
json["hum"] = int(currHum);
json["mode"] = heatingMode;
json["heating"] = turnHeatingOn;
char jsonchar[200];
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
httpServer.send(200, "application/json", jsonchar);
}); //httpServer.on /api
httpServer.on("/restart", []() {
Serial.println("web triggered restart");
ESP.restart();
});
httpServer.on("/mqttReconnect", []() {
Serial.println("web triggered mqttReconnect");
mqttReconnect();
httpServer.sendHeader("Location", String("/"), true);
httpServer.send (302, "text/plain", "OK");
});
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");
}
}
} //if
else {
httpServer.send (403, "text/plain", "FORBIDDEN");
}
});
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 <= 2) setHeatingmodeTo(valueInt);
httpServer.send (200, "text/plain", "OK");
}
}
} //if
else {
httpServer.send (403, "text/plain", "FORBIDDEN");
}
});
httpServer.on("/confdata", []() {
boolean sendData = false;
if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
sendData = true;
}
else sendData = true;
if (sendData) {
Serial.println("httpServer.on /confdata");
for (int i = 0; i < httpServer.args(); i++) {
char bufName[20];
char bufValue[101];
httpServer.argName(i).toCharArray(bufName, 20);
httpServer.arg(i).toCharArray(bufValue, 101);
if (strlen(bufName) > 0) {
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");
}
yield();
//build json object of program data
StaticJsonBuffer<1000> jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["devName"] = deviceName;
json["httpUser"] = http_user;
json["httpPass"] = http_pass;
json["httpToken"] = http_token;
json["mqttHost"] = mqtt_server;
json["mqttPort"] = mqtt_port;
json["mqttUser"] = mqtt_user;
json["mqttPass"] = mqtt_pass;
json["inTop"] = mqtt_topic_in;
json["outTop"] = mqtt_topic_out;
json["outRet"] = mqtt_outRetain;
json["willTop"] = mqtt_willTopic;
json["willQos"] = mqtt_willQos;
json["willRet"] = mqtt_willRetain;
json["willMsg"] = mqtt_willMsg;
json["domOutTop"] = domoticz_out_topic;
yield();
char jsonchar[1000];
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
httpServer.send(200, "application/json", jsonchar);
}
}); //httpServer.on /confdata
httpServer.on("/confdata2", []() {
boolean sendData = false;
if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
sendData = true;
}
else sendData = true;
if (sendData) {
Serial.println("httpServer.on /confdata2");
for (int i = 0; i < httpServer.args(); i++) {
char bufName[20];
char bufValue[101];
httpServer.argName(i).toCharArray(bufName, 20);
httpServer.arg(i).toCharArray(bufValue, 101);
if (strlen(bufName) > 0) {
Serial.print("web update ");
Serial.print(bufName);
Serial.print(" = ");
Serial.println(bufValue);
setConfig(bufName, bufValue);
}
saveConfig2ToFlash = true;
Serial.println("web triggered saveConfig2ToFlash");
}
yield();
//build json object of program data
StaticJsonBuffer<1000> jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["domIdxTherm"] = domoticzIdx_Thermostat;
json["domIdxMode"] = domoticzIdx_ThermostatMode;
json["domIdxTempHum"] = domoticzIdx_TempHumSensor;
json["domIdxHeating"] = domoticzIdx_Heating;
json["domIdxPIR"] = domoticzIdx_PIR;
json["outTempTop"] = outTemp_topic_in;
json["outHumTop"] = outHum_topic_in;
json["autoSaveTemp"] = autoSaveSetTemp;
json["autoSaveMode"] = autoSaveHeatingMode;
json["minOffTime"] = heatingMinOffTime;
json["tempMin"] = setTempMin;
json["tempMax"] = setTempMax;
json["tempLow"] = setTempLow;
json["tempDec"] = setTempDecreaseVal;
json["hyst"] = hysteresis;
json["tempCorr"] = tempCorrVal;
json["humCorr"] = humCorrVal;
json["measInt"] = measureInterval;
json["dispInt"] = displayInterval;
json["dispTout"] = displayTimeout;
json["PIRenDisp"] = PIR_enablesDisplay;
yield();
char jsonchar[1000];
json.printTo(jsonchar); //print to char array, takes more memory but sends in one piece
httpServer.send(200, "application/json", jsonchar);
}
}); //httpServer.on /confdata2
//get heap status, analog input value and all GPIO statuses in one json call
httpServer.on("/info", HTTP_GET, []() {
boolean sendData = false;
if (strlen(http_user) > 0 && strlen(http_pass) > 0) {
if (!httpServer.authenticate(http_user, http_pass)) return httpServer.requestAuthentication();
sendData = true;
}
else sendData = true;
if (sendData) {
String json = " {";
json += "\"wifissid\":\"" + WiFi.SSID() + "\"";
json += "\"heap\":" + String(ESP.getFreeHeap());
json += "}";
httpServer.send(200, "text/json", json);
json = String();
}
}); //httpServer.on /info
httpServer.on("/", []() {
httpServerHandleRoot();
});
httpServer.on("/conf", []() {
httpServerHandleConfPage();
});
httpServer.on("/conf2", []() {
httpServerHandleConf2Page();
});
httpServer.onNotFound([]() {
httpServerHandleNotFound();
}); //httpServer.onNotFound
// HTTP Updater at /update
httpUpdater.setup(&httpServer);
httpServer.begin();
}