[RELEASE] Infrared IR Device Integration via ST Anything v1.0

arduino
esp8266

(Kris) #1

Hello All,

I would like to share a new library I have created to integrate an IR controlled device with SmartThings through the ST Anything project.

This project uses a NodeMCU together with an IR transmitter to control devices such as televisions and stereos etc.

The interface is implemented as a an On/Off toggle switch, the typical use case would be to switch on and off a TV as part of a security system, or to turn on your music in the morning to wake you. Of course this button can go out of sync if you also use the physical IR remote to turn the device on and off.

Please see the Github readme file for details of how to use the library:

This project builds on the ST Anything project, and connects to SmartThings via WiFi:

It also relies on the IRremoteESP8266 library that can be added through the Arduino IDE “Manage Libraries” interface.

To use the library you need to find the correct code to send to the device, this can be found using an IR receiver connected to an Arduino, I used an Arduino Uno. More details are provided in the Github Readme file.

The parts I used are shown below (no affiliation):

NodeMCU ESP8266 based microcontroller:

IR Transmitter LEDs:

IR Receiver Module (maybe easier to use):

Using the circuit described in the Readme file I can control my LG TV from across the room, my Samsung TV seems less sensitive as it only works from around 2m (6 ft).

For the next steps I am planning to build a library for a momentary switch that can represent other types of remote control buttons, such as the volume control, that don’t have an on/off state. I am also planning an IR macro that can send a sequence of IR codes, a use case for this could be to turn on the TV, set the channel and adjust the volume with one button in the SmartThings app.

Let me know how you get on and if you have any questions/requests!

Regards,
Kris.


(Dan) #2

Kris,

I just took a look at your code and it looks great! Thank you for following the Architecture! This is exactly the what my son and I hoped for when we created ST_Anything - users adding their own work to the project so everyone can use it.

Once you’re at a point that you’re comfortable with your additions, please feel free to submit a Pull Request to the ST_Anything GithHub repo as we’d be happy to include this additional functionality.

Thanks!
Dan


(Kris) #3

Thanks Dan, I’m pretty new to Github so I will see if I can figure out how to do it :slight_smile:

I have started modifying the TimedRelay device to make the momentary switch device, this will be in a separate library file. The idea is to only send an IR code when the button is pressed, not when it switches off again after a short period. This way the ST button would work like a physical remote, for example you could move through channels by pressing the Channel Up button repeatedly.

Hopefully that sounds reasonable to you, any insight you have would be useful.

Just for interest, I have created one more ST Anything library to integrate some 433 MHz switches. As far as I know it only works for Nexa branded plugs and built in switches (sold in the UK by Clas Ohlson), so it is a bit more niche:

This is also based on the EX_Switch, but the RF uses a different code for On and Off so it is easier to stay in sync.
Thanks,
Kris.


#4

I was just going to buy a Harmony hub to do this exact thing. And I am a HUGE fan of ST_Anything. Looks like I’ll be buying a couple more boards and some IRs. :slight_smile: Thanks Kris!!!

One question though. Will this use the Child Switch DTH or does one have to build a new one?


(Kris) #5

Hello Ryan,
I shows up as a normal switch child device, you can see my screen shot below:

One thing to mention, the IR project I used can support most things but apparently the codes for air conditioning are more complicated so you may have trouble if that is what you want to control.

Let me know how you get on, if you get stuck I’ll try to help.
Thanks,
Kris.


(Kris) #6

Hello All,
I have now added a Sketch to my Github to read IR codes on the NodeMCU with a connected IR receiver (see the Amazon links in the first post).

When you use this sketch it will report the IR code and other info you need over the serial connection so you can read it on your PC. You can then put these codes into the ST Anything sketch to control your devices.
Probably there will be some codes that this does not recognise yet, let me know if you have any trouble.
Thanks,
Kris.


#7

Just got my IR LEDs and Receiver in the mail and within 10 minutes had it wired up to give it a shot with the Arduino sample sketch. AND…drum roll please…IT WORKED THE FIRST TIME!!! I have to say, I almost passed out I was so surprised. Something DIY working the first time i wired the damn thing together? UNHEARD OF! Thank you for this!! I was looking to buy a couple harmony hubs but the setup seemed so complicated with all the other stuff to get them to work. This is SOOO much easier. I anticipate by the end of the weekend I’ll have one wired up for each room of the house that has a TV. Great work!! :+1:


