123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /*
- Webserver.ino
- This script handle web serving and RESTFUL API features
- */
- void registerServeEndpoints() {
- server.serveStatic("/", LittleFS, "/index.html");
- server.serveStatic("/wol", LittleFS, "/wol.html");
- /* Button Actions */
- server.on("/power/press", handlePowerBtnPress);
- server.on("/power/release", handlePowerBtnRelease);
- server.on("/reset/press", handleResetBtnPress);
- server.on("/reset/release", handleResetBtnRelease);
- server.on("/status", handleStatus);
- server.on("/ipaddr", handleGetIPAddr);
- /* RESTFUL API */
- server.on("/api/status", handleStatus);
- server.on("/api/power", handlePowerSequence);
- server.on("/api/reset", handleResetSequence);
- server.on("/api/wol", handleSendWoLPacket);
- server.on("/api/mem/write", HTTP_POST, handleSaveMac);
- server.on("/api/mem/read", HTTP_GET, handleGetMac);
- server.onNotFound(handleNotFound);
- }
- //Reply ok to request
- void sendOK() {
- server.send(200, "application/json", "\"ok\"");
- }
- /* Send Wake-On-Lan packet to another MAC address */
- void handleSendWoLPacket() {
- // Check if there is a "mac" parameter in the URL
- if (server.hasArg("mac")) {
- // Get the value of the "mac" parameter
- String macAddress = server.arg("mac");
- Serial.print("MAC Address: ");
- Serial.println(macAddress);
- //Send MAC address to both port 9 and 7
- WOL.sendMagicPacket(macAddress);
- WOL.sendMagicPacket(macAddress, 7);
-
- server.send(200, "text/plain", "Received MAC Address: " + macAddress);
- } else {
- server.send(400, "text/plain", "Missing 'mac' parameter");
- }
- }
- /* Emulate the power button press events in hardware */
- void handlePowerBtnPress() {
- digitalWrite(PWR_BTN, HIGH);
- digitalWrite(BTN_LED, LOW);
- Serial.println("[INFO] Power button pressed");
- sendOK();
- }
- void handlePowerBtnRelease() {
- digitalWrite(PWR_BTN, LOW);
- digitalWrite(BTN_LED, HIGH);
- Serial.println("[INFO] Power button released");
- sendOK();
- }
- /* Emulate the reset button press events in hardware */
- void handleResetBtnPress() {
- digitalWrite(RST_BTN, HIGH);
- digitalWrite(BTN_LED, LOW);
- Serial.println("[INFO] Reset button pressed");
- sendOK();
- }
- void handleResetBtnRelease() {
- digitalWrite(RST_BTN, LOW);
- digitalWrite(BTN_LED, HIGH);
- Serial.println("[INFO] Reset button released");
- sendOK();
- }
- //Serving the LED status
- void handleStatus() {
- //Create a JSON object, 64 should be more than enough bytes to work with
- StaticJsonDocument<64> jsonDoc;
- jsonDoc["hdd"] = hddLedState;
- jsonDoc["pwr"] = pwrLedState;
- String jsonString;
- serializeJson(jsonDoc, jsonString);
- server.send(200, "application/json", jsonString);
- }
- //Return the ip address and mDNS name of the device
- void handleGetIPAddr() {
- String ipAddrStr = WiFi.localIP().toString();
- StaticJsonDocument<128> jsonDoc;
- jsonDoc["ipaddr"] = ipAddrStr;
- jsonDoc["mdns"] = MDNS_NAME + ".local";
- String jsonString;
- serializeJson(jsonDoc, jsonString);
- server.send(200, "application/json", jsonString);
- }
- //Handle power up sequence
- void handlePowerSequence() {
- if (server.method() == HTTP_POST) {
- Serial.println("[INFO] Toggling power button via RESTFUL API");
- pressPowerButton();
- sendOK();
- } else {
- server.send(405, "text/plain", "Method Not Allowed");
- }
- }
- //Handle reset sequence
- void handleResetSequence() {
- if (server.method() == HTTP_POST) {
- Serial.println("[INFO] Toggling reset button via RESTFUL API");
- pressResetButton();
- sendOK();
- } else {
- server.send(405, "text/plain", "Method Not Allowed");
- }
- }
- // Default not found handler
- void handleNotFound() {
- String message = "404 - 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);
- }
|