Domotique/WifiControlSensor/WebServer.ino
2016-06-04 18:45:11 +02:00

192 lines
8.1 KiB
C++

#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
void WebHandleRoot() {
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
"<h1>You are connected to " + String(hostName) + "</h1>"
"<fieldset>"
"<legend>Sensors</legend>"
#ifdef CONFIG_ENABLE_BMP180
"Temperature " + String(temp, 2) + "C<br/>"
"Pressure " + String(pressure, 2) + "mB<br/>"
#endif
#ifdef CONFIG_ENABLE_DHT
"Temperature DHT " + String(dhtTemp, 0) + "C<br/>"
"Humidity DHT " + String(dhtHumidity, 0) + "%<br/>"
#endif
#ifdef CONFIG_ENABLE_DRY_SENSOR
"Dryness " + String((dryness*100)/1024) + "%<br/>"
#endif
"</fieldset>"
#ifdef CONFIG_WEB_CONTROLLED_GPIO
"<fieldset>"
"<legend>Relay</legend>"
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&amp;value=1\">Relay ON</a><br/>"
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&value=0\">Relay OFF</a><br/>"
"</fieldset>"
#endif
"<fieldset>"
"<legend>Settings</legend>"
"<a href=\"/setup\">Enter Setup</a><br/>"
"<a href=\"/otamode\">Put device in OTA mode</a><br/>"
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>"
#ifdef CONFIG_ENABLE_BMP180
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
#endif
"Free space: " + ESP.getFreeSketchSpace() + "<br/>"
"Free heap: " + ESP.getFreeHeap() + "<br/>"
"Build the " + __DATE__ + " at " + __TIME__ + "<br/>"
"</fieldset>"
);
}
void WebSendError(const char *error) {
server.send(500, "text/plain", error);
}
void WebHandleSetup() {
uint8_t mode;
char *confSsid = "";
char *confPassword = "";
char *confHost = "";
char *mqttServer = "";
char *mqttUser = "";
char *mqttPasswd = "";
int mqttPort = 1883;
int ip_config = 0;
uint32_t ip = 0;
uint32_t gw = 0;
uint32_t mask = 0;
uint32_t dns = 0;
uint32_t dns2 = 0;
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_config, ip, gw, mask, dns, dns2);
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
"<fieldset>"
"<legend>Wifi configuration:</legend>"
"<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>"
"</fieldset>"
"<fieldset>"
"<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>"
"<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>"
"</fieldset>"
"<fieldset>"
"<legend>MQTT:</legend>"
"<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>"
"</fieldset>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>"
"</form>");
}
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", "<h1>GPIO" + server.arg("gpio") + " changed to " + server.arg("value") + "</h1>");
}
boolean WebSetIp(IPAddress &addr, const char *id, const char *error) {
if (server.arg(id) != "" && !addr.fromString(server.arg(id).c_str())) {
WebSendError(error);
return false;
}
return true;
}
void WebHandleSave() {
IPAddress ip;
IPAddress gw;
IPAddress mask;
IPAddress dns;
IPAddress dns2;
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")) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
//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;
}
}
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(),
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");
return;
}
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
"<a href=\"/reboot\">Reboot</a><br/>");
}
void WebHandleOTA() {
SKETCH_DEBUG_PRINTLN("Boot mode Set to OTA");
EepromSaveBootMode(BOOTMODE_OTA);
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
"You can reboot now<br/>"
"<a href=\"/reboot\">Reboot</a><br/>");
}
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() {
SKETCH_DEBUG_PRINTLN("HTTP request to reboot");
server.send(200, "text/html", "<h1>Device Reboot</h1><br/>");
ESP.restart();
}
void WebSetupServer(int ) {
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();
SKETCH_DEBUG_PRINTLN("HTTP server started");
}