(Kris) #8

Excellent, good to know it is working for you. I wasn’t sure if I had missed any steps in the write up.
If you don’t mind I’ll ask you a couple of quick questions:

  1. Was there anything that could do with more information in the readme file or in the sketch?
  2. Did you use the transistor circuit or just connect the IR LED to the NodeMCU pins? How is the range?

I’m thinking of adding a list of known IR codes to the GitHub page, feel free to send yours over of your like.

Now I just need to find some sort of box to put the NodeMCU in so I don’t have a bunch of circuit boards hanging around the house!
Kris.


#9
  1. You could clean up the header of the .h and the .cpp files so they match. They have conflicting info on what has to be in the device definition in the sketch. You might also want to include instructions or at least list the libraries that have to be added to the Arduino IDE in order to compile the sketch. Since you probably added them so long ago, you might have forgotten that part. I think the full listing is:
    IRremoteESP8266
    IRremoteControl
    IRremote
  2. I did both and wiring it directly did not work as well. I was only able to get it to work a couple of feet from the device using the direct wiring but using the transistor I was able to have the device across the room and it would work. So, if you’re board’s final location will be very close to the controlled device, then direct wiring might work. But I will definitely be using a transistor on all of mine, just in case I want to move it.

As far as a case, which board are you using? If i end up using and ESP01 or D1 mini, I was planning on using a box like this:

They’re pretty sturdy but still mod-able with a dremel. I’ll post pics of my final product when I get it complete. I’m still trying to figure out placement that will be least intrusive. I’m thinking that in my bedroom I might mount it on the cowl or my ceiling fan and run the power up through the ceiling. That way I won’t have ugly cords running up my walls. In my living room, I’m thinking of affixing it to the end of my coffee table. I’ve wanted to have wireless charging station on it for a while so I might finally bite the bullet and run power out to it under the carpet to hide the cords.

As far as listing known IR codes, I can pass mine along but I would still recommend that anyone working on this project also build the receiver. When debugging it was invaluable. I ran into a bunch of trouble once I moved to building the ST_Anything sketch. Well, it turns out the problem was with the board communicating with my router, not the IR part of the project. I used the receiver to confirm the correct code to make sure I had that right not just to discover the codes to use.

I had an idea as far as the other functions go as well. Is it possible to modify the library to only send the code when the switch is turned on? If so, you could then use the “Momentary Button Tile” type device send the code. It’s a device which acts like a switch in that it turns on but then it shuts itself back off again through code so it can be pressed again. If you really wanted to get fancy you could then work that into a DTH like the simulated MiniMote. But I wouldn’t try to go too crazy, simply because if someone wants that kind of control, they should just break down and by a Harmony remote control. I was thinking you’d only need something like volume up/down and maybe mode/input.


#10

I may have spoken too soon. I was using a 470 ohm resistor because that’s what’s in the pic that you linked to. However, I went back and recalculated based on the voltage of my LED (1.3) the voltage of the nodemcu Digital out (3.3) and the peak current draw (12mA). And using the calculation to determine resistance:
(3.3-1.3)/12
I get 166 ohms. So, the next closest is 220. When I swapped out the resistor I got a great signal from across the room without the transistor. So, I’m going to give this a shot in my first build.


(Kris) #11

OK thanks, I’ll update the current files with your suggestions.
I’m already working on a momentary switch based on the timed relay example in ST Anything, the idea being that it will send the IR code when you press the button then it will switch itself off after a short time, maybe 1/2 a second, without sending the code again. I’ll post an update as soon as it is ready to test.
For the LED circuit, I saw that the max safe output current for the ESP12E (as I am using) is 12ma, so you could use this to directly drive the LED as you describe, but the distance shouldn’t be as good as the transistor circuit. The IRremote project suggests to use this circuit which effectively has no current limiting resistor on the LED. This will put too much current through the LED but because it is only on for a few microseconds at a time it doesn’t damage it and you should get much better range. I am no expert but according to my calculations a 1K resistor between the ESP and the base of the PN2222 I am using should provide enough power to fully switch on the transistor and also protects the ESP output pin from being damaged, it works across the room for me. I found a really in depth description of this setup and how to build high power transmitters in a readme for one of the IR projects, I’ll post a link when I find out again.
Lastly, at the moment I only have some NodeMCUs set up, I’ve got a couple of ESP01’s but I haven’t got around to trying them yet. If this code works on the ESP01 it will be much easier to find a small box, I’ll have to give it a try :grinning:
Cheers,
Kris


