Hi there, I'm trying to do a project for controlling windows media keys over wifi. When i try to control with push button everything works well but when i do this over wifi it does not work even with the push button. How can i solve this and what is the issue?
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Adafruit_TinyUSB.h>
// Wi-Fi
const char* ssid = "ssid";
const char* password = "password";
// Web
AsyncWebServer server(80);
// HID Media Report descriptor
uint8_t const desc_hid_report[] = {
TUD_HID_REPORT_DESC_CONSUMER()
};
// USB HID
Adafruit_USBD_HID usb_hid;
const char htmlPage[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>ESP32 HID Media Controller</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f4; }
h1 { color: #333; }
button { padding: 10px 20px; font-size: 16px; color: white; background-color: #007BFF; border: none; border-radius: 5px; cursor: pointer; margin: 5px; }
button:hover { background-color: #0056b3; }
</style>
<script>
function sendKey(key) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + key, true);
xhr.send();
}
</script>
</head>
<body>
<h1>ESP32 HID Media Controller</h1>
<button onclick="sendKey('playPause')">Play/Pause</button>
<button onclick="sendKey('nextTrack')">Next Track</button>
<button onclick="sendKey('previousTrack')">Previous Track</button>
</body>
</html>)rawliteral";
bool playpauseon = false;
void setup() {
// Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
pinMode(15, OUTPUT);
pinMode(0, INPUT_PULLUP);
// TinyUSB HID
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.begin();
while (!TinyUSBDevice.mounted()) {
delay(10);
}
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", htmlPage);
});
// Play/Pause
server.on("/playPause", HTTP_GET, [](AsyncWebServerRequest *request){
playpauseon = true;
request->send(200, "text/plain", "Play/Pause Gönderildi");
});
// Next
server.on("/nextTrack", HTTP_GET, [](AsyncWebServerRequest *request){
uint16_t mediaKey = HID_USAGE_CONSUMER_SCAN_NEXT;
usb_hid.sendReport(0, &mediaKey, sizeof(mediaKey));
request->send(200, "text/plain", "Next Track Gönderildi");
});
// Previous
server.on("/previousTrack", HTTP_GET, [](AsyncWebServerRequest *request){
uint16_t mediaKey = HID_USAGE_CONSUMER_SCAN_PREVIOUS;
usb_hid.sendReport(0, &mediaKey, sizeof(mediaKey));
request->send(200, "text/plain", "Previous Track Gönderildi");
});
// Web server
server.begin();
}
void loop() {
if (playpauseon || digitalRead(0) == LOW) {
uint16_t mediaKey = HID_USAGE_CONSUMER_PLAY_PAUSE;
usb_hid.sendReport(0, &mediaKey, sizeof(mediaKey));
digitalWrite(15, HIGH);
delay(300);
digitalWrite(15, LOW);
playpauseon = false;
}
delay(50);
}