Files
arduino-sony-minidisc-ir-re…/arduino-sony-minidisc-ir-remote.ino
T

313 lines
7.5 KiB
Arduino

/*
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 <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <IRremote.hpp>
#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 sendButton12(int command)
{
Serial.print("sendButton12 command: ");
Serial.println(command);
Serial.flush();
IrSender.sendSony(0xF, command, 2, 12);
}
void sendCommand12(String commandString)
{
if (commandString == "POWER")
sendButton12(POWER);
if (commandString == "PLAY")
sendButton12(PLAY);
if (commandString == "PAUSE")
sendButton12(PAUSE);
if (commandString == "STOP")
sendButton12(STOP);
if (commandString == "PREV")
sendButton12(PREV);
if (commandString == "NEXT")
sendButton12(NEXT);
else
{
Serial.println("wrong command!");
}
}
void sendButton20(int command)
{
Serial.print(F("Send Sony/SIRCS with 7 command and 13 address bits: "));
Serial.println(command);
Serial.flush();
IrSender.sendSony(0xF & 0x1FFF, command & 0x7F, 2, SIRCS_20_PROTOCOL);
}
void sendCommand20(String commandString)
{
if (commandString == "Q")
sendButton20(Q);
if (commandString == "W")
sendButton20(W);
if (commandString == "E")
sendButton20(E);
if (commandString == "R")
sendButton20(R);
if (commandString == "T")
sendButton20(T);
if (commandString == "Z")
sendButton20(Z);
else
{
Serial.println("wrong command!");
}
}
//------------------------------------------------
//------------- 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 <IP>/api/gettextinput?message=<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 <IP>/api/control-led with a form field message set to <message>
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 <IP>/api/gettext with a form field message set to <message>
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 <IP>/api/control-deck with a form field message set to <message>
server.on("/api/control-deck", HTTP_POST, [](AsyncWebServerRequest *request)
{
String message;
if (request->hasParam("control", true)) {
message = request->getParam("control", true)->value();
sendCommand12(message);
} else {
message = "No message sent";
}
request->send(SD, "/index.html", "text/html"); });
// Send a POST request to <IP>/api/keyboard-input with a form field message set to <message>
server.on("/api/keyboard-input", HTTP_POST, [](AsyncWebServerRequest *request)
{
String message;
if (request->hasParam("key", true)) {
message = request->getParam("key", true)->value();
sendCommand20(message);
} else {
message = "No message sent";
}
request->send(SD, "/index.html", "text/html"); });
server.onNotFound(notFound);
server.begin();
}
void loop()
{
}