Introduction: Smart Home Monitoring Using Alexa and Arduino

About: Passionate about Electronics and Communication Engineering, IOT, Microcontrollers.

In the present world people spend more time in work place rather than their homes. Hence there is a need of home monitoring system where people can get to know the conditions of the house while they are at work. It would be even more better if one can just ask "someone" about their home during work hours. This can be accomplished by making use of Amazon Alexa as a assistant which can give the user the need information about their home.

Not Only Work Place the users can get to know the house condition at any place in the world as long as they have a internet connection and Amazon Alexa.

The following Functions are implemented in this project:

1) Control the household Devices like fan and light

2) Tells the status of the devices

3) Tells the weather condition of the house (temperature and humidity)

4) Sends the snapshot of interior of the house to user via Gmail when required.

5) Sends notification in case of -

* Intruder ( also sends photo)

* Fire

* Guest ( also sends photo)

Step 1: Gathering the Materials

Materials required:

Arduino Uno

ESP8266 Wifi Module

Linkit Smart 7688 Duo

DHT11

Relays

IR obstacle Sensor

Webcam

Amazon Echo Dot

Breadboard and jumper Wires

Software and online Services:

Thingspeak.com

Arduino IDE

Amazon Alexa Skill Set

Amazon Alexa Echosim.io ( for Testing Skill)

Backendless

PushingBox

Step 2: Programming the Arduino and ESP8266

Download the Arduino IDE From the official website: https://www.arduino.cc/en/Main/Software

Open Arduino IDE and go to file-> preferences-> in additional board manager URL type - http://arduino.esp8266.com/stable/package_esp8266...

Go to tools -> boards -> Board Manager --> and install the esp8266 package found at last.

To program arduino just plug the USB cable from the arduino to computer and select Arduino/Genuino UNO in tools-> boards. Also make sure you select rights COM ports in Tools ( COM port number can be found in Device manager). Write the required Program, Compile it and click upload if there are no errors.

To program ESP8266 make the connection as shown in the diagram. connect the usb from arduino to Computer. In tools-> board-> select Generic ESP8266 and also select right COM port. Write the required Program, Compile it and click upload if there are no errors. Make sure you connect Arduino RST to GND (arduino acts like a programmer to ESP8266).

In This Project First the ESP8266 is programmed and then the circuit connections are removed. Then the Circuit is reconnected as shown in the Figure named "Circuit Connections". And then the arduino is programmed.

Step 3: Configuring Thingspeak.com

Create an account in thingspeak.com.

we make use of channels in thingspeak to store the device control information and route it to arduino/alexa. we also store the temperature and humidity values in the channels. Its like a storage location for the information.

Login to thingspeak, go to channels--> my channels and create new channel. Give name and description to your channels. In our project we need 8 channels (you can do the job using less channels but it will be bit complicated while programming). Also thingspeak has a time constrain while updating a channel . There must be a gap of 10-15 sec between successive updating of a particular channel.

Eight channels with their values and meaning are given below

channel name(value1-meaning, value2-meaning,etc):

1) Device control (0 -lightON, 1- lightOff, 2- fan ON , 3- Fan Off)

2) light status (0- light off, 1- light on)

3) fan status (0- fan off, 1- fan on)

4) humidity ( value of humidity)

5) temperature ( value of temperature)

6) intruder notification (1- intruder alert)

7) fire notification ( 1- fire alert)

8)guest notification ( 1- guest alert)

when you click on any channel you can see its channel id and write API keys in the API keys tab. channel id is required to get the information/ value in the channel. And write key is required to store a value in the channel.

http request for updating a channel is :

https://api.thingspeak.com/update?api_key=<write Key>&field1=<value>

<write key>is replaced by corresponding write keys of the channel and <value> can be (0/1 in case of device control or temp/humidity values)

http request for reading value from a channel is:

https://api.thingspeak.com/channels/<channel ID>/field/field1/last.html

<channel ID> is replaced by the particular channel id of the channel which we want to read from.

Step 4: The Program

The program is divided into 3 parts :

A) Program for Arduino : the program for arduino is very simple. It receives the data from the ESP8266 serially and based on the data received the devices are controlled. More information about the program is found in the comments in the program itself.

