Automate your Home using Raspberry Pi and Amazon Echo Dot
This project uses an Amazon Echo Dot which is used for natural language processing (NPL) and a Raspberry Pi 4 Board. We can give a voice command to Alexa device and switch ON/OFF any home appliance like Light, Fan, AC, TV, etc. through a relay connected with the raspberry board using Node-Red.
Raspberry Pi 4 Pin Configuration:
Relay Board Pin Configuration:
Procedure for installing Node-Red and Alexa Skill Library:
- Insert SD card with Buster OS in the raspberry pi 4. Connect HDMI monitor, keyboard and mouse and power up the board.
- Run the following command. It will download and run the script.
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
This script will:
- remove the pre-packaged version of Node-RED and Node.js if they are present
- install the current Node.js LTS release using the NodeSource. If it detects Node.js is already installed from NodeSource, it will ensure it is at least Node 8, but otherwise, leave it alone
- install the latest version of Node-RED using npm
- optionally install a collection of useful Pi-specific nodes
- setup Node-RED to run as a service and provide a set of commands to work with the service
“Node-RED has also been packaged for the Raspbian repositories and appears in their list of 'Recommended Software'. This allows it to be installed using apt-get install node-red and includes the Raspbian-packaged version of Node.js, but does not include npm.”
3. The following commands are provided to work with the service:
- node-red-start - this starts the Node-RED service and displays its log output.
- node-red-stop - this stops the Node-RED service
- node-red-restart - this stops and restarts the Node-RED service
- node-red-log - this displays the log output of the service
You can also start the Node-RED service on the Raspbian Desktop by selecting the Menu -> Programming -> Node-RED menu option.
Autostart on boot:
If you want Node-RED to run when the Pi is turned on, or re-booted, you can enable the service to autostart by running the command:
sudo systemctl enable nodered.service
To disable the service, run the command:
sudo systemctl disable nodered.service
Once installed as a global module you can use the node-red command to start Node-RED in your terminal and the message will be as bellow.
$ node-red Welcome to Node-RED =================== 11 Oct 23:43:39 - [info] Node-RED version: v1.0.2 11 Oct 23:43:39 - [info] Node.js version: v10.16.3 11 Oct 23:43:39 - [info] Darwin 18.7.0 x64 LE 11 Oct 23:43:39 - [info] Loading palette nodes 11 Oct 23:43:44 - [warn] rpi-gpio : Raspberry Pi specific node set inactive 11 Oct 23:43:44 - [info] Settings file : /Users/nol/.node-red/settings.js 11 Oct 23:43:44 - [info] HTTP Static : /Users/nol/node-red/web 11 Oct 23:43:44 - [info] Context store : 'default' [module=localfilesystem] 11 Oct 23:43:44 - [info] User directory : /Users/nol/.node-red 11 Oct 23:43:44 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable 11 Oct 23:43:44 - [info] Creating new flows file : flows_noltop.json 11 Oct 23:43:44 - [info] Starting flows 11 Oct 23:43:44 - [info] Started flows 11 Oct 23:43:44 - [info] Server now running at http://127.0.0.1:1880/red/
You can then access the Node-RED editor by pointing your browser at http://localhost:1880 . or http://(your_RPI4_IP_Address):1880.
4. Install node-red-node-pi-gpio library to interact with Raspberry Pi GPIO. It is preconfigured in some of the distribution like Raspbian Buster.
5. Install node-red-contrib-alexa-home-skill library in Node-Red. It provides the end point node to bring your commands into Node-RED.
cd ~/.node-red npm install node-red-contrib-alexa-home-skill
6. Make an account on https://alexa-node-red.bm.hardill.me.uk/docs and. And add devices as given in the below picture.
The name you give to the device will be how you access it from Alexa and should be unique, be careful with names like "Livingroom" that may be interpreted as "Livingroom" or "Living room".
7. Now start coding on Node-Red editor.
Configuring Nodes in Node-Red editor:
You can find the Alexa Home Skill nodes in the Alexa section of the Node-RED Palette.
Dragged an Alexa Home node on to the canvas and configure it as given below.
- Click on the pencil at the end of the Accounts line to add a new account
- Enter your username and password of the account created on https://alexa-node-red.bm.hardill.me.uk/docs
- Click OK to return to the node config.
- The devices dropdown should now be populated with the devices you defined earlier, if not hit the refresh button at the end.
To start with leave the Auto Acknowledge checked, this tells Alexa that the command has succeeded as soon as it is received by Node-RED.
The Switch node works out with below command:
For TurnOnRequest: Function 1
msg.payload = “1”;
For TurnOffRequest: Function 2
After configuring all node, deploy the code. A green dot will be indicated near the device node which shows that it is connected to end-device and ready to accept the command from amazon echo dot.
8. Now make an account on Amazon Alexa and configure your amazon echo dot using Alexa App through your mobile phone and go to “skill and games”. Search for Node-Red skill which has a logo as below.
Enable this skill using username and password which you set on https://alexa-node-red.bm.hardill.me.uk/docs
Now your system is ready to work.
9. Give the command to your echo dot as “Alexa, discover devices”. It will discover all your set devices and appear in the mobile Alexa App.
10. Now give the command “Alexa, turn on kitchen light”. It will activate the relay connected to the respective GPIO pin of raspberry pi and switch on the light.
11. To turn off the light, give the command “Alexa, turn off kitchen light”.