So in this project, you will learn to make an Alexa IoT TV controller that will solve the above problem. The entire system works like this:
- Alexa will connect to an IoT device over WiFi
- The IoT device will receive commands from Alexa to trigger the IR LED for controlling the Set Top Box or TV.
The Alexa IoT TV controller is not just restricted to controlling TVs. You can use it to control any other device that uses an IR remote control.
Sounds interesting!!? Gather your tools and let’s build.
Bill Of Materials
To install the ESP8266 board in Arduino IDE, go to Files → Preferences → Paste the URL. After that, go to Tools → Board Manager → Search esp8266 and install the board in Arduino IDE.
Also, install the library esplexa in Arduino IDE as that will allow Alexa to control the IoT appliances that have been added to the Alexa app. Next, install the IR remote library that will allow you to decode the hex code for transmission to the TV.
Here we need to write two codes: one for ESP8266-01 that will be used as an IoT device for Alexa and the other for Arduino that will trigger the IR LED to control the TV. For controlling the TV using Alexa, create the code using espalexa. It also allows the addition of lighting devices to Alexa. However, do remember to set the function for passing the control commands to Arduino for controlling the TV instead of lights. So let’s write the code.
First, initialise the espalexa library and then add the required device(s) in the code so that Alexa receives the function of individual buttons of the TV remote.
Note: Since espalexa cannot be made to work as a remote device, use the following hack instead. Create a function for light, which will trigger the IR LED of the TV and enable you to control it like an IR remote. For example, for the ON button, create a function named as light 1 in the code. Continue adding 13 to 14 separate functions, that is, light 1 upto light 13-14 for performing other operations.
In the setup function, set a serial baudrate of either 9600 or 115200 so that commands can be successfully passed to Arduino.
Next, create a loop function for setting the function of devices such as light 1, light 2 etc. Whenever Alexa will be given a command, it will trigger the functions light 1, light 2 etc, associated with a specific connected device, that is, TV remote. These functions correspond to commands like volume up/down, TV ON/OFF, next channel, favourite channel, etc.
Write the code for Arduino to retrieve commands from espalexa and then trigger the IR LED to perform as a TV remote control.
Include the Infrared library and paste the remote button array in the code (see figure below). Then create a setup function to set the serial port using Serial.begin (baud rate). Enter a specific baud rate (preferably 115200).
Next, create a loop function for checking the commands coming from the serial port. After that, create a nested if condition to check the received commands and define the functions. For instance, if the command is 1 and you have to watch the Discovery channel, then set the function for this command to open that particular channel. Then send the channel number of Discovery from IR LED using IR send. That is, if the Discovery channel in your set top box is 102, then send the code for button 1, then 0 and then 2 with a delay of 200 microseconds.
Adding device to Alexa
To add an IoT device to Alexa, open the Alexa app and go to Option → Add Device → Select the device to be added and then let Alexa search the new device in the network. After the search is completed, a list of devices to be connected are shown, for example light 1, light 2, light 3….. . Select them one by one and add them to Alexa. Now it is ready to control your IoT device(s).
To set the custom voice commands for channels and remote functions to operate routinely, open the Alexa app and go to Settings → Routines → Add new routine. Now set the voice command for routine like “Alexa, Star Plus lagao“ (“Alexa, select Star Plus”) or “Alexa, volume up karo” (“Alexa, increase the volume”) etc. Then set the action to be performed for that command. Here, light 1, light 2 etc, will get toggled according to the button function and command.
Power the device and ask Alexa (as per the preset command) “Alexa, volume up kar do” (“Alexa, raise the volume up”) or “Alexa, next channel laga do” (“Alexa, switch to the next channel”). On hearing the command, Alexa will transfer the action to esp8266, which will further send the command to Arduino and trigger the LED to control the TV as requested.
Congrats!! You can now control any TV with Alexa. Enjoy your smart home experience.