#12

It does work on the ESP01. But I would recommend using the TX pin (pin 3). You just have to put the following into your setup and loop:

pinMode (3,OUTPUT);

I dunno if it’s necessary, but I’ve always set my debugs to “false” also when using Pin 3. You can’t use 0 , 1 or 2 because if you do it screw up the boot sequence.


(Kris) #13

Hello,
I have added a new library to support a momentary switch that doesn’t need to remember if it is on or off, for example a volume up button.
Using the S_TimeRelayIR libraries you can implement as many buttons as you like to replicate an IR remote control. As with the previous library you need to find the correct codes to send.
I have updated the example sketch to add some of these buttons, in this case they are to control my LG TV.

Please let me know how you get on and if anything isn’t clear.
Thanks,
Kris.


#14

What’s the debounce on the button? How fast can it be pressed via software to do multiple actions? For example, if you wanted to hit the input button 3 times, what type of delay do you need between actions?


(Kris) #15

Hello,
Currently each button returns to “off” after 0.5 seconds, so if you want to push the same button repeatedly I guess it should not be faster than this. You can change the delay in the constructor, but it defaults to 500ms if you don’t set it (as in the example sketch).
I haven’t tried pressing different buttons one after the other quickly, I’m not sure what would happen, perhaps they will be queued by ST?
Feel free to experiment and let me know if you find anything, I’ll try a few things as well.
Kris.


#16

I’m not sure that they would be qued by ST since they are different devices. For example, I have my TV and stereo on the same board. I haven’t gotten my case finished yet so I have only tried it manually and not with a routine or WebCore piston yet. So, I’m not sure if I can turn them on simultaneously or if I do, will it get confused and try to send the IR codes at the same time. I still have to some experimenting with it. But I asked specifically because in some cases, the only way to do certain functions is to press the same button multiple times. So, I wanted to check to see if there was a known limitation. But this is wonderful. I already have a board set up for my bedroom and living room. Just gotta get the dremel out to modify my case for the LED.

Oh, and btw, your “analyzer” (code grabber) worked better than the one on the Adafruit website. The adafruit one you linked to couldn’t find the code for my Sharp Television but yours found it with no problem. :slight_smile: :+1:


#17

So, I got around to finally getting the final mounting done for my two devices and I notices something very strange. When the board is first powered on, it send one command for each switch since it is setting them to “off”. I found this because I was trying to power my board from my Raspberry Pi. And my soundbar would come on and then go off again but show as On in ST. This is because when the board first powered on, it was sending the IR signal to tell the device to turn off, even though it already was.

This is kind of troubling. If I lose power, I don’t really want all my TVs to turn on when the power is restored, which seems like is guaranteed to happen. I’ve invested a bunch of time and effort into getting these working and I would hate to scrap em now. Any assistance you could provide would be a big help. I don’t know if the button/timed relay device works any different or if it’s the same. For now, I’m going to unplug my devices and rely on my remotes.


(Kris) #18

HI Ryan,
I noticed this myself a couple of days ago and already changed it. The idea was to make sure the IR LED wasn’t left turned on when the power is connected, but I have changed the way this is done.
I just updated Github, if you just want to change this part you can replace the last part of EX_SwitchIR.cpp as below:

Original code:

void EX_SwitchIR::setPin(byte pin)
  {
    m_nPin=pin;
    pinMode(m_nPin, OUTPUT);
    writeStateToPin();
  }

New code:

void EX_SwitchIR::setPin(byte pin)
  {
    m_nPin=pin;
    pinMode(m_nPin, OUTPUT);
    digitalWrite(m_nPin, LOW);
  }

The new timed switch works correctly, it doesn’t send any codes on power up.
Cheers.
Please do continue to let me know any issues. I am going to see if I can figure out what happens when buttons are pressed quickly as soon as I get a little time.

Kris.


#19

So, let me make sure I understand you…the fix you posted fixes the off on powerup? Or do you recommend going to the button instead?


(Kris) #20

Hello Ryan,
Yes, with the update I posted above it now means that the ESP won’t send any IR codes when the power is turned on.
Both the on/off buttons and the timed buttons work in the same way, they only send codes when you click the button though SmartThings.
It sounds like you are further along than me setting this up, I still didn’t get around to trying my ESP01.
Hope it helps,
Kris