Web Based IR Remote Control Project


I have trouble sending IR code address, command, hex code, and bit rate because when it transmit doesn't match on what I receive. What is the problem in this?

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <IRrecv.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <IRutils.h>

// Declare sendIRCommand function
void sendIRCommand(uint8_t address, uint8_t command, unsigned long hexCode, uint8_t bits);

// IR pins
const uint16_t kRecvPin = 14;  // D5 on ESP8266
const uint16_t kIrLedPin = 4;  // D2 on ESP8266

// LEDs
const int greenLed = 13;  // D6 - Success indicator
const int redLed = 12;    // D7 - Power indicator for TV or AC

IRrecv irrecv(kRecvPin);
decode_results results;
IRsend irsend(kIrLedPin);

// AP credentials (Hotspot)
const char* ssid = "ESP8266_Remote";
const char* password = "12345678";  // Min 8 characters for AP mode

// Web server on port 80
ESP8266WebServer server(80);

// Power states for TV and AC
bool tvPowerState = false;  // False means off, true means on
bool acPowerState = false;  // False means off, true means on

// Timing variables
unsigned long lastCommandTime = 0;  // Time of the last sent command
const unsigned long commandDelay = 2000;  // Delay in milliseconds between commands

// HTML for homepage
const char* homepageHTML = R"=====(<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remote Control Homepage</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; padding-top: 50px; }
        a { text-decoration: none; display: inline-block; margin: 20px; padding: 20px; background-color: #4CAF50; color: white; font-size: 20px; border-radius: 10px; }
        a:hover { background-color: #45a049; }
    </style>
</head>
<body>
    <h1>Welcome to the Virtual Remote</h1>
    <a href="/tv">Control TV</a>
    <a href="/ac">Control AC</a>
</body>
</html>)=====";

// HTML for TV remote
const char* tvHTML = R"=====(<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TV Remote Control</title>
    <style>
        body { font-family: Arial, sans-serif; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f9; }
        h1 { font-family: Arial, sans-serif; text-align: center; }
        .remote { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; padding: 20px; background-color: #333; border-radius: 20px; width: 300px; justify-items: center; align-items: center; }
        .remote button { width: 70px; height: 70px; font-size: 16px; color: white; cursor: pointer; background-color: #444; border-radius: 10px; }
        .remote button:active { background-color: #555; }
        .power-btn { grid-column: 1 / 4; background-color: #e70a03; height: 80px; }
        .ch-btn { background-color: #5cb85c; }
        .vol-btn { background-color: #5bc0de; }
        .back-btn { margin-top: 20px; padding: 10px 20px; background-color: #4CAF50; border-radius: 10px; font-size: 18px; color: white; cursor: pointer; }
    </style>
</head>
<body>
    <h1>TV Virtual Remote</h1>
    <div class="remote">
        <button class="power-btn" onclick="sendCommand('power_on')">Power On</button>
        <button class="vol-btn" onclick="sendCommand('volume_up')">Vol +</button>
        <button onclick="sendCommand('direction_up')">&#9650;</button>
        <button class="vol-btn" onclick="sendCommand('volume_down')">Vol -</button>
        <button onclick="sendCommand('direction_left')">&#9664;</button>
        <button onclick="sendCommand('ok')">OK</button>
        <button onclick="sendCommand('direction_right')">&#9654;</button>
        <button class="ch-btn" onclick="sendCommand('channel_up')">CH +</button>
        <button onclick="sendCommand('direction_down')">&#9660;</button>
        <button class="ch-btn" onclick="sendCommand('channel_down')">CH -</button>
        <button onclick="sendCommand('mute')">Mute</button>
        <button onclick="sendCommand('home')">Home</button>
        <button onclick="sendCommand('back')">Back</button>
        <button onclick="sendCommand('apps')">Apps</button>
        <button onclick="sendCommand('settings')">Settings</button>
        <button onclick="sendCommand('exit')">Exit</button>
    </div>

    <button class="back-btn" onclick="goBack()">Back to Homepage</button>

    <script>
        function sendCommand(command) {
            fetch('/' + command, { method: 'GET' })
                .then(response => console.log(command + ' sent'))
                .catch(error => console.error('Error:', error));
        }

        function goBack() {
            window.location.href = '/'; // Redirect to homepage
        }
    </script>
</body>
</html>)=====";

// HTML for AC remote
const char* acHTML = R"=====(<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AC Remote Control</title>
    <style>
        body { font-family: Arial, sans-serif; display: flex; flex-direction: column; justify-content: center; align-items: center; height: 100vh; background-color: #f4f4f9; }
        h1 { font-family: Arial, sans-serif; text-align: center;}
        .remote { display: grid; grid-template-columns: repeat(2, 1fr); grid-gap: 10px; padding: 20px; background-color: #333; border-radius: 20px; width: 200px; justify-items: center; align-items: center; }
        .remote button { width: 70px; height: 70px; font-size: 16px; color: white; cursor: pointer; background-color: #444; border-radius: 10px; }
        .remote button:active { background-color: #555; }
        .power-btn { grid-column: 1 / 3; background-color: #e70a03; height: 80px; }
        .back-btn { margin-top: 20px; padding: 10px 20px; background-color: #4CAF50; border-radius: 10px; font-size: 18px; color: white; cursor: pointer; }
        select { padding: 10px; font-size: 16px; margin-bottom: 20px; }
    </style>
</head>
<body>
    <h1>AC Virtual Remote</h1>

    <label for="acType">Select AC Type:</label>
    <select id="acType" onchange="setACType()">
        <option value="lg">LG</option>
        <option value="samsung">Samsung</option>
        <option value="daikin">Daikin</option>
        <option value="general">General</option>
    </select>

    <div class="remote">
        <button class="power-btn" onclick="sendCommand('power_ac')">Power</button>
        <button onclick="sendCommand('temp_up')">Temp +</button>
        <button onclick="sendCommand('temp_down')">Temp -</button>
        <button onclick="sendCommand('swing_vertical')">Swing V</button>
        <button onclick="sendCommand('swing_horizontal')">Swing H</button>
        <button onclick="sendCommand('fan_speed')">Fan Speed</button>
        <button onclick="sendCommand('mode')">Mode</button>
        <button onclick="sendCommand('energy_control')">Energy Control</button>
        <button onclick="sendCommand('jet_mode')">Jet Mode</button>
        <button onclick="sendCommand('comfort_air')">Comfort Air</button>
        <button onclick="sendCommand('light_off')">Light Off</button>
    </div>

    <button class="back-btn" onclick="goBack()">Back to Homepage</button>

    <script>
        let acType = 'lg';

        function setACType() {
            acType = document.getElementById('acType').value;
            console.log('Selected AC Type:', acType);
        }

        function sendCommand(command) {
            fetch('/' + command + '?type=' + acType, { method: 'GET' })
                .then(response => console.log(command + ' sent'))
                .catch(error => console.error('Error:', error));
        }

        function goBack() {
            window.location.href = '/'; // Redirect to homepage
        }
    </script>
</body>
</html>)=====";

// Function to send IR command
void sendIRCommand(uint8_t address, uint8_t command, unsigned long hexCode, uint8_t bits) {
    // Print parameters for debugging
    Serial.print("Sending IR command - Address: 0x");
    Serial.print(address, HEX);
    Serial.print(", Command: 0x");
    Serial.print(command, HEX);
    Serial.print(", Hex Code: 0x");
    Serial.print(hexCode, HEX);
    Serial.print(", Bit Rate: ");
    Serial.println(bits);

    // Send the command using the correct protocol
    if (bits == 32) {
        irsend.sendNEC(hexCode, bits);  // Send NEC code
    } else if (bits == 27) {
        irsend.sendNEC(hexCode, bits);  // Send LG AC code
    }

    digitalWrite(greenLed, HIGH);  // Turn on the green LED to indicate success
    delay(1000);
    digitalWrite(greenLed, LOW);  // Turn off after delay

    lastCommandTime = millis();  // Update the last command time
}

void setup() {
    Serial.begin(115200);
    irsend.begin();       // Start IR transmitter
    irrecv.enableIRIn();  // Start the IR receiver

    pinMode(greenLed, OUTPUT);
    pinMode(redLed, OUTPUT);

    // Configure ESP8266 as an Access Point
    WiFi.softAP(ssid, password);

    Serial.println("Access Point started");
    Serial.print("IP Address: ");
    Serial.println(WiFi.softAPIP());

    // Routes for HTML pages
    server.on("/", []() { server.send(200, "text/html", homepageHTML); });
    server.on("/tv", []() { server.send(200, "text/html", tvHTML); });
    server.on("/ac", []() { server.send(200, "text/html", acHTML); });

    // Handle TV commands
    server.on("/power_on", []() {
        if (!tvPowerState) {
            sendIRCommand(0x4, 0x5A, 0xA55AFB04, 32);  // Address: 0x4, Command: 0x5A, Hex Code: 0xA55AFB04, Bit Rate: 32
            tvPowerState = true;  // Update state to ON
        } else {
            sendIRCommand(0x4, 0x5B, 0xA55AFB04, 32);  // Example power off command
            tvPowerState = false;  // Update state to OFF
        }
    });
    // Other TV command handlers...
    
    // Handle AC commands based on the selected type
    server.on("/power_ac", []() {
        if (!acPowerState) {
            sendIRCommand(0x4, 0x5A, 0xFD10EF, 27);  // Example address, command, and hex code for AC power on
            acPowerState = true;  // Update state to ON
        } else {
            sendIRCommand(0x4, 0x5B, 0xFD5AA5, 27);  // Example power off command
            acPowerState = false;  // Update state to OFF
        }
    });
    // Other AC command handlers...

    server.begin();  // Start the web server
    Serial.println("Server started");
}

void loop() {
    digitalWrite(redLed, HIGH);  // Turn on the red LED to indicate server activity
    server.handleClient();  // Handle web requests
    digitalWrite(redLed, LOW);   // Turn off the red LED
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.