endpoints.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //Handlers for Web Server
  2. void handle_index() {
  3. server.send(200, "text/html", "[OK] - hds 4-way relay module");
  4. }
  5. //Handle turning on the switch
  6. void handle_toggle() {
  7. String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL
  8. if (relayState.length() > 0) {
  9. int relayValue = relayState.toInt();
  10. // Perform actions based on the relay value (1 or 0)
  11. if (relayValue < 0 || relayValue > 4) {
  12. //Out of range
  13. server.send(400, "application/json", "{\"error\":\"relay index out of range\"}");
  14. return;
  15. } else {
  16. //In range
  17. relayStatus[relayValue] = !relayStatus[relayValue];
  18. Serial.println("Toggling relay: " + String(relayValue));
  19. updateRelayStatus();
  20. server.send(200, "application/json", "{\"status\":\"ok\"}");
  21. return;
  22. }
  23. } else {
  24. server.send(400, "application/json", "{\"error\":\"invalid relay index\"}");
  25. }
  26. }
  27. void handle_on() {
  28. String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL
  29. if (relayState.length() > 0) {
  30. int relayValue = relayState.toInt();
  31. // Perform actions based on the relay value (0 to 4)
  32. if (relayValue >= 0 && relayValue < 5) {
  33. // Turn on the specified relay
  34. relayStatus[relayValue] = true;
  35. Serial.println("Turning on relay: " + String(relayValue));
  36. updateRelayStatus();
  37. server.send(200, "application/json", "{\"status\":\"ok\"}");
  38. return;
  39. } else {
  40. // Out of range
  41. server.send(400, "application/json", "{\"error\":\"relay index out of range\"}");
  42. }
  43. } else {
  44. // Missing 'relay' parameter
  45. server.send(400, "application/json", "{\"error\":\"missing relay index\"}");
  46. }
  47. }
  48. void handle_off() {
  49. String relayState = server.arg("relay"); // Get the value of the 'relay' parameter from the URL
  50. if (relayState.length() > 0) {
  51. int relayValue = relayState.toInt();
  52. // Perform actions based on the relay value (0 to 4)
  53. if (relayValue >= 0 && relayValue < 5) {
  54. // Turn off the specified relay
  55. relayStatus[relayValue] = false;
  56. Serial.println("Turning off relay: " + String(relayValue));
  57. updateRelayStatus();
  58. server.send(200, "application/json", "{\"status\":\"ok\"}");
  59. return;
  60. } else {
  61. // Out of range
  62. server.send(400, "application/json", "{\"error\":\"relay index out of range\"}");
  63. }
  64. } else {
  65. // Missing 'relay' parameter
  66. server.send(400, "application/json", "{\"error\":\"missing relay index\"}");
  67. }
  68. }
  69. void handle_status() {
  70. DynamicJsonDocument doc(256);
  71. doc["relay1"] = relayStatus[0] ? true : false;
  72. doc["relay2"] = relayStatus[1] ? true : false;
  73. doc["relay3"] = relayStatus[2] ? true : false;
  74. doc["relay4"] = relayStatus[3] ? true : false;
  75. String json;
  76. serializeJson(doc, json);
  77. server.send(200, "application/json", json.c_str());
  78. }
  79. void handle_deviceInfo() {
  80. // Open the file in read mode
  81. File file = LittleFS.open("/config.json", "r");
  82. if (!file) {
  83. // If the file doesn't exist, send a 404 Not Found response
  84. server.send(404, "application/json", "{\"error\":\"device config missing from firmware\"}");
  85. return;
  86. }
  87. String fileContent = "";
  88. while (file.available()) {
  89. fileContent += (char)file.read();
  90. }
  91. file.close();
  92. server.send(200, "application/json", fileContent);
  93. }
  94. void handle_uuid() {
  95. server.send(200, "application/json", "\"" + deviceUUID + "\"");
  96. }