//extern ESP8266WebServer httpServer;
static const char httpRoot[] PROGMEM =
R"(
WiFi Switch
WiFi verbunden mit .
Letztes Update vor
Sekunden.
WiFi-Einstellungen
Einstellungen
Firmware Update
)";
static const char httpConfPage[] PROGMEM =
R"(
WiFi Thermostat
Configuration
Home
)";
void httpServerHandleRoot() {
httpServer.send_P(200, "text/html", httpRoot);
}
void httpServerHandleConfPage() {
httpServer.send_P(200, "text/html", httpConfPage);
}
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 httpServerInit() {
//handles commands from webpage, sends live data in JSON format
httpServer.on("/api", []() {
//Serial.print("httpServer.on /api");
if (httpServer.hasArg("Btn1")) {
lastSwitchSource[0] = 2;
relaisToggle(0);
Serial.println("web Btn1");
} //if
if (httpServer.hasArg("Btn2")) {
lastSwitchSource[1] = 2;
relaisToggle(1);
Serial.println("web Btn2");
} //if
if (httpServer.hasArg("Btn3")) {
lastSwitchSource[2] = 2;
relaisToggle(2);
Serial.println("web Btn3");
} //if
//build json object of program data
StaticJsonBuffer<200> jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["ssid"] = WiFi.SSID();
json["swState1"] = relais_state[0];
if(RELAIS_COUNT > 1) json["swState2"] = relais_state[1];
if(RELAIS_COUNT > 2) json["swState3"] = relais_state[2];
json["devname"] = DEVICE_NAME;
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
//get heap status, analog input value and all GPIO statuses in one json call
httpServer.on("/info", HTTP_GET, []() {
String json = "{";
json += "\"wifissid\":\"" + WiFi.SSID() + "\"";
json += "\"heap\":" + String(ESP.getFreeHeap());
//json += ", \"analog\":" + String(analogRead(A0));
//json += ", \"gpio\":" + String((uint32_t)(((GPI | GPO) & 0xFFFF) | ((GP16I & 0x01) << 16)));
json += "}";
httpServer.send(200, "text/json", json);
json = String();
}); //httpServer.on /info
httpServer.on("/", []() {
httpServerHandleRoot();
});
httpServer.on("/config", []() {
httpServerHandleConfPage();
});
httpServer.onNotFound([]() {
httpServerHandleNotFound();
}); //httpServer.onNotFound
// HTTP Updater at /update
httpUpdater.setup(&httpServer);
httpServer.begin();
}