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