/* WebServer to use as replacement for Sony RM-D27M to control Sony MiniDisc decks. Based on the the following sketches: - SimpleWifiServer example - ESP32/SD_Test - IRremote/SendDemo If the IP address of your shield is yourAddress: http://yourAddress/H turns the LED on http://yourAddress/L turns it off Circuit: - LED attached to pin 13 (optional) - IR Transmitter attached to pin 4 (connect to 5v or 3v3) - SD-Module to load HTML and CSS from SD-card Connect the SD card to the following pins: SD Card | ESP32 ------- | ----- 3v3 | 3.3V CS | 5 MOSI | 23 CLK | 18 MISO | 19 GND | GND developed for the ESP32vn IoT Uni board 2024 */ //----------------------------------------- // Arduino Web Server using AJAX // HTML code of webpage is stored on SD card //----------------------------------------- #include #include #include #include #include "FS.h" #include "SD.h" #include "SPI.h" #include #include "secrets.h" #include "defines.h" //------------------------------------------------ // Create AsyncWebServer object on port 80 AsyncWebServer server(80); // WiFiServer server(80); const char *PARAM_MESSAGE = "message"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } //------------------------------------------------ //-------------- SD-Card Functions --------------- void initSDCard() { if (!SD.begin()) { Serial.println("Card Mount Failed"); return; } uint8_t cardType = SD.cardType(); if (cardType == CARD_NONE) { Serial.println("No SD card attached"); return; } Serial.print("SD Card Type: "); if (cardType == CARD_MMC) { Serial.println("MMC"); } else if (cardType == CARD_SD) { Serial.println("SDSC"); } else if (cardType == CARD_SDHC) { Serial.println("SDHC"); } else { Serial.println("UNKNOWN"); } uint64_t cardSize = SD.cardSize() / (1024 * 1024); Serial.printf("SD Card Size: %lluMB\n", cardSize); } void listDir(fs::FS &fs, const char *dirname, uint8_t levels) { Serial.printf("Listing directory: %s\n", dirname); File root = fs.open(dirname); if (!root) { Serial.println("Failed to open directory"); return; } if (!root.isDirectory()) { Serial.println("Not a directory"); return; } File file = root.openNextFile(); while (file) { if (file.isDirectory()) { Serial.print(" DIR : "); Serial.println(file.name()); if (levels) { listDir(fs, file.path(), levels - 1); } } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print(" SIZE: "); Serial.println(file.size()); } file = root.openNextFile(); } } //------------------------------------------------ //--------------- IR Functions ------------------- void initIR() { IrSender.begin(IR_SEND_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Specify send pin and enable feedback LED at default feedback LED pin Serial.println(); Serial.print(F("Send IR signals at pin ")); Serial.println(IR_SEND_PIN); } void sendButton(int command) { Serial.println("sendButton command: "); Serial.println(command); Serial.flush(); IrSender.sendSony(0xF, command, 2, 12); } //------------------------------------------------ //------------- Webserver Functions -------------- void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(SSID, PASSWORD); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); } //------------------------------------------------ //-------------------- Setup --------------------- void setup() { Serial.begin(115200); initWiFi(); initSDCard(); pinMode(LED_PIN, OUTPUT); // set the LED pin mode initIR(); server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { request->send(SD, "/index.html", "text/html"); }); server.serveStatic("/", SD, "/"); // Send a GET request to /api/gettextinput?message= server.on("/api/gettextinput", HTTP_GET, [](AsyncWebServerRequest *request) { String message; if (request->hasParam("text_input")) { message = request->getParam("text_input")->value(); Serial.println(message); } else { message = "No message sent"; } request->send(SD, "/index.html", "text/html"); }); // Send a POST request to /api/control-led with a form field message set to server.on("/api/control-led", HTTP_POST, [](AsyncWebServerRequest *request) { String message; if (request->hasParam("led", true)) { message = request->getParam("led", true)->value(); if (message == "1") digitalWrite(LED_PIN, HIGH); if (message == "0") digitalWrite(LED_PIN, LOW); } else { message = "No message sent"; } request->send(SD, "/index.html", "text/html"); }); // Send a POST request to /api/gettext with a form field message set to server.on("/api/gettext", HTTP_POST, [](AsyncWebServerRequest *request) { String message; if (request->hasParam("text_box", true)) { message = request->getParam("text_box", true)->value(); Serial.println(message); } else { message = "No message sent"; } request->send(SD, "/index.html", "text/html"); }); // Send a POST request to /api/control-deck with a form field message set to server.on("/api/control-deck", HTTP_POST, [](AsyncWebServerRequest *request) { String message; if (request->hasParam("control", true)) { message = request->getParam("control", true)->value(); //Serial.print(message); sendButton(message); /* if (message == "POWER") sendButton(POWER); if (message == "PLAY") sendButton(PLAY); if (message == "PAUSE") sendButton(PAUSE); if (message == "STOP") sendButton(STOP); if (message == "PREV") sendButton(PREV); if (message == "NEXT") sendButton(NEXT); */ } else { message = "No message sent"; } request->send(SD, "/index.html", "text/html"); }); server.onNotFound(notFound); server.begin(); } void loop() { }