webserver.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Webserver.ino
  3. This script handle web serving and RESTFUL API features
  4. */
  5. void registerServeEndpoints() {
  6. server.serveStatic("/", LittleFS, "/index.html");
  7. server.serveStatic("/wol", LittleFS, "/wol.html");
  8. /* Button Actions */
  9. server.on("/power/press", handlePowerBtnPress);
  10. server.on("/power/release", handlePowerBtnRelease);
  11. server.on("/reset/press", handleResetBtnPress);
  12. server.on("/reset/release", handleResetBtnRelease);
  13. server.on("/status", handleStatus);
  14. server.on("/ipaddr", handleGetIPAddr);
  15. /* RESTFUL API */
  16. server.on("/api/status", handleStatus);
  17. server.on("/api/power", handlePowerSequence);
  18. server.on("/api/reset", handleResetSequence);
  19. server.on("/api/wol", handleSendWoLPacket);
  20. server.on("/api/mem/write", HTTP_POST, handleSaveMac);
  21. server.on("/api/mem/read", HTTP_GET, handleGetMac);
  22. server.onNotFound(handleNotFound);
  23. }
  24. //Reply ok to request
  25. void sendOK() {
  26. server.send(200, "application/json", "\"ok\"");
  27. }
  28. /* Send Wake-On-Lan packet to another MAC address */
  29. void handleSendWoLPacket() {
  30. // Check if there is a "mac" parameter in the URL
  31. if (server.hasArg("mac")) {
  32. // Get the value of the "mac" parameter
  33. String macAddress = server.arg("mac");
  34. Serial.print("MAC Address: ");
  35. Serial.println(macAddress);
  36. //Send MAC address to both port 9 and 7
  37. WOL.sendMagicPacket(macAddress);
  38. WOL.sendMagicPacket(macAddress, 7);
  39. server.send(200, "text/plain", "Received MAC Address: " + macAddress);
  40. } else {
  41. server.send(400, "text/plain", "Missing 'mac' parameter");
  42. }
  43. }
  44. /* Emulate the power button press events in hardware */
  45. void handlePowerBtnPress() {
  46. digitalWrite(PWR_BTN, HIGH);
  47. digitalWrite(BTN_LED, LOW);
  48. Serial.println("[INFO] Power button pressed");
  49. sendOK();
  50. }
  51. void handlePowerBtnRelease() {
  52. digitalWrite(PWR_BTN, LOW);
  53. digitalWrite(BTN_LED, HIGH);
  54. Serial.println("[INFO] Power button released");
  55. sendOK();
  56. }
  57. /* Emulate the reset button press events in hardware */
  58. void handleResetBtnPress() {
  59. digitalWrite(RST_BTN, HIGH);
  60. digitalWrite(BTN_LED, LOW);
  61. Serial.println("[INFO] Reset button pressed");
  62. sendOK();
  63. }
  64. void handleResetBtnRelease() {
  65. digitalWrite(RST_BTN, LOW);
  66. digitalWrite(BTN_LED, HIGH);
  67. Serial.println("[INFO] Reset button released");
  68. sendOK();
  69. }
  70. //Serving the LED status
  71. void handleStatus() {
  72. //Create a JSON object, 64 should be more than enough bytes to work with
  73. StaticJsonDocument<64> jsonDoc;
  74. jsonDoc["hdd"] = hddLedState;
  75. jsonDoc["pwr"] = pwrLedState;
  76. String jsonString;
  77. serializeJson(jsonDoc, jsonString);
  78. server.send(200, "application/json", jsonString);
  79. }
  80. //Return the ip address and mDNS name of the device
  81. void handleGetIPAddr() {
  82. String ipAddrStr = WiFi.localIP().toString();
  83. StaticJsonDocument<128> jsonDoc;
  84. jsonDoc["ipaddr"] = ipAddrStr;
  85. jsonDoc["mdns"] = MDNS_NAME + ".local";
  86. String jsonString;
  87. serializeJson(jsonDoc, jsonString);
  88. server.send(200, "application/json", jsonString);
  89. }
  90. //Handle power up sequence
  91. void handlePowerSequence() {
  92. if (server.method() == HTTP_POST) {
  93. Serial.println("[INFO] Toggling power button via RESTFUL API");
  94. pressPowerButton();
  95. sendOK();
  96. } else {
  97. server.send(405, "text/plain", "Method Not Allowed");
  98. }
  99. }
  100. //Handle reset sequence
  101. void handleResetSequence() {
  102. if (server.method() == HTTP_POST) {
  103. Serial.println("[INFO] Toggling reset button via RESTFUL API");
  104. pressResetButton();
  105. sendOK();
  106. } else {
  107. server.send(405, "text/plain", "Method Not Allowed");
  108. }
  109. }
  110. // Default not found handler
  111. void handleNotFound() {
  112. String message = "404 - Not Found\n\n";
  113. message += "URI: ";
  114. message += server.uri();
  115. message += "\nMethod: ";
  116. message += (server.method() == HTTP_GET) ? "GET" : "POST";
  117. message += "\nArguments: ";
  118. message += server.args();
  119. message += "\n";
  120. for (uint8_t i = 0; i < server.args(); i++) {
  121. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  122. }
  123. server.send(404, "text/plain", message);
  124. }