In previous tutorials we have seen How we can build an Amazon Echo Speaker and then how can we control any Raspberry Pi GPIO using Alexa Voice. Now we are making an IoT project to control home appliances from anywhere in the world using AlexaPi and ESP-12E (or any ESP board).
Hardware Requirements
- Raspberry Pi with AVS installed in it
- USB 2.0 mic /Webcam
- ESP-12E
- Relay module
- LED/AC bulb
You should have your Raspberry Pi ready with Alexa voice service installed in it with a properly configured Amazon Developer Account. So go through below tutorial to get Alexa services ready.
Project Flow for Alexa Voice Controlled LED
We will follow this flowchart for this IoT controlled LED project:
So, the process is something like this. First, input is given to Raspberry Pi through the USB Mic. Now, this recording is sent to Alexa voice services and after voice recognition, AVS sent the data to IFTTT and it triggers the condition in IFTTT. According to the recipe, IFTTT will send the command to Adafruit IO which is the MQTT broker to perform an action. Then ESP12e will receive the data from Adafruit IO via MQTT protocol and LED will turn ON/OFF according to the command.
Here we have used a USB WebCam for Microphone. We have already used Alexa Voice service to turn on a bulb, but it can only handle appliances which are connected locally.
As we assume that you already have Raspberry Pi with Alexa Voice services installed, so we are remaining with below two steps, which we will explain one by one:
- Setting Up an Adafruit account for communication
- Link Alexa to Adafruit IO using IFTTT
- Upload code in ESP12
Setting Up an Adafruit account for communication
First, we will make a feed in Adafruit IO. Feed stores the data sent by IFTTT. To make feed follow these steps:
Step 1:- Login to Adafruit IO with your credentials or Sign up if you don’t have an account.
Step 2:-Click on My account -> Dashboard
Step 3:-Click on Actions and Create a New Dashboard.
Step 4:-Give name and description to your feed and click on Create.
Step 5:-Click on Key button and note down the AIO Keys, we will use this key in our code.
Step 6:-Click on ‘+’ button to create a new block and click on Toggle button.
Step 7:-Now, Enter Name of Feed and click on Create. Then Select the feed and click on Next step.
Step 8:-In block settings, Write ‘1’ in Button ON text field and ‘0’ in Button OFF Text field.
Step 9:-Your Feed is successfully created.
Link Alexa to Adafruit IO using IFTTT
Follow these steps to make an Applet/Recipe in IFTTT: Adafruit and click on it.
Step 9:-Login to Adafruit Account using your credentials. Click on Send data to Adafruit.
Select feed name that you just created in Adafruit IO. Now, give ‘1’ in data to save, this implies LED will be ON. Click on Create Action.
Step 10:-Follow the same steps to make applets to turn the LED OFF. Just put ‘0’ in the Data to save field. All steps remain the same.
You have done half of your work. Now, it’s time to program your ESP-12E.
ESP12 Code Explanation
We will program ESP12 with Arduino IDE. Complete code is given at the end of this Tutorial.
First, we need a Adafruit Mqtt library which can be downloaded from this link. Just open Arduino IDE . Go to examples -> adafruit mqtt library -> mqtt_esp8266
We will edit this code according to our AIO keys and Wi-Fi credentials.
First, we included all the libraries for ESP8266WIFI and Adafruit MQTT.
#include <ESP8266WiFi.h> #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h"
We defined the SSID and Password for your Wi-Fi, from which you want to connect your ESP-12e.
#define WLAN_SSID "xxxxxxxx" #define WLAN_PASS "xxxxxxxxxxx"
This section defines the Adafruit server and server port which is fixed as “io.adafruit.com” and “1883” respectively.
#define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883
Replace below fields with your username and AIO keys which you have copied from Adafruit site while making the Feed.
#define AIO_USERNAME "********" #define AIO_KEY "******************************"
Then create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Setup a feed called 'light' for subscribing to changes.
Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/light");
In setup function, we declare PIN of ESP-12e on which you want to get output. I am using D0 pin as output. Then, we connect ESP-12e to Wi-fi access point.) { …. …. …
Setup MQTT subscription for light feed.
mqtt.subscribe(&light); }
In loop function, we will ensure that the connection to the MQTT server is alive using MQTT_connect(); function.
void loop() { MQTT_connect();
Now, we subscribe our ‘light’ feed and get the string from adafruit IO and convert this string to number using atoi(); function and write this number to PIND0 using digitalWrite(); function.
Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { if (subscription == &light) { Serial.print(F("Got_light: ")); Serial.println((char *)light.lastread); uint16_t num = atoi((char *)light.lastread); digitalWrite(16,num); }
Working:
Connect your ESP-12E with the laptop and upload below code (don’t forget to edit your credentials in the code).
Connect a LED or relay to pin D0. Now, make sure your Alexa service is running on your RPi. add more appliances in the above code by adding the GPIO pins of ESP-12E and by making different Applets with different phrases in IFTTT.
Check the complete code and Demonstration Video below. Also check our all the Home Automation Projects here
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "**********"
#define WLAN_PASS "**********"
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME “**********"
#define AIO_KEY "**********************"
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe light = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/light");
void MQTT_connect();());
// Setup MQTT subscription for light feed.
mqtt.subscribe(&light);
}
uint32_t x=0;
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &light) {
Serial.print(F("Got_light: "));
Serial.println((char *)light.lastread);
uint16_t num = atoi((char *)light.lastread);
digitalWrite(D0,num);
}
}!");
}
Jul 24, 2018
I think this post very useful to so many people.
So I really thank you for your help.
Jul 24, 2018
nice post its helpful