void WebHandleRoot() {
server.send(200, "text/html",
"
"
"You are connected
"
"Current temperature " + String(temp, 2) + "C
"
"Current pressure " + String(pressure, 2) + "mB
"
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "
"
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "
"
"Free space: "+ ESP.getFreeSketchSpace() +"
"
"Free heap: "+ ESP.getFreeHeap() +"
"
"Setup
"
"OTA mode
"
"ON
"
"OFF
"
);
}
void WebHandleSetup() {
uint8_t mode;
char *confSsid = "";
char *confPassword = "";
char *confHost = "";
char *mqttServer = "";
char *mqttUser = "";
char *mqttPasswd = "";
int mqttPort = 1883;
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
server.send(200, "text/html", "");
}
void WebHandleGpio() {
if (!server.hasArg("gpio") || !server.hasArg("value")) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
MqttChangeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt());
server.send(200, "text/html", "GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "
");
}
void WebHandleSave() {
String password;
String ssid;
String hostName;
String mqttServer;
String mqttUser;
String mqttPasswd;
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") ) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
if (EepromSaveConfig(BOOTMODE_NORMAL, server.arg("ssid"), server.arg("password"), server.arg("host"), server.arg("mqttServer"), server.arg("mqttUser"), server.arg("mqttPasswd"), server.arg("mqttPort").toInt()) < 0) {
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return;
}
server.send(200, "text/html", "Configuration Saved
"
"Reboot
");
}
void WebHandleOTA() {
Serial.println("Boot mode Set to OTA");
EepromSaveBootMode(BOOTMODE_OTA);
server.send(200, "text/html", "OTA Mode set
"
"You can reboot now");
}
void WebHandleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void WebHandleReboot() {
Serial.println("HTTP request to reboot");
server.send(200, "text/html", "Device Reboot
");
ESP.restart();
}
void WebSetupServer(int bootmode) {
server.on("/", WebHandleRoot);
server.on("/setup", WebHandleSetup);
server.on("/save", WebHandleSave);
server.on("/gpio", WebHandleGpio);
server.on("/otamode", WebHandleOTA);
server.on("/reboot", WebHandleReboot);
server.onNotFound(WebHandleNotFound);
server.begin();
Serial.println("HTTP server started");
}