How to Control your TV through Alexa and Smartthings

The Unofficial SmartThings Blog
Jump to: navigation, search

IR remote control through Alexa

This project takes what others have done and combines them to create a DIY cost effective Infrared (anything IR) TV controller for Smartthings with Alexa support. It does something along the same lines as can be done with the Harmony hub.

Why DIY?

  • Cost effective
  • Makes use of Smartthings
  • No Port Forward
  • Multi-room / Multi Echo support

Why not?

  • A little complex
  • Involves wires and resistors and such like (the author of this wiki is no expert, but likes to have a go)
  • Might involve trying to work out funny IR codes (work in progress for the author, but Panasonic TV in the kitchen is fully functional in this configuration)

Approach

This solution consist of

  • An ESP8266 NodeMCU WiFi unit with IR LED and IR receiver. This transmits the IR signals, the commands are sent locally from SmartThings via HTTP to the ESP webserver.
  • Ask Alexa and webCoRE, Ask Alexa gets the instructions from Alexa, and through its Macro functionality calls webCoRE which executes a piston, which in turn, sends the local http request to the ESP8266.

The process from Alexa to the TV is along these lines

Alexa to ESP.png


Building the IR Remote transmitter

The Hardware

For the ESP8266 NodeMCU follow mdhiggins excellent writeup and implementation on it. Details can be found here - https://github.com/mdhiggins/ESP8266-HTTP-IR-Blaster.

Breadboard config blu tacked on top of a USB plug, pointing towards the TV.Final version on a PCB.

A couple of things to note when putting the hardware together.

The Software

Library manager and IRremoteESP8266
For the software side follow the instructions in the readme in mdhiggins repository, up to the point of doing any sort of port forwarding on your router and IFTTT. These steps are not needed here as Ask Alexa and webCoRE take care of communicating with the ESP8266.

Please note that this guide makes no attempt to copy or take credit for any of the work others have done

  • To install the ESP8266 Arduino Core, use the Board Managed installation process as described through the links in mdhiggins repository .
  • With the libraries installed install the mdhiggins IRController blueprint (it runs the web service and controls the IR transmitter and receiver) into the Arduino IDE. Blueprint available from the repository.Finally upload to your board.
  • Once the code is compiled and loaded (use the Serial Monitor to setup and check what is happening) and you’ve connected the ESP to your wireless network (following the instructions from mdhiggins) get some codes from your remotes - Open your browser (Serial Monitor will also show codes received) to the ipaddress of the ESP and the web interface will show you what has been sent and received. Hopefully the codes will be a recognised type and you can then send some quick tests back. For example, a Panasonic remote returns the following
Received signal.png

To send the command back use the following simple URL -

http://192.xxx.xxx.5/msg?code=1000809:PANASONIC:48&address=16388

Keep an eye out for remotes that use 'address', and make sure it is included when sending codes back. Panasonic always do it seems. You may need to convert the address received from HEX to DEC when sending the code back, in the example above the address received from the remote was 0x4004, in DEC is 16388.

Panasonic remotes may complain about the RAWBUF being too small, refer here for details and troubleshooting. Try setting the RAWBUF to 255U in the IRrecv.h file (found in the Arduino library folder)

For sending more more complex commands, including RAW commands - where the manufacture isn't recognised, and for sending multiple codes - e.g. channel 103 would be 3 commands - you need to use JSON, as follows

http://192.xxx.xxx.5/json?&plain=[{"type":"panasonic","data":"1000809","length":48,"address":16388},
                                {"type":"panasonic","data":"1000809","length":48,"address":16388},
                                {"type":"panasonic","data":"1000809","length"]]:48,"address":16388}]

Some trial and error may be required in this area to get the codes and format for sending back correct.

  • If you encounter problems with any of the above and you are not sure if the IR LED is emitting, it is useful to point a smartphone camera at the LED when sending commands, it should it light up purple, use your TV remote to check your camera is picking up IR light first.

Integrating with Alexa

Getting Alexa to talk to the ESP involves Ask Alexa and webCoRE. To get then up and running refer to the links below

Make sure you are using the latest versions of the above. This solution uses recently updated functionality with allows for the passing of parameters from the skill all the way to the Piston.

The flow from Alexa to your TV is as follows:

  1. Alexa/Echo sends commands to Ask Alexa via the Skill and the Lambda code (e.g. 'Alexa, Ask the 'Kitchen TV' to change the 'channel' to 'BBC ONE) - note 'Kitchen TV' is the skill invocation name, 'channel' is the macro name, and 'BBC ONE' is the parameter (LIST_OF_WCP).
  2. Ask Alexa executes the defined webCoRE Piston through its Macro functionality and passes the parameter to the Piston (the Lambda code calls the Ask Alexa 'Channel' macro and passes the 'BBC ONE' LIST_OF_WCP xParam parameter)
  3. webCoRE piston sends local HTTP request to the ESP
  4. ESP transmits IR codes to TV.


Configuration

webCoRE Piston

