//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"(
Configuration
)";
static const char httpConf2Page[] PROGMEM =
R"(
Configuration 2
)";
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 httpServerInit() {
//handles commands from webpage, sends live data in JSON format
httpServer.on("/api", []() {
//Serial.println("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"] = deviceName;
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("/confdata", []() {
Serial.println("httpServer.on /confdata");
// if (httpServer.hasArg("saveToFlash")) {
// if (httpServer.arg("saveToFlash").toInt() == 1) {
// Serial.println("web saveToFlash triggered");
// //saveConfig();
// saveConfigToFlash = true;
// }
// }
// else if (httpServer.hasArg("restart")) {
// if (httpServer.arg("restart").toInt() == 1) {
// Serial.println("web restart triggered");
// ESP.restart();
// }
// }
// else {
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(strcmp(bufName, "willRet") == 0) {
// willRetSet = true;
// }
//if (strlen(bufName) > 0 && strlen(bufValue) > 0) {
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");
}
// }
//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["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["willTop"] = mqtt_willTopic;
json["willQos"] = mqtt_willQos;
json["willRet"] = mqtt_willRetain;
json["willMsg"] = mqtt_willMsg;
json["domoOutTop"] = domoticz_out_topic;
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", []() {
Serial.println("httpServer.on /confdata2");
// if (httpServer.hasArg("saveToFlash")) {
// if (httpServer.arg("saveToFlash").toInt() == 1) {
// Serial.println("web saveToFlash triggered");
// //saveConfig();
// saveConfigToFlash = true;
// }
// }
// else if (httpServer.hasArg("restart")) {
// if (httpServer.arg("restart").toInt() == 1) {
// Serial.println("web restart triggered");
// ESP.restart();
// }
// }
// else {
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(strcmp(bufName, "willRet") == 0) {
// willRetSet = true;
// }
//if (strlen(bufName) > 0 && strlen(bufValue) > 0) {
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");
}
// }
//build json object of program data
StaticJsonBuffer<1000> jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["usedRelais"] = usedRelaisCount;
json["usedButtons"] = usedButtonsCount;
json["outTop_hold1"] = mqtt_topic_out_hold_1;
json["outTop_hold2"] = mqtt_topic_out_hold_2;
json["outTop_hold3"] = mqtt_topic_out_hold_3;
json["outPld_hold1"] = mqtt_payload_out_hold_1;
json["outPld_hold2"] = mqtt_payload_out_hold_2;
json["outPld_hold3"] = mqtt_payload_out_hold_3;
json["domoIdx1"] = domoticzIdx[0];
json["domoIdx2"] = domoticzIdx[1];
json["domoIdx3"] = domoticzIdx[2];
json["impuls1"] = relais_impulse[0];
json["impuls2"] = relais_impulse[1];
json["impuls3"] = relais_impulse[2];
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, []() {
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("/conf", []() {
httpServerHandleConfPage();
});
httpServer.on("/conf2", []() {
httpServerHandleConf2Page();
});
httpServer.onNotFound([]() {
httpServerHandleNotFound();
}); //httpServer.onNotFound
// HTTP Updater at /update
httpUpdater.setup(&httpServer);
httpServer.begin();
}