To switch your TV on and off with Alexa you have to connect an IR LED to your Photon, write and upload firmware containing a function which can be accessed by Alexa and write a corresponding Alexa skill and Lambda Function.
Get the IR dataYou can look out for the raw signals or use a library (like IRTRANSMITTER by AnalysisIR, see here, or IRREMOTE by Ken Sheriff, see here for details) with the appropriate codes for your remote and a method for sending these to you remote. Look here or here for getting started.
I recorded and analyzed the raw IR signals of my remote control with an IR receiver and a logic analyzer (the inexpensive Scanalogic-2 EDU KIT). Then I compared the measured signals with the data for my remote control provided here to clean my data from measurement errors. Look at my protip here to see how to use a logic analyzer.
This is an image of the recorded sequence:
Here I looked for the matching timings for my Samsung remote:
#define SAMSUNG_BITS 32
#define SAMSUNG_HDR_MARK 5000
#define SAMSUNG_HDR_SPACE 5000
#define SAMSUNG_BIT_MARK 560
#define SAMSUNG_ONE_SPACE 1600
#define SAMSUNG_ZERO_SPACE 560
#define SAMSUNG_RPT_SPACE 2250
MARK means HIGH and SPACE means LOW. I hard coded the data in my code:
int send(String s) {
pulse(5000);
delayMicroseconds(5000);
pulse(560);
delayMicroseconds(1600);
pulse(560);
delayMicroseconds(1600);
pulse(560);
delayMicroseconds(1600);
pulse(560);
delayMicroseconds(560);
....
delayMicroseconds(1600);
pulse(560);
return 1;
}
Alternatively you can use IRREMOTE. If the photon library doesn't support you TV remote you can extend it based on the code of the original Arduino library here.
Change my code to make it suitable for your TV remote control.
Board SetupSet up the breadboard according to the schematic. The schematic describes a simple LED driver, where the base of the transistor is connected to pin A4 of the Photon. This ensures that a sufficient current of approx. 200 mA fires the LED. Place the board near to your TV set and power the Photon.
Attaching the LED direct to the pin didn't work for, presumably due to insufficient current. I still have to check this.
Photon FirmwareThe IR code to be transmitted is a sequence of carefully timed writes to the pin with the IR LED and delays. I used the code which was posted here by tritaris.
The code publishes the function:
int send(String s);
to make it possible to call it with the CLI:
$ particle call 0123456789abcdef01234567 send
or the API:
$ curl https://api.particle.io/v1/devices/0123456789abcdef01234567/send -d access_token=1234
See the attached AWS Lambda Code for Particle Photon for the details of doing HTTP requests to the API from Alexa.
Create a Node.js projectCreate a folder and copy the attached code for the Lambda function (AWS Lambda Code and AWS Lambda Code for Particle Photon) in the folder you created. Name the AWS Lambda Code index.js and the AWS Lambda Code for Particle Photon particle.js.
Initialize the project with:
$ npm init
Thus creating the file package.json and install the modules you need:
$ npm install alexa-sdk request request-promise --save
Thus creating a folder node_modules. Zip the files in the folder - not the folder containing the files.
Translate, modify or extend the languageStrings
accordingly.
Create a Lambda function in a region which supports Alexa. Look at the Getting Started for details. Name the handler index.handler. This ensures that events are routed to index.js.
Choose the Blank Function blueprint and the Alexa Skills Kit trigger. Upload the zip file you created in the Lambda Function. Add the following environment variables with the corresponding values:
- APPID (the app ID of your Alexa skill, see below)
- DEVICEID (the id of your Photon)
- USERNAME (your Particle.io username for accessing the API)
- PASSWORD (your Particle.io password for accessing the API)
After you created the skill (see below) you can test the skill in the ASK UI. You can copy the json from the Service Request box and use it for configuring the test event for the Lambda Function for automatic testing after deploying.
Alexa SkillCreate a skill with the Alexa Skills Kit in the appropriate language. Look at the Getting Started for details.
Paste the intent scheme and the sample utterances in the corresponding boxes in the interaction model section of the skill. Translate, modify or extend the sample utterances accordingly.
Paste the ARN of the Lambda function you created in the box in the Configuration section of the skill. You find the ARN in the UI of the Lambda service.
You can test the skill in the test section. You can copy a json from the service request section and use it to configure a test event for the Lambda function. See the actions button.
Try itNow it should be possible to ask Alexa to turn your TV on:
- Say: "Alexa, start TV remote".
- Alexa should answer: "TV remote is ready."
- Now say: "Switch the TV on."
- Alexa should answer: "I switch the TV on for you."
This should turn your TV on.
Have fun!
Comments
Please log in or sign up to comment.