Introduction: Voice Control Your House
In this fast growing world, voice technology plays a very important role. Think about controlling almost anything with your voice. Getting things done just by speaking is a great way to interact with things around us. This instructable is all about using the Amazon Echo (Not compulsory that you own one) as a tool to interact with your devices. This instructable will teach you how to control lights, fan & Television with just your voice. There might be a chance that other device will also be added to be voice controlled. But for now lets start with this.
I hope this project will be fun and you get the most out of it. Feel free to comment and ask your doubts.
NOTE: The first 3 Steps guide you to control lights and table fans. The steps after that, guide you to control your television.
Also see my controlling Television Video above.
Step 1: Getting the Stuff Needed. (Controlling the Lights Part)
- First of all, it is not compulsory to have an Amazon Echo. If you have one that's awesome.
- Secondly, if you don't have one, that is awesome too because you can simply install the Alexa Voice Service (AVS).
- If you are too lazy to get that service on your Raspberry Pi, you can use Alexa on the Alexa Simulator and skip this step.
Other things that you will need:
A Raspberry Pi with Raspbian.
A Breadboard.
Jumper Cables (Female to Female)
A relay module (5V)
A long cable which you can get from any electrical shop
A light bulb and a holder for it.
LETS GET STARTED.
Step 2: The Hardware (Controlling the Lights Part)
This part is the most easy part but you need to be careful as it involves playing with the mains.
You just have to make the proper connections. Refer the diagram which will make it easy for you to construct all the stuff. While connecting your lamp to the relay module be sure that you cut the live wire(shown in red in the diagram) and make the connections as shown. The black wire is the wire that goes to earth and there is direct connection between earth and the bulb, while the red wire coming from the bulb is cut and is connected to the relay module and from there it goes to the plug. If the connections are correct then you will not have any problems.
Step 3: The Software (Controlling the Lights Part)
In order to use Alexa to control your lights you need to make an Alexa Skill on the amazon developer website.
So first go to the amazon developer website and login into your amazon account and follow the following steps:
- After signing in go to the Alexa tab and click on Get Started below the Alexa Skills Kit. (The images above might help.)
- Then click on add a new skill.
- Enter the name as: "Light Control" and the invocation name as: "System".(without quotes)
- Save and go to interaction model and insert the following code inside the Intent Schema:
{ "intents": [ { "slots": [ { "name": "status", "type": "GPIO_CONTROL" }, { "name": "pin", "type": "AMAZON.NUMBER" } ], "intent": "LightIntent" } ] }
Then inside Custom Slot types, enter type as "GPIO_CONTROL" and Enter values as 1. on and 2. off .
- Scroll down and inside and under sample utterances type:
LightIntent to turn light {pin} {status}
- Image 4 and 5 show these above steps.If everything is correct Save and click on Next.
Now after saving, power up your Raspberry Pi and follow the steps:
- Open a terminal window and type
sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install python2.7-dev python-dev python-pip sudo pip install Flask flask-ask
- Then Visit https://ngrok.com/download and get the latest Linux ARM release as a zip and unzip inside the home directory:
unzip /home/pi/ngrok-stable-linux-arm.zip sudo ./ngrok http 5000
Keep this terminal open and go ahead.
- Open a new terminal and type:
sudo nano light_control.py
And Copy the following code :from flask import Flask from flask_ask import Ask, statement, convert_errors import RPi.GPIO as GPIO import logging GPIO.setmode(GPIO.BCM) app = Flask(__name__) ask = Ask(app, '/') logging.getLogger("flask_ask").setLevel(logging.DEBUG) @ask.intent('LightIntent', mapping={'status': 'status', 'pin': 'pin'}) def light_control(status, pin): try: pinNum = int(pin) except Exception as e: return statement('Pin number not valid.') GPIO.setup(pinNum, GPIO.OUT) if status in ['on', 'high']: GPIO.output(pinNum, GPIO.HIGH) if status in ['off', 'low']: GPIO.output(pinNum, GPIO.LOW) return statement('Turning light {} {}'.format(pin, status)) if __name__ == '__main__': port = 5000 #the custom port you want app.run(host='0.0.0.0', port=port)
- Press ctrl+X and then Y.
- Now type in the following and keep this terminal open too.
sudo python light_control.py
- Now on your pc go back to the amazon developer site. Go to the Configuration tab and select HTTPS. Check both North America and Europe and in the blanks type in the forwarding address which you will find in the terminal where you ran the ngrok command.
- Then go to SSL certificate tab and select the second last option and save.
- Then you have to test it but its not a compulsion so you can skip that.
NOTE: The images are all arranged step by step. Follow them and things will go right. Feel free to ask doubts in the comment section.
Step 4: Getting the Stuff Needed (The Television Part)
Things needed:
- A Television and its remote control.
- A Raspberry Pi with Raspbian.
- Breadboard, zero pcb, Soldering gun and wire.
- Infrared Sensor--TSOP38238.
- IR LED.
- NPN Transistor PN2222.
- 1 Kohm and 220 ohm resistors.
- Jumper Cables
That is it. Let us move ahead.
Step 5: Getting You Remote Control Values Into Raspberry Pi
In this step we will get all the remote control values into our Raspberry Pi.
Firstly make the circuit as shown in the diagram.
Then power on your Raspberry Pi and:
- Open a Terminal and run these commands
sudo apt-get update<br>sudo apt-get upgrade sudo rpi-update sudo reboot
- After rebooting open another Terminal and run this command:
sudo apt-get install lirc
- Then open
sudo nano /etc/modules
- Add these lines in it:
- lirc_dev
- lirc_rpi gpio_in_pin=18 gpio_out_pin=17
These lines startup LIRC on boot and sets IR sensor pin to pin 18 and IR LED to pin 17. Press ctrl+x and then y so that its saved.
- Then we have to change the hardware configuration file. So first open it:
sudo nano /etc/lirc/hardware.conf
- Change these lines inside it:
- DRIVER="default"
- DEVICE="/dev/lirc0"
- MODULES="lirc_rpi"
- NOTE: If you are working on RPi 2 then:
- Open a terminal and open:
sudo nano /boot/config.txt
- And add this line:
dtoverlay=lirc-rpi,gpio_in_pin=18,gpio_out_pin=17,gpio_in_pull=up
Done. Now simply reboot.
sudo reboot
- Open a terminal and open:
- Change these lines inside it:
- Now after rebooting we need to check whether our IR Sensor is working or not.
- Open a terminal and type these two commands:
sudo /etc/init.d/lirc stop
mode2 -d /dev/lirc0
- Now point your Remote Control towards the IR sensor and when you click the buttons you see pulses and spaces (as shown in the picture 2 above.). This indicates that everything is working fine
- Open a terminal and type these two commands:
- Lets Record our Remote Control values on the Raspberry Pi.
But First NOTE that you need to give a name for every button after you record it. But the names should be valid that is they should be from the names given in the LIRC list. So run this command and you will get all the valid names. Run this :
irrecord --list-namespace <br>
Now run these commands and start recording:
sudo /etc/init.d/lirc stop irrecord -d /dev/lirc0 ~/lircd.conf <br>
Follow the instructions displayed in the terminal and finish the recording process.
- .After that replace the original config file with the new recorded one by running this command:
sudo cp lircd.conf /etc/lirc/lircd.conf <br>
That is it. Start LIRC now:
sudo /etc/init.d/lirc start
This is it. Now lets move ahead and make the Raspberry Pi Remote Circuit.
Step 6: Making the Raspberry Pi Remote Circuit.
Make the Circuit just the same way as shown in the diagram.
You Should have no problems if you make it the same way.
Test it on a breadboard and if things are working correctly at the end of this instructable then you can make a permanent circuit on a zero PCB.
Now lets train ALEXA and do some Python.
Step 7: Training Alexa and Coding in Python.
Now lets train ALEXA so that we can control our TV by simply asking her.
- Go back to developer.amazon.com and sign in.
- Go to Alexa tab. Select the Alexa skills kit section and add a new skill.
- Enter the name: "Remote Control" and Invocation Name: "remote". (shown in the first pic.)
- In the interaction model do this:
- Intent Schema:
{
"intents": [ { "intent": "RemoteControlIntent", "slots": [ { "name": "channel", "type": "AMAZON.NUMBER" } ] } ] } - Sample Utterances:
RemoteControlIntent to go to channel {channel}
- Intent Schema:
- Do the Configuration process the same way you did in Step 3.
- Also Repeat the SSL Certificate process from Step 3.
- Do the testing and we are done with training Alexa.
Lets move on to python on the Raspberry Pi.