commit 07cbc7f56a261f1a0686f4f380162a120a89bdb1 Author: Tom Date: Tue Nov 5 13:02:19 2024 +0100 working diff --git a/IrSendRecv.ino b/IrSendRecv.ino new file mode 100644 index 0000000..40fb600 --- /dev/null +++ b/IrSendRecv.ino @@ -0,0 +1,363 @@ +#include +#include +#include +#include + +const char* ssid = "paradicsom"; +const char* password = "19700318"; + +String url = "http://192.168.50.69"; + +WebServer server(80); + +const char webpage[] PROGMEM = R"rawliteral( + + + + + + Arduino Remote Web + + + + + + + +
+
Power
+
Exit
+
Guide
+
Mute
+
Source
+
+ +
+
+
Volume up
+
Volume down
+
+
+
Channel up
+
Channel down
+
+
+ +
+
+
1
+
2
+
3
+
+
+
4
+
5
+
6
+
+
+
7
+
8
+
9
+
+
+
0
+
+
+ +
+ +
+ + +)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 // 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 +}