In a smart home electronic appliances can be controlled through voice or gesture, including a TV set. Unfortunately, this feature is not available in most TVs. To use such a feature, a Fire TV stick is needed, but it gives only limited TV access.
So, here is a project to make an Alexa IoT TV controller that will solve the problem. The system works by connecting Alexa to an IoT device over Wi-Fi. The IoT device receives commands from Alexa to trigger the IR LED for controlling the set-top box or TV.
Fig. 1: Author’s prototype
The Alexa IoT TV controller is not just restricted to controlling TVs. You can use it to control any device that uses an IR remote control. Sounds interesting? Gather your tools and let us get started.
To install the ESP8266 board in Arduino IDE, go to Files→Preferences→Paste the URL. Then go to Tools→Board Managers→Search esp8266 and install the board in Arduino IDE.
Also, install the library espalexa in Arduino IDE, which 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.
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 library. 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.
Fig. 2: Code for ESP8266
Fig. 3: Code setup function
Fig. 4: ESP code consisting of commands to send from serial to Arduino
Fig. 5: Arduino code
Fig. 6: IR remote code
Fig. 7: Arduino code with IR PWM code to send from IR LED
Fig. 8: Defining variables to store the IR code array
Fig. 9: Arduino setup function
Fig. 10: Loop function for Arduino
Fig. 11: Code sending IR signal for respective buttons of remote
For writing the code, first initialise 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, say, Light 1 up to Light 13 (as per requirement) for performing other operations.
In the setup function, set a serial baud rate of either 9600 or 115200 so that commands can be successfully passed on to Arduino.
Create a loop function for setting the function of devices such as Light 1, Light 2, etc.
Whenever Alexa is given a command, it will trigger 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.
Arduino code. 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, 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 between them.
Fig. 12: Connection diagram
Fig. 13: Alexa app UI
Fig. 14: Alexa app adding a new device
Fig. 15: Alexa app options to add device
Fig. 16: Alexa searching Wi-Fi device to add on same network
Fig. 17: List device added
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 is shown such as Light 1, Light 2, Light 3, etc. 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 your TV with Alexa and enjoy your smart home experience.
Download Source Code
Ashwini Kumar Sinha is technology enthusiast at EFY