B) Program for ESP8266 : the program for ESP8266 involves 3 things

1) updating the temperature and humidity using http request

client.print(String("GET ") + "/update?key=<write Key>&field1="+humidity+" HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); //update humidity

client.print(String("GET ") + "/update?key=<write Key>&field1="+temperature+" HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); // update temperature

the <write Key> is replaced by the corresponding write key found in thingspeak channel for humidity and temperature respectively. and host is api.thingspeak.com.

download dht library from: https://github.com/adafruit/DHT-sensor-library

2) reading from corresponding channels of thingspeak and controlling device based on the obtained values: client.print(String("GET ") + "/channels/<channel Id>/field/field1/last.html HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

where <channel ID> is replaced by corresponding channel id found in thingspeak.

3) Sending alert in case of high temperature via pushing box

String host1="api.pushingbox.com";

client.print(String("GET ") + "/pushingbox?devid=<your Device ID> HTTP/1.1\r\n" + "Host: " + host1 + "\r\n" + "Connection: close\r\n\r\n");

where <your Device ID> is replaced by your device id in pushingbox.

Step 5: Configuring Linkit Smart 7688 Duo and Webcam

In this project webcam and linkit smart 7688 duo is used to capture required photo and send it to the user. You can also use arduino camera module and interface it with arduino or use can use any IP camera.

Press and hold wifi button on the linkit smart 7688 for 20 sec to reset the board. Then after wifi setting is reset you will be able to see its access point name in the wireless network connections. Now connect the computer to that network. after you connect it open the browser and type 192.168.100.1 in the address bar. You will be able to see its portal. set a password to login to its portal.

After you login go to network tab and select Station mode ( we need it(linkit smart 7688 duo) to access the internet) and connect it to your Wifi network and press Configure and restart.

After the board it restarted it will be assigned a local IP address. Find out the address using any IP tools or your router portal. In my case it was 192.168.1.4.
Now type the local IP address in the address bar of the browser. make sure that the computer is connected to the same network as the linkit smart. You will be asked to login again.

To enable streaming from webcam you should enable MJPG streamer(mjpg-streamer is a command line application that copies JPEG frames from one or more input plugins to multiple output plugins). To do this in the portal go to OpenWrt location in the top right. you need to login again.

After you login go to service tab and check enable MJPG streamer and assign a port number for this service. I have assigned 4440. Click save and apply.

After the setup to can connect you webcam to the linkit smart 7688 duo usb host through an OTG cable.
Now to see the Stream open the browser and type : in the address bar. in my case it is 192.168.1.4:4400

to take the snap shot type the command <local IP>:<port>?action=snapshot

Now this image is available locally but we need to make this available to pushing box service . To achieve this we need to do port forwarding. Port forwarding can be done in router portal. The process id different for different routers. Just google to know how to port forward for for specific router. It is usually available under NAT service.
After you port forward use can access this port (ie.. 4440) from your external IP. External IP can be found by tying "whats my ip" in google.

You need to put this address

ie.. http://:<External IP>:<port>?action=snapshot

in the pushingbox( which is explained in next step) so that pushingbox can access this image and attach it to the mail and send it to you whenever required.

You can also store the image in sd card as Linkit smart 7688 duo also comes with a sd card slot for storing information. More information about this can be found at:

https://docs.labs.mediatek.com/resource/linkit-sm...

Step 6: Configuring PushingBox

pushingbox is used to send notification about different alerts in the project to gmail.

signin to pushingbox using google account: https://www.pushingbox.com/

go to my services--> add service. there are many services to choose from like Gmail, twitter, push notification for android etc...

select Gmail( as we need to send photo as attachment) and fill in appropriate gmail name configuration and gmail id of the user to whom the alert has to be sent.

go to my scenarios and create a new scenario. give name to the scenario( ex : ALERT) add the previously created service.

write suitable subject and body of the mail and enter the url to take the screenshot of web cam for attaching photo. Create different scenarios for different alerts.
api for executing a pushing box scenario is : http://api.pushingbox.com/pushingbox?devid=<device ID>

Step 7: Creating Alexa Skill Using Backendless

backendless is used to create the alexa skill. It is a simple drag and drop programming used to create alexa skill (or any programs) which can be accessed by backendless API.

create an account in backendless: https://backendless.com/

  • Log in to your account in backendless account. click Create app and give a name to your app
  • Click the Business Logic icon located in the icon bar on the left. You will see the API SERVICES screen.
  • Click the “+” icon to create a new service. Make sure to select CODELESS in the “New Service” popup. Enter “AlexaService”for the service name. Click the SAVE button:
  • Backendless creates the API Service and will prompt you to create a method for the service. This will be the method which will process requests from Alexa. Enter “handleRequest” for the method name. Make sure to select POST for the REST operation, and declare an argument with the name of “req” and type “Any Object” as shown:
  • Backendless creates a placeholder for the codeless logic of the method. Click the EDIT button to start switch to the Codeless Logic Designer. In the created function placeholder block, click the area that says “doSomething” and change it to “sendAlexaResponse”. This function is used to make alexa say something which can be passed as argument. Click the SAVE button so the function is save.
  • Click the gear icon located in the purple block right next to the word “Function”. Add two arguments by dragging the input blocks as shown in the image below. Assign the names of the arguments as “whatToSay” and “waitForResponse“. Notice that as you add arguments, the Context Blocks area is automatically populated with the blocks representing argument values.
  • Modify the function’s logic so it looks as in the image. For the “Create Objects” blocks, use the gear icon to change the name of the object properties. Do not forget to save your work by clicking the SAVE button.
  • Now that the custom function is built, switch back to the handleRequest method of the AlexaService service. Click the Custom Functions category in the toolbar on the left and drag the sendAlexaResponse block to connect with the return connector of your service method
  • The above steps can also be found in their website: https://backendless.com/developing-alexa-skill-wi... https://backendless.com/developing-an-alexa-skill...
  • Click the “Add New” node under Functions in the BROWSER section.In the created function placeholder block, click the area that says “doSomething” and change it to “getIntentName”modify the blocks so that the function looks like the shown image.This will get the intent name based on sample utterances. Go back to api services->-->handle request in the browser section. Variables and logic is create from system section. Create the following variables shown in images.
  • next we store the intent name to request variable. And then compare with the intents. for example if the request is "introduction" then respond variable is set to "hi! I can control......." and this response is read aloud by alexa at last. modify the block as shown.
  • if the request is LightsOn intent then we update the thingspeak channel to '0' using http get request and at the same time we update the device status( 1/0 depending on On/Off). Same thing is repeated for LightsOff, FanOn and FanOff.
  • For weather we read from Temperature and humidity channel and store the result in respond variable. As the channel only give values we append texts to make the response meaningfull
  • for snapshot of the living room we run pushingbox scenario
  • for device status we read information from status channel of thingspeak:
  • for notification and alerts we read from alert channels( fire, intruder and guest):
  • based on the values that we get from notification field corresponding alert messages are stored in respond0 variable. if there are no notification then no notification message is stored.
  • once the notification is read out then the '0' is updated in the notification channels so that alexa wont read same notification again. Then at last based on the request, respond0/respond variable is read aloud.

Step 8: Configuring Alexa Skill in Amazon Developer Console:

go to amazon developer console and signin using amazon account. https://developer.amazon.com/

go to developer console and click on ALEXA tab. Click on alexa skills kit get started.

create custom skill type, give name and invocation name to the skill.
the respective intents and sample utterances are given in the code.

in the configuration tab select HTTPS as service end point type fill the default URL with the API URL from backendless.Select the 2nd option in Certificate for default endpoint in SSL certificate. You can also test the skill using test simulator.

After the test is complete you can publish the skill with required publish information.

Step 9: Final Setup and Finish!

Make the circuit connection as shown.

Sometimes ESP8266 will malfunction because of insufficient current. So, though notmentioned in the circuit, it is recommended to power the ESP8266 from separate 3.3v source. If you are using a power bank make sure you reduce the voltage from 5v to 3.3v using a 3.3v voltage regulator.
Upload program to ESP8266 and arduino. I have shown the connection to bulb, same thing can be extended to fan or any devices. Finally use amazon echo or echosim.io to test you skill.

You must activate the skill using the invocation name ( as is my case - "myhome"). Sometimes it won't work if it is used without invocation name as I have shown couple of times in my video.

Hope you enjoyed the tutorial!

Thank You!