Alexa, turn off the TV

I’ve been playing with home automation for about a year or two and am pretty happy with my setup. I mostly use Z-Wave devices (a proprietary wireless mesh network) with Vera Lite hub that can be accessed via HTTP (web pages, smartphone apps, etc). I plan to write more about it another time, but since I haven’t updated my blog for 4 years I need to start with something smaller and simpler.

Amazon Echo

I discovered Amazon Echo (a.k.a. “Alexa”) when it was in beta and I immediately preordered one. It really is the missing link in the evolution of smart home, and vice versa – home automation is Echo’s “killer app”. It doesn’t seem as much, but it’s a very good quality bluetooth speaker, fused with the best voice recognition technology I’ve ever used. The voice recognition happens in the cloud (amazon’s servers) and it can be used to check weather, set alarms and timers, check spelling, wikipedia etc. Coming from Europe, I use it often to convert the local units to the standard ones (Alexa, what’s 100 degrees Fahrenheit in Celcius… Alexa, convert 130 pounds to kilos.. ). In any case, I love this device. Despite my Polish accent, it can understand me very well. And unlike most phones or even wearables, I don’t have to “trigger” it with any button – I can yell “alexa” from a ladder, with hands covered in paint, and ask her to turn on more lights – been there, done that – it beats operating a smart phone app for sure.

Again, I plan to write a detailed post of my configuration but today let’s just say that I can use Alexa to send a HTTP GET request to any HTTP endpoint and trigger some actions. While my Z-Wave switches can control many devices, they are not the best candidate for controlling my TV screen – if I use a switch to cut off the TV’s power, I won’t be able to turn it back on using the standard TV remote (and vice versa).

Arduino IR blaster to the rescue!

I needed a way to send a TV remote signal (on/off) using IR (infrared) LED. I played with Arduino before and I knew it was pretty easy, but also I remembered that using an IR library was taking most of the available storage space on Arduino (I use Diecimila with 14KB of available storage) and I could no longer use Ethernet library when I used IR. So this time I ended up controlling the Arduino via USB cable, using an always-on HTPC (home theather PC, or as I call it – “the laptop behind the TV”). Since my setup also involves a Raspberry Pi with Linux, I could probably control the IR LED with that, but in my case that was not convenient (my PC is next to the TV, whereas the RPI is far away and hidden).

First, you need an IR LED 940nm – $1.50, https://www.sparkfun.com/products/9349
and Arduino (I used Diecimila, I think the current UNO model is the most similar) – make sure you can connect it to PC without extra converters – $25, https://www.sparkfun.com/products/11021

Ideally, you should also add a 100 Ohm resistor – I had these 330 Ohm resistors laying around so I used 3 of them in parallel to get ~110 Ohm. I am not expert (far from it) but I was told the LED should use a 100 Ohm resistor, even though you don’t really need it for the short bursts used for IR remote.

arduino-led-3
Note: the LED orientation is important. Pay close attention to the metal elements visible inside the LED, or the length of the legs. Short bursts from arduino should not burn the LED and you should see the light using a digital camera (if you don’t, switch the LED orientation and re-test briefly). Your eyes can’t see IR, but most digital cameras capture it as purple light, just look at the camera’s preview. Check out my clip attached to this post, you should see the faint light.

The IR sample code I found used pin 3 so I also used that one, but any PWM pin will do, and you may find one closer to GND for easier assembly. Just make sure you update the Arduino sketch.

#include <IRremote.h>
IRsend irsend;

//samsung tv power on/off
unsigned int S_pwr[68]={4600,4350,700,1550,650,1550,650,1600,650,450,650,450,650,450,650,450,700,400,700,1550,650,1550,650,1600,650,450,650,450,650,450,700,450,650,450,650,450,650,1550,700,450,650,450,650,450,650,450,650,450,700,400,650,1600,650,450,650,1550,650,1600,650,1550,650,1550,700,1550,650,1550,650};

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

void loop() {
if (Serial.read() != -1) {
irsend.sendRaw(S_pwr,68,38);
}
}

(copy at https://gist.github.com/Ekus/92842e60e8a3f53f38d3 )

the Samsung On/Off code (for S_pwr variable) I googled on github. The arduino IRRemote library contains codes for some other brands built in. If you don’t find your code, you can always “record it” with an IR photoresistor attached to Arduino, but that’s another project.

After assembly, my Arduino looked like this:

WP_20160112_009

I wrapped the LED cable around the Arduino to avoid disconnecting the pins when moving the cable, and wrapped everything in some antistatic foil I had lying around. I also added some hook & loop (a.k.a. velcro) sticker to attach the LED to the furniture and keep it pointed in the general direction of my TV.

WP_20160112_002

Once I had my Arduino ready and working, I had to control it from my PC.

I’m using Windows 10 so I went to the control panel to “install Windows components” and installed IIS with ASP.NET. I then made this ASP.NET file and put it in the root folder, or in a new application folder:

<%@ Page Language="C#" AutoEventWireup="true" %>
<%@Import Namespace="System.IO.Ports" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
using (SerialPort sp = new System.IO.Ports.SerialPort())
{
sp.PortName = "COM3";
sp.BaudRate = 9600;
sp.Open();
sp.Write("1");
System.Threading.Thread.Sleep(100);
sp.Write("1"); // repeat, just in case. That's not the best way to do it but I didn't have time to investigate Samsung IR codes more. It's good enough.
sp.Close();
}
}
</script>

<html>
<body>
<h1>HI</h1>
<%=System.DateTime.Now %>
</body>
</html>

(copy at https://gist.github.com/Ekus/4f86aba04487bdb452a4 )

Now, whenever I (or my Alexa->home automation bridge) open the page http://<ip of my htpc>/EkusTV.aspx, the page executes and causes the arduino to send the on/off signal. In my next blog post I plan to describe this missing link – how I got my Amazon Echo to send the http request to my devices. In the meantime, enjoy the clip below!

Advertisement