1

I'm attempting to use a raspberry pi to send ir codes directly to my arduino to have as simple as a C program as possible. Here's my code I've been tinkering with that leads to errors or not working if I don't specifically hardcode the hex ir signal.

#include <IRremote.h>

char buf[80];

String code = "empty";

int readline(int readch, char *buffer, int len) {
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\r': // Ignore CR
        break;
      case '\n': // Return on new-line
        rpos = pos;
        pos = 0; // Reset position index ready for next time
        return rpos;
      default:
        if (pos < len-1) {
          buffer[pos++] = readch;
          buffer[pos] = 0;
        }
    }
  }
  return 0;
}


void setup() {
  Serial.begin(115200);
}

IRsend irsend;

void loop() {
  if (readline(Serial.read(), buf, 80) > 0) {
    Serial.print("You entered: >");
    //Serial.print(buf);
    //Serial.println("<");
    //irsend.sendNEC((uint32_t) buf, 32);
    String str(buf);
    //irsend.sendNEC((uint32_t) code, 32);
    //irsend.sendNEC(0x210704FB, 32);
    code = buf;
    delay(600);
    //Serial.print((uint32_t) buf, 32);
    Serial.print(code);
    irsend.sendNEC(buf, 32);
    
  }

I've left my mess on the bottom to illustrate how little I know and what I've tried. I've been trying to figure out what I'm sending it but it doesn't help that serial.print doesn't work like irsend. I'm intending to send hex codes directly and have the arduino blindly plug them into irsend.sendNEC(0x210704FB, 32); which works but when I try to put my own variable in it won't.

Very much appreciate any help! I know it'd be easy for anybody who knows a little C.

irsend.sendNEC(buf, 32); returns: initializing argument 1 of 'void IRsend::sendNEC(uint32_t, uint8_t, bool)' void sendNEC(uint32_t data, uint8_t nbits, bool repeat = false);

and irsend.sendNEC((uint32_t) buf, 32); doesn't throw any errors but also does not seem to do anything

3
  • 2
    when I try to put my own variable ... what variable? ... what is its value?
    – jsotola
    Nov 15 '20 at 7:26
  • 1
    warning: invalid conversion from 'char*' to 'long unsigned int' [-fpermissive] irsend.sendNEC(buf, 32);
    – KIIV
    Nov 15 '20 at 7:52
  • Trying to use buf. irsend.sendNEC(buf, 32); irsend.sendNEC((uint32_t) buf, 32); don't work added two more sentences to the bottom to help clear anything up Nov 15 '20 at 8:44
2

If you are sending HEX values like 0x1234abcd from the Pi to the Arduino to send those as an IR code you first have to interpret that string data as a hexadecimal number.

That's not as simple as just casting the string since, as @timemage says, all you're doing there is casting the address of the character array.

Instead you should use strtoul() to parse the string and give you the equivalent number to what it contains.

strtoul() can convert numbers in a number of different bases, and can even work automatically. From the Linux man page:

SYNOPSIS

unsigned long int strtoul(const char *nptr, char **endptr, int base);

And later on:

If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).

That means it can automatically parse 0x1234abcd as a hexadecimal number, 12345678 as a decimal number, and 052714 as an octal number (it's important to remember that one so you don't accidentally add a leading zero to a decimal number and have it parsed wrong).

So knowing all that you can now:

uint32_t code = strtoul(buffer, NULL, 0);

and code will contain the contents of buffer represented as an actual numeric variable (assuming the content can be parsed as a number).

1
  • It works! Thank you so so much. I think this is the last bit of C that I needed for this project. I appreciate the help so much. It's for remotely controlling my mother's tv while I'm at work. She has dementia and is at the point where she'll give up on the tv and sit in silence all day and her mind comes up with lots of scary thoughts by the time I'm back over to check on her. With regular cable she always winds up watching the news and in her state she feels genuine distraught and fear and spirals downward. You've helped us so so much. Nov 16 '20 at 2:40
1

The library you're using and the protocols it implements are not for sending large chunks of data.

The sendNEC function you're using is simply limited to 32 bits per transmission. You're not going to be able to send an arbitrary length string with it, except by constructing some protocol around it that breaks the string into multiple transmissions of as much as 32 bits at a time.

Without splitting hairs about the language standard and qualifying everything to be perfectly accurate: what you're doing with your (uint32_t) cast is sending the memory address of your string over IR, which is no doubt not what you wanted nor likely particularly useful on the receiving end.

What you appear to want is something closer to IrDA. That said, you probably could do what you want by calling sendNEC() on each character of your data in the simple case. Better would be to send 32-bits of the string at a time since there is overhead to each transmission. But again, these functions and protocols are designed for sending short, infrequent messages. They were designed to be dirt simple so that they could on be implemented hardware a fraction of the power of a typical Arduino, so that they could be cheap decades ago when when they were inverted. In an effort to be both simple and at least somewhat reliable, they sacrifice a lot of speed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.