2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleRoot() {
|
2016-03-17 01:05:19 +01:00
|
|
|
server.send(200, "text/html",
|
|
|
|
"<head><meta http-equiv=\"refresh\" content=\"" + String(SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"<h1>You are connected to " + String(hostName) + "</h1>"
|
|
|
|
"<fieldset>"
|
|
|
|
"<legend>Sensors</legend>"
|
|
|
|
"Temperature " + String(temp, 2) + "C<br/>"
|
|
|
|
"Pressure " + String(pressure, 2) + "mB<br/>"
|
|
|
|
"</fieldset>"
|
|
|
|
"<fieldset>"
|
|
|
|
"<legend>Pump</legend>"
|
2016-03-26 15:07:15 +01:00
|
|
|
"<a href=\"/gpio?gpio=2&value=1\">Power PUMP ON</a><br/>"
|
|
|
|
"<a href=\"/gpio?gpio=2&value=0\">Power PUMP OFF</a><br/>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"</fieldset>"
|
|
|
|
"<fieldset>"
|
|
|
|
"<legend>Settings</legend>"
|
|
|
|
"<a href=\"/setup\">Enter Setup</a><br/>"
|
|
|
|
"<a href=\"/otamode\">Put device in OTA mode</a><br/>"
|
2016-03-24 14:54:55 +01:00
|
|
|
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>"
|
|
|
|
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"Free space: " + ESP.getFreeSketchSpace() + "<br/>"
|
|
|
|
"Free heap: " + ESP.getFreeHeap() + "<br/>"
|
|
|
|
"</fieldset>"
|
2016-03-11 00:43:24 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleSetup() {
|
2016-03-14 17:36:56 +01:00
|
|
|
uint8_t mode;
|
|
|
|
char *confSsid = "";
|
|
|
|
char *confPassword = "";
|
|
|
|
char *confHost = "";
|
|
|
|
char *mqttServer = "";
|
|
|
|
char *mqttUser = "";
|
|
|
|
char *mqttPasswd = "";
|
|
|
|
int mqttPort = 1883;
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort);
|
2016-03-14 17:36:56 +01:00
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
|
2016-03-14 17:36:56 +01:00
|
|
|
"<fieldset>"
|
|
|
|
"<legend>Wifi configuration:</legend>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"<div><label for=\"ssid\">Wifi SSID :</label> <br/><input type=\"text\" name=\"ssid\" value=\"" + String(confSsid) + "\" /></div>"
|
|
|
|
"<div><label for=\"password\">Wifi Password :</label><br/><input type=\"password\" name=\"password\" /> </div>"
|
|
|
|
"<div><label for=\"host\">Hostname :</label><br/><input type=\"text\" name=\"host\" value=\"" + String(confHost) + "\" /> </div>"
|
2016-03-14 17:36:56 +01:00
|
|
|
"</fieldset>"
|
|
|
|
"<fieldset>"
|
|
|
|
"<legend>MQTT:</legend>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(mqttServer) + "\" /> </div>"
|
|
|
|
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(mqttUser) + "\" /> </div>"
|
|
|
|
"<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" /> </div>"
|
|
|
|
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(mqttPort) + "\" /> </div>"
|
2016-03-14 17:36:56 +01:00
|
|
|
"</fieldset>"
|
2016-03-11 00:43:24 +01:00
|
|
|
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
|
|
|
|
"</form>");
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleGpio() {
|
2016-03-11 00:43:24 +01:00
|
|
|
if (!server.hasArg("gpio") || !server.hasArg("value")) {
|
|
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
MqttChangeGpioValue(server.arg("gpio").toInt(), server.arg("value").toInt());
|
2016-03-11 00:43:24 +01:00
|
|
|
server.send(200, "text/html", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleSave() {
|
2016-03-11 00:43:24 +01:00
|
|
|
String password;
|
|
|
|
String ssid;
|
|
|
|
String hostName;
|
2016-03-14 01:47:43 +01:00
|
|
|
String mqttServer;
|
|
|
|
String mqttUser;
|
|
|
|
String mqttPasswd;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-14 17:18:36 +01:00
|
|
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttPort") ) {
|
2016-03-11 00:43:24 +01:00
|
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
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) {
|
2016-03-11 00:43:24 +01:00
|
|
|
server.send(500, "text/plain", "Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
|
2016-03-17 01:05:19 +01:00
|
|
|
"<a href=\"/reboot\">Reboot</a><br/>");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleOTA() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Boot mode Set to OTA");
|
2016-03-24 14:54:55 +01:00
|
|
|
EepromSaveBootMode(BOOTMODE_OTA);
|
2016-03-11 01:41:21 +01:00
|
|
|
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"You can reboot now"
|
|
|
|
"<a href=\"/reboot\">Reboot</a><br/>");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleNotFound() {
|
2016-03-11 00:43:24 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleReboot() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("HTTP request to reboot");
|
2016-03-16 01:09:25 +01:00
|
|
|
server.send(200, "text/html", "<h1>Device Reboot</h1><br/>");
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
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);
|
2016-03-11 01:41:21 +01:00
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
server.onNotFound(WebHandleNotFound);
|
2016-03-11 00:43:24 +01:00
|
|
|
server.begin();
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("HTTP server started");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|