To ensure this all works it is recommended that you work backwards from the webCoRE piston. Included below is an export of the two pistons which deals with taking the request from Ask Alexa and sending the HTTP request to the ESP8266.

  • The first Piston takes the xParam parameter and determines the channel sequence to send. For ease use the code in the image to import into webCoRE.
  • The second piston receives the channel number from the fist, determines the code sequence and then sends the commands to the ESP.

The Pistons are split in this way to ensure there is capacity to add other channels (there are some size limitations per piston).

Channel selection Piston (is called from the Ask Alexa Macro with the xParam)

Make sure you update the case statements within this Piston to match your channel names and numbers. The channel names should match the LIST_OF_WCP entries that you will update later. Note the Piston strips out any special characters and spaces to reduce match requirements. Also, Alexa when hearing any numbers, passes them through as digits. For an example BBC One would be passed through in the xParam as BBC 1, it may also be passed as B.B.C. 1, but the Piston would cut this down to BBC1 for the case statement.

Channel name piston.png

Channel execute Piston (is called from the Channel selection Piston)

Update the variables to match the codes of your TV, the codes in this Piston are for a Panasonic TV. If you configuration does not use the address variable you may need to exclude this from the final section of the piston which creates the HTTP request.

Channel execute piston.png

  • Note that in the sendIRCMD string (which builds the IR command) includes a number of ‘\’ before ‘{‘ and ‘}’, this is due to webCoRE using {}for internal purposes

Test the Piston to make sure it sends the commands. create a temporary Piston which calls the 'Channel name' piston, passing an xParam with a channel name as the argument.

Ask Alexa Macro

With the Piston saved and humming, create a Ask Alexa Macro to link to and execute the Piston.

Extensions.png

  • Open the Ask Alexa SmartApp from the SmartThings mobile app and under Ask Alexa Extensions, select Macros from the Installed extensions section.
  • Select ‘Create a New Macro’ and give it a name. The name here is important as it is what you will ask Alexa. For this example the Macro name is 'Channel' (Alexa, Ask the 'Kitchen TV' to change the 'Channel' to 'BBC ONE'). This needs to be unique.
  • Specify the Macro Type as webCoRE, and select the Piston you just created.

Acck.png

The Advanced features configuration section allows you to manage acknowledgement messages and if you require the additional parameter.

  • If you are specifying a custom acknowledgement you can include '%xParam%' (as written here) within the text and Ask Alexa will add in the additional parameter it received as part of the acknowledgement message.
  • Selecting 'Require Additional Parameter to Run Macro' will stop the macros executing its action if the additional parameter (xParam) is not received from Alexa.
  • If you have specified you require the additional parameter you can also supply a custom acknowledgement message when the parameter is not received from Alexa.

Update Alexa skill

The final step is to tell Alexa about the Macro.

  • Open the Skill in the Amazon developer tool, and under Interaction Model edit the ‘LIST_OF_MACROS’

Alexalistofmacros.png

  • Click Edit (as highlighted above), add in the name of the Macro as you created in the Ask Alexa config You will see from the image a number of Macros exist, the primary macro being Channel in this example. Make sure what you have in this list matches what you have in the Ask Alexa app, refer to the links at the end of this article for full details of populating these slots..

Macrovalues.png

  • Finally edit the LIST_OF_WCP and add in the parameters (in this example the TV channel names). Included below is an example list of channel names and numbers. If you add or change the channel names, make sure you add/edit in the associated case statement in the Channel Piston (as described above).

Alexalistofxparam.png

XParams.png

  • Make sure you click Update on both sections when you have updated, finally scroll down to the bottom of the page and click Save.
   The steps above will allow you to quickly add in the Macro to the Alexa Skill, for full details and the recommended 
   method of keeping the Skill updated with custom slot information please refer to 
   Ask Alexa - Custom Slot Information and Ask Alexa - Setup Variables.


You should now be able to say... ‘Alexa, ask the Kitchen TV to change the channel to BBC one’.

Note, in this example the Skill Invocation Name is 'Kitchen TV'.

If you want to repeat this for another room, simply create another instance of the skill and repeat above, set a different Invocation Name - see Multi Name Control for details about setting up multi room configurations.


Example LIST_OF_WCP

  • In the list below where any items are likely to be interpreted as abbreviations use '.' and 'spaces' to spell it, Alexa seems to matche more in this format than without. Repeat the item in question without the special characters to help ensure a match, e.g. 'BBC ONE' 'B. B. C. ONE'. Spell out numbers rather than using digits.
  • To cover all channel numbers expand your list as required.
BBC ONE
B. B. C. ONE
BBC TWO
B. B. C. TWO
ITV
I. T. V.
ITV ONE
I. T. V. ONE
ITV TWO
I. T. V. TWO
CHANNEL FOUR
CHANNEL FIVE
FIVE
BBC NEWS
B. B. C. NEWS
FILM FOUR
DAVE
SPIKE
MORE FOUR
ITV THREE
I. T. V. THREE
ITV FOUR
I. T. V. FOUR
ONE
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
ELEVEN
TWELVE
THIRTEEN
FOURTEEN
FIFTEEN
SIXTEEN
SEVENTEEN
EIGHTEEN
NINETEEN
TWENTY