2016-03-30 00:44:37 +02:00
|
|
|
#define STRINGIFY(x) #x
|
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleRoot() {
|
2016-03-17 01:05:19 +01:00
|
|
|
server.send(200, "text/html",
|
2016-03-30 00:44:37 +02:00
|
|
|
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"<h1>You are connected to " + String(hostName) + "</h1>"
|
2016-04-06 15:51:27 +02:00
|
|
|
#ifdef CONFIG_ENABLE_BMP180
|
2016-03-26 01:26:36 +01:00
|
|
|
"<fieldset>"
|
|
|
|
"<legend>Sensors</legend>"
|
|
|
|
"Temperature " + String(temp, 2) + "C<br/>"
|
|
|
|
"Pressure " + String(pressure, 2) + "mB<br/>"
|
|
|
|
"</fieldset>"
|
2016-04-06 15:51:27 +02:00
|
|
|
#endif
|
2016-03-30 00:44:37 +02:00
|
|
|
#ifdef CONFIG_WEB_CONTROLLED_GPIO
|
2016-04-06 15:51:27 +02:00
|
|
|
"<fieldset>"
|
2016-03-30 00:44:37 +02:00
|
|
|
"<legend>Relay</legend>"
|
|
|
|
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&value=1\">Relay ON</a><br/>"
|
|
|
|
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&value=0\">Relay OFF</a><br/>"
|
2016-03-26 01:26:36 +01:00
|
|
|
"</fieldset>"
|
2016-03-30 00:44:37 +02:00
|
|
|
#endif
|
2016-03-26 01:26:36 +01:00
|
|
|
"<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/>"
|
2016-04-06 15:51:27 +02:00
|
|
|
#ifdef CONFIG_ENABLE_BMP180
|
2016-03-24 14:54:55 +01:00
|
|
|
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
|
2016-04-06 15:51:27 +02:00
|
|
|
#endif
|
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-04-07 02:02:51 +02:00
|
|
|
void WebSendError(String error) {
|
|
|
|
server.send(500, "text/plain", error);
|
|
|
|
}
|
|
|
|
|
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-04-06 19:34:29 +02:00
|
|
|
int ip_config = 0;
|
2016-04-07 02:02:51 +02:00
|
|
|
uint32_t ip;
|
|
|
|
uint32_t gw;
|
|
|
|
uint32_t mask;
|
|
|
|
uint32_t dns;
|
|
|
|
uint32_t dns2;
|
2016-03-14 17:36:56 +01:00
|
|
|
|
2016-04-07 02:02:51 +02:00
|
|
|
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, ip, gw, mask, dns, dns2);
|
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>"
|
2016-04-06 19:34:29 +02:00
|
|
|
"<legend>IP Configuration</legend>"
|
|
|
|
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>"
|
2016-04-07 12:05:37 +02:00
|
|
|
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (ip == 0 ? "192.168.0.123": IPAddress(ip).toString()) + "\" /> </div>"
|
|
|
|
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (gw == 0 ? "192.168.0.250": IPAddress(gw).toString()) + "\" /> </div>"
|
|
|
|
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (mask == 0 ? "255.255.255.0": IPAddress(mask).toString()) + "\" /> </div>"
|
|
|
|
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (dns == 0 ? "192.168.0.250": IPAddress(dns).toString()) + "\" /> </div>"
|
|
|
|
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (dns2 == 0 ? "": IPAddress(dns2).toString()) + "\" /> </div>"
|
2016-04-06 19:34:29 +02:00
|
|
|
"</fieldset>"
|
|
|
|
"<fieldset>"
|
2016-03-14 17:36:56 +01:00
|
|
|
"<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-04-07 02:02:51 +02:00
|
|
|
boolean WebSetIp(IPAddress &addr, char *id, String error) {
|
|
|
|
if (server.arg(id) != "" && !addr.fromString(server.arg(id).c_str())) {
|
|
|
|
WebSendError(error);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleSave() {
|
2016-04-07 02:02:51 +02:00
|
|
|
IPAddress ip;
|
|
|
|
IPAddress gw;
|
|
|
|
IPAddress mask;
|
|
|
|
IPAddress dns;
|
|
|
|
IPAddress dns2;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-04-06 19:34:29 +02:00
|
|
|
if (!server.hasArg("ssid") || !server.hasArg("password") || !server.hasArg("host")
|
|
|
|
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
|
|
|
|
|| !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip")
|
|
|
|
|| !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns")
|
|
|
|
|| !server.hasArg("dns2")) {
|
2016-03-11 00:43:24 +01:00
|
|
|
server.send(500, "text/plain", "Bad arguments\r\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 02:02:51 +02:00
|
|
|
|
|
|
|
//Check Ip configuration
|
|
|
|
if (server.arg("ip_config").toInt() == 1) {
|
|
|
|
if (!WebSetIp(ip, "ip", "Incorrect IP") || !WebSetIp(gw, "gw", "Incorrect Gateway") || !WebSetIp(mask, "mask", "Incorrect NetMask") ||
|
|
|
|
!WebSetIp(dns, "dns", "Incorrect DNS") || !WebSetIp(dns2, "dns2", "Incorrect DNS2")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:34:29 +02: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(),
|
2016-04-07 02:02:51 +02:00
|
|
|
server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw),
|
|
|
|
static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2)) < 0) {
|
|
|
|
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
|
2016-03-11 00:43:24 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|