Files
ArduinoOnlineRemote/IrSendRecv.ino
2024-11-05 13:02:19 +01:00

364 lines
11 KiB
C++

#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
const char* ssid = "paradicsom";
const char* password = "19700318";
String url = "http://192.168.50.69";
WebServer server(80);
const char webpage[] PROGMEM = R"rawliteral(<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arduino Remote Web</title>
<style>
/* Base styles and color scheme */
:root {
--bg-color: #2c3e50;
--btn-color: #ecf0f1;
--btn-hover-color: #bdc3c7;
--text-color: #34495e;
--highlight-color: #3498db;
--border-radius: 10px;
--box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.2);
}
/* Body and basic styling */
body {
background-color: var(--bg-color);
font-family: Arial, sans-serif;
color: var(--btn-color);
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.flex-container {
display: flex;
flex-direction: row;
justify-content: center;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
.flex-container-column {
display: flex;
flex-direction: column;
justify-content: center;
gap: 10px;
}
/* Button and div styling */
.flex-container>div,
.flex-container-column>div {
background-color: var(--btn-color);
color: var(--text-color);
border-radius: var(--border-radius);
padding: 20px;
font-size: 24px;
font-weight: bold;
text-align: center;
box-shadow: var(--box-shadow);
transition: transform 0.2s ease, background-color 0.3s ease;
cursor: pointer;
flex: 1;
/* Allow buttons to grow and fill available space */
}
/* Hover and active effects */
.flex-container>div:hover,
.flex-container-column>div:hover {
background-color: var(--btn-hover-color);
transform: scale(1.05);
}
.flex-container>div:active,
.flex-container-column>div:active {
transform: scale(0.95);
background-color: var(--highlight-color);
color: #fff;
}
/* Input field styling */
#channel-input input {
width: 100%;
max-width: 300px;
height: 50px;
font-size: 18px;
text-align: center;
border: none;
border-radius: var(--border-radius);
padding: 10px;
box-shadow: var(--box-shadow);
transition: box-shadow 0.3s ease;
}
#channel-input input:focus {
outline: none;
box-shadow: 0px 4px 12px rgba(0, 123, 255, 0.4);
}
/* Cross-browser compatibility for hiding spinner arrows */
#channel-input input[type="number"] {
appearance: textfield;
/* Standard property */
-webkit-appearance: none;
/* WebKit browsers (Chrome, Safari, Edge) */
-moz-appearance: textfield;
/* Firefox */
}
/* Explicitly remove outer and inner spin buttons in WebKit browsers */
#channel-input input[type="number"]::-webkit-outer-spin-button,
#channel-input input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Numpad specific styles */
#numpad {
display: flex;
flex-direction: column;
gap: 5px;
/* Reduced gap between rows in the numpad */
}
#numpad div {
display: flex;
/* Use flex for rows */
justify-content: center;
/* Center buttons horizontally */
gap: 10px;
/* Space between buttons */
flex-wrap: nowrap;
/* Prevent wrapping to keep buttons in one row */
}
/* Center the number-0 button */
#number-0 {
flex: 1;
/* Allow the button to grow and take full width */
display: flex;
justify-content: center;
/* Center horizontally */
align-items: center;
/* Center vertically */
height: 60px;
/* Increase height for better tap target */
}
/* Responsive adjustments */
@media (max-width: 768px) {
.flex-container {
flex-direction: column;
}
/* Ensure numpad rows remain horizontal on smaller screens */
#numpad div {
flex-direction: row;
/* Set row layout for numpad buttons */
justify-content: space-between;
/* Distribute space between buttons */
}
}
@media (max-width: 480px) {
.flex-container>div,
.flex-container-column>div {
font-size: 20px;
padding: 15px;
}
#numpad div {
font-size: 20px;
padding: 10px;
}
#number-0 {
height: 60px;
/* Keep a consistent height for the number 0 button */
}
#channel-input input {
height: 40px;
font-size: 16px;
}
}
</style>
<script type="application/javascript">
//function sendButtonSignal(buttonCode) {
//console.log(buttonCode)
//fetch(`/sendSignal?value=${buttonCode}`)
//}
function sendButtonSignalField(){
//get value of input
//split it to individual numbers
//send it with delay
}
function sendButtonSignal(buttonCode) {
fetch('/sendSignal', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: buttonCode }) // Sending JSON data
})
}
</script>
</head>
<body>
<div id="buttons" class="flex-container">
<div id="power" onclick="sendButtonSignal(0x15)">Power</div>
<div id="exit" onclick="sendButtonSignal(0x13)">Exit</div>
<div id="guide" onclick="sendButtonSignal(0x47)">Guide</div>
<div id="mute" onclick="sendButtonSignal(0x16)">Mute</div>
<div id="source" onclick="sendButtonSignal(0x12)">Source</div>
</div>
<div class="flex-container">
<div id="volume" class="flex-container-column">
<div id="volume-up" onclick="sendButtonSignal(0x1B)">Volume up</div>
<div id="volume-down" onclick="sendButtonSignal(0x1A)">Volume down</div>
</div>
<div id="channel" class="flex-container-column">
<div id="channel-up" onclick="sendButtonSignal(0x19)">Channel up</div>
<div id="channel-down" onclick="sendButtonSignal(0x18)">Channel down</div>
</div>
</div>
<div id="numpad">
<div id="numpad-row-1" class="flex-container">
<div id="number-1" onclick="sendButtonSignal(0x1)">1</div>
<div id="number-2" onclick="sendButtonSignal(0x2)">2</div>
<div id="number-3" onclick="sendButtonSignal(0x3)">3</div>
</div>
<div id="numpad-row-2" class="flex-container">
<div id="number-4" onclick="sendButtonSignal(0x4)">4</div>
<div id="number-5" onclick="sendButtonSignal(0x5)">5</div>
<div id="number-6" onclick="sendButtonSignal(0x6)">6</div>
</div>
<div id="numpad-row-3" class="flex-container">
<div id="number-7" onclick="sendButtonSignal(0x7)">7</div>
<div id="number-8" onclick="sendButtonSignal(0x8)">8</div>
<div id="number-9" onclick="sendButtonSignal(0x9)">9</div>
</div>
<div id="numpad-row-4" class="flex-container">
<div id="number-0" onclick="sendButtonSignal(0x0)">0</div>
</div>
</div>
<div id="channel-input" class="flex-container">
<input id="channel-input-field" type="number" value="0" placeholder="Enter Channel" onchange="sendButtonSignalfield()"/>
</div>
</body>
</html>)rawliteral";
unsigned long lastTime = 0;
unsigned long timeDelay = 5000;
//#define DECODE_NEC // Includes Apple and Onkyo. To enable all protocols , just comment/disable this line.
#define NEC_PROTOCOL 8
#define IR_RECEIVE_PIN 2
#define IR_TRANSMIT_PIN 3
#define LED 7
#include <IRremote.hpp> // include the library
#define DELAY_AFTER_SEND 1000
#define DELAY_AFTER_LOOP 5000
bool switching = false;
void SendNecSignal(int buttonCode) {
Serial.print(F("Send NEC with 8 bit address code: "));
Serial.println(buttonCode);
Serial.flush();
IrSender.sendNEC(0x7F00, buttonCode, 1);
delay(DELAY_AFTER_SEND); //delay must be greater than 5 ms (RECORD_GAP_MICROS), otherwise the receiver sees it as one long signal
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("connecting to wifi");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.println(".");
}
Serial.println();
Serial.print("Connected to wifi. IP: ");
Serial.println(WiFi.localIP());
/////////////////////////////////////////////////////////
IrSender.begin(IR_TRANSMIT_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
// Specify send pin and enable feedback LED at default feedback LED pin
// Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
Serial.print(F("Ready to receive IR signals of protocols: "));
printActiveIRProtocols(&Serial);
pinMode(LED, OUTPUT);
// Define route for root
server.on("/", HTTP_GET, []() {
server.send(200, "text/html", webpage);
});
// Define route for LED toggle
server.on("/sendSignal", HTTP_POST, []() {
String json = server.arg("plain"); // Get JSON payload as a string
Serial.print("Received JSON: ");
Serial.println(json);
// Parse JSON
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, json);
if (error) {
Serial.print("Failed to parse JSON: ");
Serial.println(error.c_str());
server.send(400, "text/plain", "Invalid JSON");
return;
}
int code = doc["code"];
Serial.print("code: ");
Serial.println(code);
SendNecSignal(code);
Serial.println();
server.send(200, "text/plain", "SentSignal: " + code);
});
// Start the server
server.begin();
}
void loop() {
server.handleClient(); // Handle incoming client requests
}