Need help with IR encrypting

Hey, making a Program where I can send strings via IR. The String is always 5 letters long and only contains A-I . It's sending and receiving data but not the same as it's sending.
Does someone have an idea why?

My Code:

#include <IRremote.h>

const int IR_SEND_PIN = 19;
const int IR_RECEIVE_PIN = 18;

void setup() {
  Serial.begin(9600);
  // Initialize the IR sender and receiver
  IrSender.begin(IR_SEND_PIN, DISABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void sendIRSignal(uint32_t value) {
  IrSender.sendNEC(value, 32); // Send NEC code with 32-bit data
  Serial.println("Sent");
}

uint32_t stringToIRCode(const String& input) {
  uint32_t code = 0;
  for (int i = 0; i < 5; i++) {
    char c = input.charAt(i);
    if (c >= 'A' && c <= 'I') {
      code = (code << 4) | (c - 'A');
    } else {
      return 0; // Invalid character
    }
  }
  return code;
}

String IRCodeToString(uint32_t code) {
  String result = "";
  for (int i = 0; i < 5; i++) {
    uint8_t value = (code >> (4 * (4 - i))) & 0xF;
    if (value >= 0 && value <= 8) {
      result += char('A' + value);
    } else {
      return ""; // Invalid value
    }
  }
  return result;
}

void loop() {
  if (Serial.available() > 0) {
    String input = Serial.readStringUntil('\n');
    input.trim(); // Remove whitespace

    if (input.length() == 5) {
      uint32_t code = stringToIRCode(input);
      if (code != 0) {
        sendIRSignal(code);
      } else {
        Serial.println("Invalid input. Only letters A-I are allowed.");
      }
    } else {
      Serial.println("Invalid input. The input must be exactly 5 letters long.");
    }
  }

  // Receive an IR signal
  if (IrReceiver.decode()) { // Check if an IR signal has been received
    uint32_t receivedCode = IrReceiver.decodedIRData.decodedRawData;
    Serial.print("Received signal: ");
    Serial.println(receivedCode, HEX); // Output the received value

    String decodedString = IRCodeToString(receivedCode);
    if (!decodedString.isEmpty()) {
      Serial.print("Decoded string: ");
      Serial.println(decodedString);
    } else {
      Serial.println("Invalid IR code received.");
    }

    IrReceiver.resume(); // Prepare the receiver for the next signal
  }
}


Some examples:

(the number after Sent is the variable "value" in void sendIRSignal)

Want to send: IABCC
Output: 19:40:54.168 -> Sent (524578)
19:40:54.168 -> Received signal: 44801000
19:40:54.201 -> Decoded string: ABAAA

Want to send: AABBC
Output: 19:40:54.168 -> Sent 274
19:40:54.168 -> Received signal: 44800000
19:40:54.201 -> Decoded string: AAAAA

Want to send: ABCDE
Output: 19:40:54.168 -> Sent 4660
19:40:54.168 -> Received signal: 2C480000
19:40:54.201 -> Decoded string: IAAAA

Want to send: IGHAC
Output: 19:40:54.168 -> Sent 550658
19:40:54.168 -> Received signal: 40E61000
19:40:54.201 -> Decoded string: GBAAA

My guess it that the algorithm and/or the code is not correct.

Post some examples that demonstrate the problem.

@jremington
Okay, here are some:
(the number after Sent is the variable "value" in void sendIRSignal)

Want to send: IABCC
Output: 19:40:54.168 -> Sent (524578)
19:40:54.168 -> Received signal: 44801000
19:40:54.201 -> Decoded string: ABAAA

Want to send: AABBC
Output: 19:40:54.168 -> Sent 274
19:40:54.168 -> Received signal: 44800000
19:40:54.201 -> Decoded string: AAAAA

Want to send: ABCDE
Output: 19:40:54.168 -> Sent 4660
19:40:54.168 -> Received signal: 2C480000
19:40:54.201 -> Decoded string: IAAAA

Want to send: IGHAC
Output: 19:40:54.168 -> Sent 550658
19:40:54.168 -> Received signal: 40E61000
19:40:54.201 -> Decoded string: GBAAA

First, verify that you can send and receive unencrypted signals. When that is working, add the encryption.

Tryd to send data raw like this:
const uint16_t rawData[] = {9000, 4500, 4500, 4500, 4500, 4500, 4500, 4500};

but not even that worked, The receiver always gets other rawdata thats always kinda random (always around the same but quite some differences) tryd multiple receivers.

I`m using this one: ZkeeShop 3 Sets 38 kHz IR Receiver IR Transmitter Sensor Module Kit Compatible with Arduino: Amazon.de: Business, Industry & Science

Why could it fail to even send raw data, I'm clueless

My code

#include <IRremote.h>

const int IR_SEND_PIN = 19;
const int IR_RECEIVE_PIN = 18;

// Example raw data for a short IR signal
const uint16_t rawData[] = {9000, 4500, 4500, 4500, 4500, 4500, 4500, 4500};
const int rawDataLength = sizeof(rawData) / sizeof(rawData[0]);

void setup() {
  Serial.begin(9600);
  // Initializes the IR sender and receiver
  IrSender.begin(IR_SEND_PIN, DISABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("IR sender and receiver initialized");
}

void sendIRSignal() {
  IrSender.sendRaw(rawData, rawDataLength, 38); // Sends raw data with 38 kHz carrier frequency
  Serial.println("Sent:");
  for (int i = 0; i < rawDataLength; i++) {
    Serial.print(rawData[i]);
    Serial.print(" ");
  }
  Serial.println();
}

void loop() {
  // Sending an IR signal
  sendIRSignal();
  delay(3000); // Wait time between transmissions

  // Receiving an IR signal
  if (IrReceiver.decode()) { // Checks if an IR signal has been received
    Serial.print("Received signal: ");
    for (uint16_t i = 0; i < IrReceiver.decodedIRData.rawDataPtr->rawlen; i++) {
      Serial.print(IrReceiver.decodedIRData.rawDataPtr->rawbuf[i]);
      Serial.print(" ");
    }
    Serial.println();
    IrReceiver.resume(); // Prepares the receiver for the next signal
  }
}

I receive Rawdatas like these:
0 182 89 91 89 91 89 92
0 181 90 90 90 90 90 90
0 181 90 90 90 90 90 91

000010101011101010101010 = ABAAA (24 bits)
000010101010111001101111101000 = 4401000 (32 bits)

Probably dropping (or adding) the last (or first) eight bits and switching LSB with MSB.

I suggest to spend some more time with the documentation and studying the examples in the IRRemote library.

It is essential that you understand how to send raw data and the limitations of the method first. Once you understand the examples, you should have no problem getting your encoding scheme to work.

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