Building Alexa universal IR remote, to control TV or AC with ESP8266 (Wemos D1 mini)

(I have bolded the important details if you want to do this project and I added images, cause I find it to be helpful)

This is my first main project after getting started with Arduino, which is fairly easy in theory, but allowed me to learn a lot as a beginner. But essentially, I record IR codes with an IR receiver and program it to send those signals with an Alexa command to turn on the TV for example.

Getting Wemos D1 mini working

Wemos D1 mini

I choose wemos D1 mini, because I looking for esp-01 alternative (because I had problems getting it to work) and found this, which has a micro USB with 5v input, so I can easily power it with a phone charger. And it worked without any problems for me!

When you get it out of the box, it doesn’t have headers, so I had to solder it. Once done, you can just connect it directly to your computer.

First go to the wemos D1 mini website and download and install the CH340 Driver. (or click this link).

Once connected, you have to open the Arduino IDE, go to preferences and paste these urls in Addition boards manager URLS

https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json

Then you have to go to Tools > Board > Board Manager

Then you search for esp8266 and install esp8266 by ESP8266 Community. Then go to Tools > Board > ESP8266 Boards (version) > WEMOS D1 R2 & mini

Connect the D1 mini with micro USB cable and run the blink sketch.

Recording IR codes with IR Receiver

Setup the wemos as shown above keep in mind the input pin D2. Then install the IRremoteESP8266 library.

You can use a regular Arduino Uno with the normal IRremote library, however, IRremoteESP8266 for me, was able to capture more IR codes with better accuracy.

Then connect the wemos and run the IRrecvDumpV3 example from the IRremoteESP8266 library. Then change the kRecvPin variable to D2. After opening the Serial monitor at baud rate 115200, press the button on the remote, which you want to use (lets say the TV power) and it should dump the IR code in hex, also keep in mind the protocol (NEC, Samsung, etc.)

Setting up the IR Blaster

Next I set up the IR led

I used a 10 ohm resistor, which I don’t know is correct, but with this value I was able to emit a strong enough signal and the led did not get damaged.

Setting up Sinric

Sinric allows us to setup our ESP8266 as a smart home device in Alexa. First you have to register for a Sinric account. When making my account I wasn’t able to find a way to register on the website, so I just used this URL.

www.sinric.com/register

First I want make sure everything is working by using a test switch. I used my fan power on and off IR code, but I found that using TV mute/unmute works really well too. Now login and click on Add in Smart Home Device Box, give the device a name and select switch for device type. Now note down the Device ID in Your devices and the Your API key.

Next go to the Alexa app and add the Sinric skill make sure to discover device and your new switch should show up in Devices, but does nothing yet.

Go to the Sinric github, where you can find the switch_example and various others, in arduino examples. the Install the WebSocketsClient and ArduioJson from the github links in the code. Now you have to fill out the code, with Your API key, your wifi SSID and password, and add your Device ID in the turnOn and turnOff functions.

You have to now add the following libraries to the code

#include <IRremoteESP8266.h>
#include <IRsend.h>

Add the pin for the IR led and the IR code.


int kIrLed = D2; // led Pin
IRsend irsend(kIrLed); // setup IR send
int testSwitch=0xD81; // IR CODE (CHANGE TO YOUR YOUR OWN)

Add the following line to the turnOn and turnOff functions under the if statement. You have to change the protocol, to the one your IR code uses or the code will not work.

irsend.sendSymphony(testSwitch); // Change protocol to your IR code type

Finally, don’t forget to add the following to the setup function.

irsend.begin();

Now upload the code to your Wemos and inspect the Serial monitor, because the wemos is going to take a few seconds to connect to the wifi. Once its ready point the IR led to the device you want to control and tell Alexa to turn the device on or off.

Code for controlling TV and AC explained

You can find the code I used to control my living room TV and AC here. This code connects the wemos to wifi and allows me to control my TV power on/off, adjust volume, play, pause and even a custom (skip ad command). It also controls the AC power, adjust temperature and set modes.

The code is simply a modification of the Sinric TV example code. I added two devices to Sinric for TV and AC. (Also the device takes a few minutes to show up in the Alexa app, so I disabled and enabled the skill again and that adds the device quickly). I added various functions for each command (power, play, etc.), each function checks the device ID, with an if statement. The volume and temperature adjust functions use the same adjustIfElse and adjustLoop functions run send the IR signal multiple times. The argument for the adjustIfElse named extra is very important. I noticed that when changing the TV volume, the given change in volume and the volume that is changed in the TV is off by 2 points, so you can the extra argument to calibrate this. The AC temperature adjustment doesn’t have this problem.

I noticed that when I give Alexa commands “Alexa, skip” or “Alexa, skip the ad”, the wemos received a NEXT action. So I used this to skip YouTube ads, on my Apple TV. I also added DEFAULT_VOL variable at the top of the code, which you can change to the value you want Alexa to change the volume by, if you don’t specify a value.

Final Design

Before you solder the components, make sure you check where you want to place the wemos, so your devices can receive the IR signal. (Tip: use a small power bank (5V output) to power the wemos and find a location). Once you find your location, you can solder it all together, keeping in mind the direction the IR led should be facing and the direction the mirco usb port of the wemos should be facing. I soldered in two configurations shown below.

I put the first one in enclosure made of cardboard and tape, and placed it in my living room.