#ifndef CONFIG_DISABLE_WEB #include ESP8266HTTPUpdateServer httpUpdater; String gpioControlHTML = ""; String pwmControlHTML = ""; void WebBuildGpioObserved(String &html) { if (NB_ELEMENTS(gpioObserved) > 0) { html += "
" "Detector"; for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) { html += "Sensor " + String(gpioObserved[i]) + ": " + digitalRead(gpioObserved[i]) + "
"; } html += "
"; } } void WebHandleRoot() { String gpioObserved = ""; String optimiseConfig = ""; WebBuildGpioObserved(gpioObserved); if (WiFi.status() == WL_CONNECTED) { optimiseConfig = "Optimize Config
"; } server.send(200, "text/html", "" "

You are connected to " + String(conf.host) + "

" "
" "Sensors" #ifdef CONFIG_ENABLE_BMP180 "" + (BMP180IsConnected() ? "
BMP180
Temperature " + String(temp, 2) + "C
Pressure " + String(pressure, 2) + "hPa
" : "BMP180 Disconnected" ) + "" #endif #ifdef CONFIG_ENABLE_DHT "" + (DHTIsConnected() ? "
DHT
Temperature " + String(dhtTemp, 0) + "C
Humidity " + String(dhtHumidity, 0) + "%
" : "DHT Disconnected" ) + "" #endif #ifdef CONFIG_ENABLE_BME680 "" + (BME680IsConnected() ? "
BME680
Temperature " + String(bme680T, 2) + "C
Pressure " + String(bme680P, 2) + "hPa
Humidity " + String(bme680H, 2) + "%
Gaz " + String(bme680G, 2) + "kOhm
" : "BME680 Disconnected" ) + "" #endif #ifdef CONFIG_ENABLE_DRY_SENSOR "Dryness " + String((dryness * 100) / 1024) + "%
" #endif "
" + gpioControlHTML + gpioObserved + pwmControlHTML + "
" "Settings" "Enter Setup
" "Update firmware
" + optimiseConfig + "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "
" "Wifi Strength: " + WiFi.RSSI() + "dBm
" "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 WebBuildSSIDList(String &datalist) { int n = WiFi.scanNetworks(); datalist = ""; // sort by RSSI int indices[n]; for (int i = 0; i < n; i++) { indices[i] = i; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) { std::swap(indices[i], indices[j]); } } } for (int i = 0; i < n; ++i) { datalist += ""; } void WebHandleSetupPreConfig() { conf.bssid = strdup( WiFi.BSSIDstr().c_str()); conf.channel = WiFi.channel(); conf.ip_mode = 1; conf.ip = WiFi.localIP(); conf.mask = WiFi.subnetMask(); conf.gw = WiFi.gatewayIP(); conf.dns = WiFi.dnsIP(); conf.dns2 = WiFi.dnsIP(1); WebHandleSetup(); } void WebHandleSetup() { String ssidlist; WebBuildSSIDList(ssidlist); String dhcpChecked = conf.ip_mode == 0 ? "checked" : ""; String staticChecked = conf.ip_mode == 1 ? "checked" : ""; server.send(200, "text/html", "
" "
" "Wifi configuration:" "

" "" + ssidlist + "" "

" "

" "

" "

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

" "

" "

" "

" "

" "
" "
" "MQTT:" "

" "

" "

" "

(8883 for secure Mqtts)
" "
" "
" "
"); } 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 WebHandlePWM() { if (!server.hasArg("gpio") || !server.hasArg("value")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } MqttChangePWMValue(server.arg("gpio").toInt(), server.arg("value").toInt()); server.send(200, "text/html", "

PWM" + 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.hasArg("channel") || ! server.hasArg("bssid")) { 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")) { server.send(500, "text/plain", "Bad arguments\r\n"); return; } } productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").c_str()), strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()), strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(), server.arg("ip_config").toInt(), static_cast(ip), static_cast(gw), static_cast(mask), static_cast(dns), static_cast(dns2), static_cast(server.arg("channel").toInt()), strdup(server.arg("bssid").c_str()) }; if (EepromSaveConfig(newConf) < 0) { WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n"); return; } if (WiFi.softAPIP() != IPAddress((uint32_t)0)) { //In STA mode, we can test the AP connection WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str()); server.send(200, "text/html", "

Configuration Saved


" "Check Wifi Configuration
" "Reboot
"); } else { server.send(200, "text/html", "

Configuration Saved


" "Reboot
"); } } void WebHandleOTA() { SKETCH_DEBUG_PRINTLN("Activating OTA mode"); server.send(200, "text/html", "

Setting OTA mode


" "Web ui will be disabled
"); mode = BOOTMODE_OTA; OTASetup(); } 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(); } String statusToString(wl_status_t status) { switch (status) { case WL_IDLE_STATUS: return String("Idle"); case WL_NO_SSID_AVAIL: return String("Wifi not found"); case WL_CONNECTED: return String("Connected"); case WL_CONNECT_FAILED: return String("Connection failed (Wrong password ?)"); case WL_CONNECTION_LOST: return String("Connection Lost"); case WL_DISCONNECTED: return String("Connecting"); default: return String(status); } } void WebHandleWifiStatus() { String message; if (WiFi.status() == WL_DISCONNECTED) message += ""; message += "

Wifi Connection Status


"; message += "Connection to "; message += WiFi.SSID(); message += ":
"; message += statusToString(WiFi.status()); message += "
"; if (mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED) { message += "Wifi correctly setup! You can reboot now
"; message += "Reboot
"; } if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL) { message += "Try to reconfigure you WiFi details
"; message += "Enter Setup
"; } server.send(200, "text/html", message); } void WebBuildGpioControl() { if (NB_ELEMENTS(gpioControlled) > 0) { gpioControlHTML += "
" "Relay"; for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) { gpioControlHTML += "Relay " + String(gpioControlled[i]) + " " + "ON/"; gpioControlHTML += "OFF
"; } gpioControlHTML += "
"; } } void WebBuildPwmControl() { if (NB_ELEMENTS(pwmControlled) > 0) { pwmControlHTML += "
" "PWM"; for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) { pwmControlHTML += "PWM " + String(pwmControlled[i]) + "
"; pwmControlHTML += ""; } pwmControlHTML += ""; pwmControlHTML += "
"; } } void WebSetupServer(int ) { WebBuildGpioControl(); WebBuildPwmControl(); server.on("/", WebHandleRoot); server.on("/setupPreConfig", WebHandleSetupPreConfig); server.on("/setup", WebHandleSetup); server.on("/save", WebHandleSave); server.on("/gpio", WebHandleGpio); server.on("/pwm", WebHandlePWM); server.on("/otamode", WebHandleOTA); server.on("/reboot", WebHandleReboot); server.on("/wifiStatus", WebHandleWifiStatus); server.onNotFound(WebHandleNotFound); httpUpdater.setup(&server, "/upload"); server.begin(); SKETCH_DEBUG_PRINTLN("HTTP server started"); } #else void WebHandleRoot() {} void WebHandleSetupPreConfig() {} void WebHandleSetup() {} void WebHandleGpio() {} void WebHandleSave() {} void WebHandleOTA() {} void WebHandleNotFound() {} void WebSetupServer(int bootmode) {} #endif