#define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) void WebHandleRoot() { server.send(200, "text/html", "" "

You are connected to " + String(hostName) + "

" "
" "Sensors" #ifdef CONFIG_ENABLE_BMP180 "Temperature " + String(temp, 2) + "C
" "Pressure " + String(pressure, 2) + "mB
" #endif #ifdef CONFIG_ENABLE_DHT "Temperature DHT " + String(dhtTemp, 0) + "C
" "Humidity DHT " + String(dhtHumidity, 0) + "%
" #endif #ifdef CONFIG_ENABLE_DRY_SENSOR "Dryness " + String((dryness*100)/1024) + "%
" #endif "
" #ifdef CONFIG_WEB_CONTROLLED_GPIO "
" "Relay" "Relay ON
" "Relay OFF
" "
" #endif "
" "Settings" "Enter Setup
" "Put device in OTA mode
" "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "
" #ifdef CONFIG_ENABLE_BMP180 "BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "
" #endif "Free space: " + ESP.getFreeSketchSpace() + "
" "Free heap: " + ESP.getFreeHeap() + "
" "Build the " + __DATE__ + " at " + __TIME__ + "
" "
" ); } 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", "
" "
" "Wifi configuration:" "

" "

" "

" "
" "
" "IP Configuration" "
DHCP Static
" "

" "

" "

" "

" "

" "
" "
" "MQTT:" "

" "

" "

" "

" "
" "
" "
"); } 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") + "

"); } 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(ip), static_cast(gw), static_cast(mask), static_cast(dns), static_cast(dns2)) < 0) { WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } server.send(200, "text/html", "

Configuration Saved


" "Reboot
"); } void WebHandleOTA() { SKETCH_DEBUG_PRINTLN("Boot mode Set to OTA"); EepromSaveBootMode(BOOTMODE_OTA); server.send(200, "text/html", "

OTA Mode set


" "You can reboot now
" "Reboot
"); } 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", "

Device Reboot


"); 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"); }