//Handlers for Web Server void handle_index() { server.send(200, "text/html", "[OK] - hds 4-way relay module"); } //Handle turning on the switch void handle_toggle() { String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL if (relayState.length() > 0) { int relayValue = relayState.toInt(); // Perform actions based on the relay value (1 or 0) if (relayValue < 0 || relayValue > 4) { //Out of range server.send(400, "application/json", "{\"error\":\"relay index out of range\"}"); return; } else { //In range relayStatus[relayValue] = !relayStatus[relayValue]; Serial.println("Toggling relay: " + String(relayValue)); updateRelayStatus(); server.send(200, "application/json", "{\"status\":\"ok\"}"); return; } } else { server.send(400, "application/json", "{\"error\":\"invalid relay index\"}"); } } void handle_on() { String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL if (relayState.length() > 0) { int relayValue = relayState.toInt(); // Perform actions based on the relay value (0 to 4) if (relayValue >= 0 && relayValue < 5) { // Turn on the specified relay relayStatus[relayValue] = true; Serial.println("Turning on relay: " + String(relayValue)); updateRelayStatus(); server.send(200, "application/json", "{\"status\":\"ok\"}"); return; } else { // Out of range server.send(400, "application/json", "{\"error\":\"relay index out of range\"}"); } } else { // Missing 'relay' parameter server.send(400, "application/json", "{\"error\":\"missing relay index\"}"); } } void handle_off() { String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL if (relayState.length() > 0) { int relayValue = relayState.toInt(); // Perform actions based on the relay value (0 to 4) if (relayValue >= 0 && relayValue < 5) { // Turn off the specified relay relayStatus[relayValue] = false; Serial.println("Turning off relay: " + String(relayValue)); updateRelayStatus(); server.send(200, "application/json", "{\"status\":\"ok\"}"); return; } else { // Out of range server.send(400, "application/json", "{\"error\":\"relay index out of range\"}"); } } else { // Missing 'relay' parameter server.send(400, "application/json", "{\"error\":\"missing relay index\"}"); } } void handle_status() { DynamicJsonDocument doc(256); doc["relay1"] = relayStatus[0] ? true : false; doc["relay2"] = relayStatus[1] ? true : false; doc["relay3"] = relayStatus[2] ? true : false; doc["relay4"] = relayStatus[3] ? true : false; String json; serializeJson(doc, json); server.send(200, "application/json", json.c_str()); } void handle_deviceInfo() { // Open the file in read mode File file = LittleFS.open("/config.json", "r"); if (!file) { // If the file doesn't exist, send a 404 Not Found response server.send(404, "application/json", "{\"error\":\"device config missing from firmware\"}"); return; } String fileContent = ""; while (file.available()) { fileContent += (char)file.read(); } file.close(); server.send(200, "application/json", fileContent); } void handle_uuid() { server.send(200, "application/json", "\"" + deviceUUID + "\""); }