LED Strip Controlled by Amazon Echo
by AdamF109 in Circuits > Computers
22933 Views, 31 Favorites, 0 Comments
LED Strip Controlled by Amazon Echo
Hello! This is a guide on how to control an RBG LED Strip from your Amazon Echo. You will be able to tell the Echo to turn on and off the LED strip along with changing its brightness and fading between colors. However, there is also more you can do if you customize this further.
Here is a video of it working:
What You Will Need
Amazon Echo
https://www.amazon.com/Amazon-Echo-Bluetooth-Speak...
Raspberry Pi B+ with breadboard kit (Any raspberry pi will work just make sure you have a Micro SD card, power supply, and a way to get internet connected to it as well)
https://www.amazon.com/Raspberry-Pi-Model-512MB-Co...
RGB LED Strip
https://www.amazon.com/gp/product/B013OQX3JG/ref=o...
Three MOSFETS (max threshold voltage of 3.3v)
https://www.amazon.com/gp/product/B00AKTLO4M/ref=o...
Power Supply (12V DC ~2A)
https://www.amazon.com/gp/product/B00PJZQDDO/ref=o...
Power jack
https://www.amazon.com/gp/product/B00VERUREC/ref=o...
Breadboard
Jumper wires (Male to male) and (Male to Female)
https://www.amazon.com/gp/product/B017C54VZA/ref=o...
You probably could find some of these laying around instead of buying the whole kit above but either way it’s a good way to get into electronics with the Pi.
Setting Up the RGB LED Strip With the Raspberry Pi
First we will start with the setting up the RGBLED Strip with the Pi. This has been done before so you can follow this guide by Popoklopsi:
http://popoklopsi.github.io/RaspberryPi-LedStrip/#...
He explains it perfectly! The only difference however is that my Red wire from the RGB Strip is connected to pin 17 on my Pi’s GPIO, blue is connected to pin 24, and green is connected to 27. Once you can successfully change between the Red, Green, Blue or any other mix of colors from the command “pigs p 17 255” etc. you are ready for the next part.
Setting Up Nginx
So we now can change the color of the light strip from the command line of the Raspberry Pi. This is pretty awesome! But we want to allow us to change the colors from the Amazon Echo.
To do so first let’s set up a web server for the Echo to communicate with on the Pi. Follow this official guide to set up nginx on your Raspberry Pi.
https://www.raspberrypi.org/documentation/remote-a...
Make sure to also install PHP with it!
You now should be able to type in your Pi’s IP into your URL and connect to its website from any computer in your home. It will have the nginx default screen displayed or the default PHP page.
Setting Up a Static IP
We need make sure that this IP stays the same so we will make your Raspberry Pi have a static IP. Here’s a simple guide to do it.
https://www.modmypi.com/blog/tutorial-how-to-give-...
You may have to go searching else if this is outdated, also it might be slightly different if you are setting up one with a wired connection vs wireless.
Great! Now your Pi is hosting a web server with a static IP so you always know where it is hosting.
Setting Up the Pi to Work With the Amazon Echo
Next we will write a script in python to check the contents of text file on the Pi. This script we will call controller.py. The text file will be called settings.txt. The controller.py script will run a loop to constantly check the contents of the settings.txt file. Based on the contents of the settings.txt file the controller.py script will change the Raspberry Pi’s GPIO pins(17, 24, 27) to either be on or off from our pigpio library installed on the Pi. And by doing this the LED will change as you saw as you did before from the pigs p command.
The settings.txt file will have 5 numbers, the first 3 will determine the brightness of the lights, anything from 0 to 255. The forth number will be 0 or 1 to indicate whether or not the fade effect will be active, in order to fade between different colors (0 for off, 1 for on). The last number will indicate the color 0-9, and the controller.py script will change the GPIO pins to show a certain color from the LED strip based off the number you assign it.
The controller.py script is running a loop to constantly check the values of the settings.txt file. It gets the 5 numbers from the file and parses them to read the information correctly. It then uses the pigpio library to run the pigs p command based on the numbers from the file in order to change the LED colors.
One last thing in order for the controller.py script to get the contents of the settings.txt file it must send a request to a php file. We will call this getColors.php and this file will simply grab the contents of settings.txt for us.
The code for these three files can be found in my git hub repository located here:
https://github.com/adza45/echo/tree/master/Raspber...
When you download these files place in the same location where nginx is hosting its web server. Most like will be here:
cd /var/www/html
Completing the RGB LED Strip With the Pi
This current configuration has the lights changing to full brightness for the color green with the fade effect turned off. You can see this because the first three numbers from the settings.txt file is 255 which is max brightness. The forth number is for the fade and it is zero so the fade settings is off. And the last number is 1, where as you can see in the controller.py file if this number is 1 it sets pin 27 high for green.
Once the files are in that directory we can test this part of the application. Run the controller.py python script by typing:
python controller.py
The lights should turn to green if 25501 is the current contents of the settings.txt file. Open a second terminal and navigate to the settings.txt file:
cd /var/www/html
Open the settings.txt file:
sudo nano settings.txt
Play around with the numbers in the file and make sure when you save and exit the LED Strip changes colors accordingly. Remember the first the numbers are the intensity, the forth is if you want to turn fading on, and the last is the number of the corresponding color.
Sweet! We now can change the color the LED’s based of the contents of the settings.txt file. Cool but we still haven’t even touch the Echo yet, but we are almost there!
So we somehow need the Echo to be able to communicate to the web server that the Pi is hosting to change the contents of the settings.txt file that will change the color of the LED Strip. We can make this happen from our web server. First we need to change the contents of the index.php file inside the directory /var/www/html. We need this file to allow a get request from an outside source to change the contents of the settings.txt. The index.php file is located in the same repository as before so make sure that you download that and overwrite your index.php with that one.
Setting Up Access for Your Echo
Hosting this file on our webservers allows someone to write to the contents of the settings.txt file. You can see this file has the variables color, intensity, and fade. We write their values into the settings.txt file. But how do we set the variables to equal something? Since we are hosting this code on a webserver we can set the values through a query string. The code then gets the value from the query string that we write. For example, we can type:
http:/192.168.1.43/index.php?intensity=255&fade="0"&color="0"&get=1
Into our browser and the sever will set the intensity, fade, and color variables based on what you typed above. Make sure you are still running the controller.py script on your Pi and then type in the line above into any browser from any computer in your home. Make sure you replace my IP with the static IP you set for your Pi. The lights should change! Well actually they probably won’t because we don’t have high enough permissions to write the settings.txt file. To allow us to do so go to the /var/www/html directory on your Pi and type the following command:
sudo chmod 757 settings.txt
Now this is super unsafe as anyone can edit the file but since we are only testing a lights switch they can’t do that much damage. Just don’t ever use this on something very important. You can research other ways to do this safety but this is quick and I want to make it easy for beginners.
Okay so now go back and re run your controller.py script. And now type in the request into your browser and your lights should change accordingly. If not make sure that the settings.txt file is changing from the request and check and see if your controller.py stopped running with an error.
For this previous part I must credit 5errated from YouTube https://www.youtube.com/watch?v=n6u-H1sza-w
Although my code is different he provided with the knowledge to combine a text file with a looping python script and a web server to change the contents of the text file and eventually change the color of the lights.
Alright so now it’s is time for the Echo. The Amazon Echo allows you to create skills such as a skill where you could request domino’s pizza or a twenty question game. We are going to create our own skill to change the colors of the lights. If you have never created a skill on the Echo I suggest you get some basic understanding from here first.
https://developer.amazon.com/public/solutions/alex...
The code we write for the Echo needs to send a get request to our web server to change the lights. Here is the code I used to do so.
How the Amazon Echo Code Works
Basically this code does the same thing you did with your web browser to change the lights on/off, its color, and the fade effect on/off. In the exports.js I have the different functions for the echo to perform based on what you say. So if you say “Alexa, turn on the lights” it runs the intent lights on, which runs the function LightsOn in the exports.js file. See the function code below.
function LightsOn(intent, session, callback) {
var cardTitle = intent.name;
var repromptText = "";
var sessionAttributes = {};
var shouldEndSession = true;
var speechOutput = "";
var tools = require('./tools');
tools.ChangeColorRequest("255", "pass", "pass", function(result) {
speechOutput = "The lights are now on.";
repromptText = "The lights are now on.";
callback(sessionAttributes,
buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
});
}
The lights on uses the tools.js file to send a request to the index.php web server hosted on your Pi. I pass in the value 255 for the lights intensity, and type “pass” for the color and fade so that only the intensity changes in the settings.txt file. So when you turn the lights off the next time you turn it on it will be the same color as before. The other functions work similarly, when changing the color, I type pass for the intensity and fade and then pass in a number for the last option to change the last number in the settings.txt file to the correct color.
In the tools.js make sure you change the hostname to your IP that your ISP gives you. Just type into google “what’s my IP” and it will be the first result.
I used the test.js file to test that I can change the value of the settings.txt file from my computer. I typed up this code in notepad++ and added a plugin to let me run node.js code from it. Here’s how you allow notepad++ to run node.js code
http://blog.aguskurniawan.net/post/notepadjs.aspx
To make this code work with your echo you need to make a lambda function from this site (Amazon Management Console).
https://us-west-2.console.aws.amazon.com/
Make sure when you set up your function in the amazon management console to change "handler" to "exports.js" because I called the main file exports.
When you upload this code to the amazon management console you have to make a zip file since there is more than one file we are uploading. So zip the exports.js and tools.js files together and name that compressed folder exports. Then upload that compressed file to your lambda function in the amazon management console.
Setting Up the Echo on the Amazon Developer Service Site
Now to make the skill on your Echo you need to go to this site:
https://developer.amazon.com/
Click on the Alexa tab and then click on Alexa Skills Kit. Here you will make the settings for your Alexa Skill to change the light strip. You will also link this to your Lambda Function that you just made on your last step so it knows what code to run.
Down on the interaction modal for when you create the skill you will need an intent schema. Here is mine below.
{
"intents": [
{
"intent": "ChangeColor",
"slots": [
{
"name": "Color",
"type": "COLOR"
}
]
},
{
"intent": "ChangeIntensity",
"slots": [
{
"name": "Intensity",
"type": "AMAZON.NUMBER"
}
]
},
{
"intent": "LightsOn"
},
{
"intent": "LightsOff"
},
{
"intent": "DimLights"
},
{
"intent": "BrightenLights"
},
{
"intent": "MaxIntensity"
},
{
"intent": "MinIntensity"
},
{
"intent": "FadeOn"
},
{
"intent": "FadeOff"
},
{
"intent": "Exit"
}
]
}
Below that is the samples utterances. Here is mine.
ChangeColor change color to {Color}
ChangeColor change to {Color}
ChangeColor turn lights to {Color}
LightsOn turn lights on
LightsOff turn lights off
LightsOn lights on
LightsOff lights off
Exit exit
Exit quit
Exit quit god
Exit goodbye god
Exit goodbye
ChangeIntensity change intensity to {Intensity}
ChangeIntensity change brightness to {Intensity}
ChangeIntensity change intensity {Intensity}
ChangeIntensity change brightness {Intensity}
DimLights dim the lights
BrightenLights brighten the lights
DimLights intensity down
BrightenLights intensity up
DimLights brightness down
BrightenLights brightness up
MaxIntensity turn to maximum intensity
MaxIntensity turn to maximum brightness
MaxIntensity maximum intensity
MaxIntensity maximum brightness
MinIntensity turn to minimum intensity
MinIntensity turn to minimum brightness
MinIntensity minimum intensity
MinIntensity minimum brightness
FadeOn turn fade on
FadeOn fade on
FadeOff turn fade off
FadeOff fade off
Also for the custom slot types in the interaction modal I created my own called “COLOR” and the values in there are as follows:
RED
BLUE
PURPLE
GREEN
YELLOW
AQUA
WHITE
BLACK
ORANGE
CYAN
Configuring Your Pi So That the Echo Can Communicate With It
Alright so all seems fine but we need to do one last thing. Since your Amazon Echo’s code is hosted in the cloud it won’t be able to access index.php on your web server if you haven’t set up port forwarding. We do this in you router however for every router it’s different and hopefully you don’t have to go through multiple routers because that may complicate things. Anyways go to your main routers home page from your browser, most likely 192.168.1.1 or 192.168.200. And in their search for a port forwarding tab and configure it like so.
Make sure to change the IP address to the static IP you gave your Pi. Also hopefully your ISP doesn’t block port 80 but if it does not big deal. You will just have to change the port nginx is hosting on and change the port corresponding above. Google should help you find that out.
Alright so now you can test your skill from the Amazon Developer Service under the test tab. If it all works out, you should be able to run any of the sample utterances I provided above by invoking the skill! In the skill information tab in the amazon developer service I named the invocation name “lights”. So all I have to say is “Alexa, ask lights to turn on.” Or “Alexa, ask lights to change to cyan”. Pretty cool right!?
If it’s not working start at the lowest level, your Pi command line and work your way up to the Echo to see what part is failing.
Good Luck!