#include <WiFi.h>
#include <WebServer.h>
#include <IRremote.h>
#include <IRsend.h>
// WiFi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// IR LED pin
const int irLedPin = D3; // You can change this to any suitable pin
// IR send object
IRsend irsend(irLedPin);
// Web server object
WebServer server(80);
// Function to send IR code
void sendIRCode(unsigned long code, int bits) {
irsend.sendRaw(&code, bits, 38); // Send the raw code.
delay(50); // Add a small delay
}
// Function to handle requests for device control
void handleCommand() {
String command = server.arg("command");
Serial.print("Received command: ");
Serial.println(command);
// TV Commands (Example)
if (command == "tv_on") {
sendIRCode(0x20DF10EF, 32); // Example NEC code for TV power on. Replace with your TV's actual code.
} else if (command == "tv_off") {
sendIRCode(0x20DF11EE, 32); // Example NEC code for TV power off. Replace with your TV's actual code.
} else if (command == "tv_channel_up") {
sendIRCode(0x20DF12ED, 32);
} else if (command == "tv_channel_down") {
sendIRCode(0x20DF13EC, 32);
}
//Roku Commands
else if (command == "roku_up") {
sendIRCode(0xFFB04F, 32); // Example for Roku Up
} else if (command == "roku_down") {
sendIRCode(0xFFB0CF, 32);
} else if (command == "roku_left") {
sendIRCode(0xFFB04D, 32);
} else if (command == "roku_right") {
sendIRCode(0xFFB04E, 32);
} else if (command == "roku_select") {
sendIRCode(0xFFB050, 32);
}
else if (command == "roku_home") {
sendIRCode(0xFFB068, 32);
}
else if (command == "roku_back") {
sendIRCode(0xFFB008, 32);
}
else if (command == "roku_play") {
sendIRCode(0xFFB020, 32);
}
else if (command == "roku_pause") {
sendIRCode(0xFFB020, 32);
}
else if (command == "roku_fwd") {
sendIRCode(0xFFB018, 32);
}
else if (command == "roku_rev") {
sendIRCode(0xFFB010, 32);
}
// Add more device commands here (e.g., for Fire TV, etc.)
else {
server.send(200, "text/plain", "Invalid command");
return;
}
server.send(200, "text/plain", "OK"); // Send a response to the client
}
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize IR sender
irsend.begin();
// Set up web server routes
server.on("/command", handleCommand); // Handle commands
server.on("/", []() { // Serve a simple HTML page
String html = "<h1>ESP8266 Universal Remote</h1>";
html += "<p>TV Controls:</p>";
html += "<button onclick=\"window.location.href='/command?command=tv_on'\">TV On</button>";
html += "<button onclick=\"window.location.href='/command?command=tv_off'\">TV Off</button>";
html += "<button onclick=\"window.location.href='/command?command=tv_channel_up'\">Channel Up</button>";
html += "<button onclick=\"window.location.href='/command?command=tv_channel_down'\">Channel Down</button>";
html += "<p>Roku Controls:</p>";
html += "<button onclick=\"window.location.href='/command?command=roku_up'\">Up</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_down'\">Down</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_left'\">Left</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_right'\">Right</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_select'\">Select</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_home'\">Home</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_back'\">Back</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_play'\">Play/Pause</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_fwd'\">Fwd</button>";
html += "<button onclick=\"window.location.href='/command?command=roku_rev'\">Rev</button>";
html += "<p>Add more buttons for other devices as needed</p>";
server.send(200, "text/html", html);
});
server.begin();
Serial.println("Web server started");
}
void loop() {
server.handleClient();
}