# Implementation of the received infrared code possible?

**URL:** <https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920>\
**Category:** Integrations\
**Created:** [May 23, 2021, 12:14pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920 "2021-05-23T12:14:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [May 23, 2021, 12:14pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/1 "2021-05-23T12:14:07Z")

</div>

Hello,

would it be possible to transmit the received HEX codes via MQTT (my preferred way) or to provide them via the JSON-API ?

---

<div class="post-metadata">

**Author:** ![huggy-d1](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/huggy-d1/32/51_2.png) [@huggy-d1](https://wled.discourse.group/u/huggy-d1)\
**Post date:** [May 24, 2021, 4:30pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/2 "2021-05-24T16:30:58Z")

</div>

I like the way you think.  
Maybe on the info page, if IR enabled, display last IR code received.  
That might not be what you are after, but a custom usermod would certainly do what you are looking for.  
I think if WLED were able to send say the last 10-16 IR codes in a single JSON-api request, you could push that many IR remote buttons and capture all the button codes in a single call. Then in a different API call, send just the last IR code received, or trigger an MQTT send for each IR code received.

If you want to build a usermod, if it is straightforward, @Aircoookie might merge it into WLED for everyone. If not, you’d still have your own problem solved.

---

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [June 4, 2021, 6:23pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/3 "2021-06-04T18:23:17Z")

</div>

I found this suggestion to expand the ir.cpp.

```auto
if (WLED_MQTT_CONNECTED) {
  char s[16];
  char subuf[38];

  sprintf(s,"0x%08x",code);
  strcpy(subuf, mqttDeviceTopic);
  strcat(subuf, "/ircode");
  mqtt->publish(subuf, 0, true, s);
}

```

[here](https://github.com/Aircoookie/WLED/issues/962)

If I now want to build the firmware myself, I get this message:  
…  
In file included from C:\Users\bla\WLED-master\WLED-master\wled00\wled.h:99,  
from C:\Users\bla\WLED-master\WLED-master\wled00\wled00.ino:13:  
C:\Users\bla\WLED-master\WLED-master\wled00\src\dependencies\espalexa\Espalexa.h:620:2: error: #endif without #if  
620 | #endif  
| ^ ~~~~  
…

What and where do I have to change at Espalexa.h ?

---

<div class="post-metadata">

**Author:** ![Artacus](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/artacus/32/1258_2.png) [@Artacus](https://wled.discourse.group/u/Artacus)\
**Post date:** [June 5, 2021, 5:13am UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/4 "2021-06-05T05:13:54Z")

</div>

I’ve been thinking about a better way to get the ir codes. Especially since the next release should allow the users to add support for most any remote without any coding or compiling. But you need to be able to get the IR codes in order to do that.

The codes are currently echoed on the serial port. Beginning in 0.13 it will be debug prints instead. Not hard for developers, but not easy for most users. Having the last 10 or 15 codes received in the JSON response is probably overly optimistic for being able to piece back together and map your buttons. Speaking as someone who has spent a fair amount of time mapping codes to buttons.

Using mqtt isn’t a bad idea, but still leaves the bar a little higher than I’d like.

---

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [June 5, 2021, 5:57am UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/5 "2021-06-05T05:57:38Z")

</div>

I don’t want to use the IR codes to switch the LEDs, I want to be able to switch any device (in FHEM).  
In practice, I just want to save myself an additional, for example, Tasmota device or something else.  
And MQTT would be my preferred way of getting the codes.

---

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [June 20, 2021, 12:48pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/6 "2021-06-20T12:48:20Z")

</div>

Hello,

I have now managed to compile myself, to expand the ir.cpp with the suggestion I found (see above).  
Unfortunately, the reception of the IR codes does not work.  
Nothing happens in the serial monitor.  
I checked the TSOP used with the IRrecvDumpV2-Sketch for functionality, it receives the codes from my remote control without any problems.  
In the ir.cpp I read that you can change the pin in the NpbWrapper.h.  
Where is this file, can’t find it?

---

<div class="post-metadata">

**Author:** ![Artacus](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/artacus/32/1258_2.png) [@Artacus](https://wled.discourse.group/u/Artacus)\
**Post date:** [June 21, 2021, 12:43am UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/7 "2021-06-21T00:43:36Z")

</div>

Newer versions don’t echo codes to serial and use debug instead.

---

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [June 21, 2021, 12:54pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/8 "2021-06-21T12:54:55Z")

</div>

I now have `#define WLED_DEBUG`  
added at the top of the WLED.cpp.

Now I have `--- DEBUG INFO ---` output in the serial monitor, but I still don’t see any received codes.

Regardless of whether there are debug outputs or not, no codes with the proposed extension are transmitted via MQTT  
Incidentally, I added the following to ir.cpp:

```auto
void decodeIR(uint32_t code)
{ if (WLED_MQTT_CONNECTED) {
  char s[16];
  char subuf[38];

  sprintf(s,"0x%08x",code);
  strcpy(subuf, mqttDeviceTopic);
  strcat(subuf, "/ircode");
  mqtt->publish(subuf, 0, true, s);
}
  if (code == 0xFFFFFFFF) //repeated code, continue brightness up/down
  {
    irTimesRepeated++;
    applyRepeatActions();
    return;
  }
  lastValidCode = 0; irTimesRepeated = 0;
  if (decodeIRCustom(code)) return;
  if (code > 0xFFFFFF) return; //invalid code
  else if (code > 0xF70000 && code < 0xF80000) decodeIR24(code); //is in 24-key remote range
  else if (code > 0xFF0000) {
    switch (irEnabled) {
      case 1: decodeIR24OLD(code); break; // white 24-key remote (old) - it sends 0xFF0000 values
      case 2: decodeIR24CT(code); break; // white 24-key remote with CW, WW, CT+ and CT- keys
      case 3: decodeIR40(code); break; // blue 40-key remote with 25%, 50%, 75% and 100% keys
      case 4: decodeIR44(code); break; // white 44-key remote with color-up/down keys and DIY1 to 6 keys 
      case 5: decodeIR21(code); break; // white 21-key remote  
      case 6: decodeIR6(code); break; // black 6-key learning remote defaults: "CH" controls brightness,
                                           // "VOL +" controls effect, "VOL -" controls colour/palette, "MUTE" 
                                           // sets bright plain white
      case 7: decodeIR9(code); break;
      default: return;
    }
  }
  if (nightlightActive && bri == 0) nightlightActive = false;
  colorUpdated(NOTIFIER_CALL_MODE_BUTTON); //for notifier, IR is considered a button input
  //code <= 0xF70000 also invalid
}

```

And where i can find the NpbWrapper.h File, i can’t find it ?

`IRrecv* irrecv;//change pin in NpbWrapper.h`

---

<div class="post-metadata">

**Author:** ![MeisterAdebar](https://yyz2.discourse-cdn.com/free1/user_avatar/wled.discourse.group/meisteradebar/32/1558_2.png) [@MeisterAdebar](https://wled.discourse.group/u/MeisterAdebar)\
**Post date:** [June 24, 2021, 7:05pm UTC](https://wled.discourse.group/t/implementation-of-the-received-infrared-code-possible/2920/9 "2021-06-24T19:05:17Z")

</div>

I have now solved that with exactly the extension linked above that I made in ir.cpp.

The file NpbWrapper.h probably no longer exists because you can now set the IR pin in the settings and this is set to -1 by default and not defined.

To ensure that the IR codes are also transmitted via MQTT, any FB must be selected under Sync Interfaces / Sync setup / Button setup / Infrared Remote.

Now I am happy 🙂
