MQTT to IR

I have an routine which takes an unsigned char and sends it as an IR command.

i am trying to receive this over mqtt using:

unsigned char code[100];
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
for(i=0; i<length; i++) {
code = payload*;*
* }*
sendHexCodetoIR(code, sizeof(code) / sizeof(code[0]), 38);
it seems to receive it over mqtt but "code" doesn't seem to actually contain what i send by mqtt
if i set example:
unsigned char test[] = {0xA6,0xAC,0x00,0x02,0x40,0xA0,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x54};
it works fine
your guidance onto the right track would be much appreciated

9H1LO:
I have an routine which takes an unsigned char and sends it as an IR command.

i am trying to receive this over mqtt using:

unsigned char code[100];
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
for(i=0; i<length; i++) {
code = payload*;*
* }*
sendHexCodetoIR(code, sizeof(code) / sizeof(code[0]), 38);
it seems to receive it over mqtt but "code" doesn't seem to actually contain what i send by mqtt
if i set example:
unsigned char test[] = {0xA6,0xAC,0x00,0x02,0x40,0xA0,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x54};
it works fine
your guidance onto the right track would be much appreciated
[/quote]
What kind of "IR code" do you plan to use? Infrared commands are usually "pulses" (i.e. on times) of a 40 khz carrier, with specific ON and OFF times to denote binary 1 and 0. Additionally, there is usually a header which signifies the beginning of an IR command (as well as being a "dummy" signal so that the IR receiver can adjust it's AGC).
A typical IR signal looks like this:
IR_pulses.jpg
The faint lines inside each "pulse" denote the IR LED pulsing on ans off at 40 khz.
So, to send an 8 bit character, you would send a header, then the appropriate 1 or 0 bits afterwards. You may want to also send along a checksum or a CRC so that the receiving end can tell if the received signal is valid.
**

Thanks for your reply. The IR part is working. I'm using code from AnalysIR as it's on an esp

If I use for example unsigned char test[] = {0xA6,0xAC,0x00,0x02,0x40,0xA0,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x54};

It works fine

However I want the array to be populated from the MQTT payload which comes in as bytes

I need to convert the payload to an unsigned char array it seems

still can't get my head around this....anyone ?

hello All,

I m trying to to control my TV over Wifi, i hv subscribed to the mqtt and receiving the HEX code in String now my problem id that i m not able to send the data to IR in string format i.e

irsend.sendRC6(data, 24);

gives an error saying "no matching function for call to 'IRsend::sendRC6(String&, int)"
so i tried converting string to uint16_t but that gave me the decimal number which again m not able to convert into hexadecimal.

can anyone help me in this respect , the code i m using is below:

void callback(char* topic, byte* payload, unsigned int length) {
String myString = "";
myString = String((char )payload);
Serial.println(myString);
const char
hexstring = myString.c_str();
uint16_t hex = atoi((char *)hexstring);

Serial.print("Hexa code : ");

Serial.println(hex);

irsend.sendRC6(hex, 24);
delay(2000);

}

    String  myString = "";
    myString = String((char *)payload);
    Serial.println(myString);
    const char* hexstring = myString.c_str();
    uint16_t hex = atoi((char *)hexstring);

This is uselessly pissing away resources. The payload array is the same size as a char array. All that it is missing is a NULL terminator. Wrapping it in a String adds a NULL terminator, but who knows where.

    payload[length] = '\0';

Puts it in the proper place.

Then, you can just pass payload (cast to char *) to atoi:

   uint16_t num = atoi((char *)payload);

However, that won't work, because atoi() expects the string to be a base 10 representation of an int. strtoul() can handle a string in any base.

    payload[length] = '\0';

Puts it in the proper place.

Then, you can just pass payload (cast to char *) to atoi:
[/quote]

I am not really a C expert, but didn't just potentially overwrite some random part of memory?

I am not really a C expert, but didn't just potentially overwrite some random part of memory?

Not if the payload array is large enough.

thanks a lot for your replies it really helped me simplifying the code ..but my problem is not yet solved ... i tried strtoul() , but it converts the string to respective base , but i need the same data which is in string i.e the exact HEX code in undefined int or some other data type to sent it to ir receiver pin.. i.e

irsend.sendRC6(hex, 24);
delay(2000);

the 'hex' in the above code cannot be a string or const char* .... it must be a hex code in undefined long or something.

Thank you

it must be a hex code in undefined long or something.

Snippets suck. Do NOT ever post a snippet again. Unless all you want is a snippet of help.

If that IS the case, you need to ... Well, that's enough of an answer for now.