[OUTDATED] Having Amazon Alexa, via an Amazon Echo, control a Google Chromecast via a Raspberry Pi running a Python script.
Doing the Impossible: Google Chromecast and Amazon Alexa
Ever since the release of the Amazon Echo, people have been asking for One thing.
— This is outdated, please view new instructions on Github —
Is there any way I can control Chromecast with Echo?
asked reddit user MichaelFR85
suggested reddit user phasefx
But now, wait no longer!
Chromecast and Alexa now work together in a cross-platform attempt to control Chromecasts using a Raspberry Pi and some python. Talked about on “AlexaDev Tuesday: Innovative Alexa Skills Spotlight” from http://www.lovemyecho.com and revered as
[one of] the most innovative Alexa skills being created by Alexa developers. There’s a lot to be learned from the way these developers have solved coding and technical challenges to build something extraordinary.
Using Raspbian, Python, and Chromecast with Echo Support, let us begin our journey through joining this forbidden love.
Why Raspberry Pi?
The simple answer is “not only Raspberry Pi”. You can run this script on any capable hardware, and the results will be the same. However, I chose the Raspberry Pi due to it’s compatibility, as well as it’s size, and portability.
How it’s Made:
All of this was written in Python. However, I used the module to assist me in connecting with the Chromecasts, and the module to assist in connecting to Databases. I used the “Alexa Color Expert” Example as a basis for the skill, writing from scratch all the functions. The rest is from scratch.
How it works:
- User starts session by asking Alexa “Launch/Ask/Run Chromecast”
- User Tells Alexa Command (default response is to search YouTube)
- Request is Parsed, then sent to MySQL Database hosted on Raspberry Pi
- Python Script (also hosted on Rasp. Pi), then reads database, and runs corresponding command
- Row is deleted after command is complete
Step 1: Setting up your Raspberry Pi for Port Forwarding
Before we begin:
- Make sure your Raspberry Pi is on and connected to the internet
- Find your Raspberry Pi’s IP Address
On your Raspberry Pi or another PC:
- Launch your preferred terminal (cmd or Terminal)
- Find your default gateway using ifconfig (Linux) or ipconfig (Windows)
Visit your Default Gateway to access your router.
For more information for your port forwarding your specific router, look here: http://portforward.com/
In this case, we’re going to be port forwarding both Ports 3306 (The Default Port for MySQL), Port 80 (For PHPMyAdmin), and Port 22 (For SSH)
PLEASE NOTE: This opens your network and Raspberry Pi to be accessed by other people, and does cause risks to your information.
Step 2: Setting up mysql
sudo apt-get install mysql-server –fix-missing
Step 3: Setting up PhpMyAdmin
Use the included build.sql to create the required Database.
NOTE: Make sure your user is accessible from other locations, you can view how to do that here.
Step 4: Editing your Files
To get started, let’s define a few terms we’ll be replacing:
- RASP_PI_DNS – Refers to the MySQL Server URL we’ll be using.
- MYSQL_USER – Refers to your MySQL Username
- MYSQL_PASS – Refers to your MySQL Password
- DB_NAME – Refers to your MySQL Database Name
- CHROMECAST_NAME – The name of your Chromecast (Or the chromecast you want to connect to)
Replace all occurrences of these strings in both index.py and raspberry.py with their corresponding values.
Step 5: Uploading to Lambda
Zip up all the file and folders EXCEPT speechAssets, and raspberry.py, then log into your AWS Dashboard.
In Lambda, click “Create a new Lambda Function”.
Click “Skip” for blueprint selection.
For your function name, type “alexaChromecast”, for runtime, make sure “Python 2.7” is selected.
For Code entry type, select “Upload a Zip File”.
Click the Upload Button, and upload your Zip File.
Set Your handler to index.lambda_handler, your Role To Basic Execution, and your timeout to 30 seconds.
Step 6: Setting up the skill in the Amazon Developer Console
Sign in to the Amazon Developer Console at developer.amazon.com
Click on the “Alexa” Tab.
Click on the “Get Started” for the Alexa Skills Kit.
Click the “Add a new Skill Button” to add a new skill.
Leave the Skill Type as is. For the Name of the App, type “Alexa Chromecast”, and for the Invocation Name Type “Chromecast”
Click the Next Button in the lower right hand corner of the page.
For the Intent Schema, use the intentSchema.json located in the speechAssets Folder of the repository, and paste it’s contents. Do the same for the sample utterances using sample-utterances.txt.
Click the Next Button.
In the “End Point” Box, paste your “Lambda ARN”, The Lambda ARN is located on the function page in the upper right hand corner.
Select “No” for account linking.
Step 7: Setting up raspberry.py
Move raspberry.py onto your Raspberry Pi by copying the file to a flash drive, or copying it via WinSCP available here.
Step 8: (OPTIONAL) Setting up Cron Job
Create a bash file that will run raspberry.py, and save it as “launcher.sh”.
Put the following code into launcher.sh, replacing it with the location of raspberry.py
sudo python3 /location/to/raspberry.py
To find out how to set up the cronjob to run launcher.sh on boot, You can follow the instructions located here.
Now, use the Test Tab to try out your new skill! If you did everything correctly, you should be able to control your Chromecast using Amazon Alexa!
VUI Interface Diagram:
Read More Detail : Amazon Alexa Controlling a Chromecast
Current Project / Post can also be found using:
- alexa control google chromecast