element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
IoT: In the Cloud
  • Challenges & Projects
  • Project14
  • IoT: In the Cloud
  • More
  • Cancel
IoT: In the Cloud
Blog Smart Switch Using Arduino and Alexa
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join IoT: In the Cloud to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: ruchir1674
  • Date Created: 18 Mar 2019 7:04 PM Date Created
  • Views 3358 views
  • Likes 10 likes
  • Comments 6 comments
  • mkr_1010
  • ardintermediate
  • arduino_iot_cloud
  • iotcloudch
Related
Recommended

Smart Switch Using Arduino and Alexa

ruchir1674
ruchir1674
18 Mar 2019
image

IoT: In the Cloud

Enter Your Project for a chance to win an Arduino MKR WAN 1300 Kit with a Compatable Lora Gateway or an MKR 1400 GSM with a Shield Combo + a $100 shopping cart!

Back to The Project14 homepage image

Project14 Home
Monthly Themes
Monthly Theme Poll

 

Smart Switch Using Arduino and Alexa

 

List of materials

  • 12V Relay Module  == > $ 4.20
  • Arduino uno  == > $ 8
  • DHT11 temperature sensor  == > $ 3
  • ESP8266 Module == > $ 4.70
  • 4N26 optocoupler == > $ 0.60
  • LM1117 voltage regulator == > $ 0.60
  • Breadboard == > $ 2.20
  • Jumper wires == > $ 2.50
  • Push button == > $ 2.50

 

Total cost of the project is about $30.

 

You can also use NodeMCU instead of using Arduino and ESP8266.

This project is divided into three parts. First, we use Heroku to create an app and link GitHub with Heroku. Second, we build an Amazon Alexa skill to implement our work (most important part). Third, we setup our hardware and program it using Arduino IDE.

 

1. Link Heroku with GitHub

 

Heroku is a cloud platform as a service (PaaS) supporting several programming languages that is used as a web application deployment model.

First, go to heroku site create a new account or login there. Link is given: https://www.heroku.com/

Let's start with creating a new app. I have given my app the name "iottempswitch".

 

image

Once the app is made go to GitHub: https://github.com/

Login there or signup if you do not have an account. Once logged in, create new repository.

 

image

 

image

Give any name you want to choose and then press create repository. After that you will see page like this

image

On this page, click on README and you will see page like this.

image

On this page, give the description you want to share with others. After that, click on commit new file.

image

 

image

Next, click on upload button. You will see a page like this.

image

There are two options: either you drag-and-drop the folder or choose the file. Download the required files from below. After selecting the files, press commit changes. You will see page like this.

image

Once done, it will look like this.

image

 

Next we upload three libraries. Click again on the upload file, upload the required files.

 

image

Create a new file, name it Procfile and write web: python app.py. After that commit it.

image

Create a new file, name it requirements.txt and write websockets==3.2.

 

image

Create new file, name it runtime.txt and write python-3.6.3 or python-3.6.4

image

 

image

Open app which you created at Heroku then go to deploy section.

image

 

Click on GitHub you will see like this.

image

 

Give repository name which you created at GitHub side. In my case it is Smart-Relay. Copy that and paste it here. You will see like this.

image

Once your link is shown click on connect. You will see like this.

image

 

Next, click on deploy branch.

image

After completing deploy you will see like this.

 

image

 

image

 

Now, we have created our app successfully. Go to setting and use this link for Amazon and Arduino program.

image

 

2. Amazon

 

On Amazon Developer site we use Amazon skill to control switch trigger by setting temperature and humidity.

  • Go to Amazon Developer Site. Link is given: https://developer.amazon.com/
  • Go to Developer console.

Go to Alexa then select Alexa Skill Kit and then create new skill by clicking on Add new skill. When you add new skill you will see skill information page.

 

1. Skill Information

We have to provide skill type, language, name.

  • It will ask for skill name and language.
  • Name ==> Select any name.
  • Language ==> Choose according to your region or country.

image

On next page it will ask for skill type, there are three skill type namely custom, flash briefing, smart home.

image

Skill type ==> select custom.

image

After that you will see a page like this.

image

 

2. Interaction Model

Now invocation name comes under interaction model before it comes under skill information.

  • Go to invocation section and give any you want to use.
  • Invocation name ==> which you use while communicating with Alexa.

For example: Alexa, ask sensor to turn switch trigger on or Alexa, ask light on, here invocation names are sensor and light.

image

  • Firstly, we create new intents. Click on Add (on left hand side) and give any name you want I used "smartswitch". After that add slots type.

image

 

  • Give slot type name "measurement_type" and slot values "temperature" and "humidity".

image

 

  • After that add slot type name "query" and slot values are "what" and "is". It will look like this.

image

 

  • After that add slot type "switchstate" and slot values are "on" and "off".

image

 

  • Add another slot type "tempscale" and slot values are "fahrenheit" and "celcuis".

image

  • After that add new slot type here we use existing slot type for that we have to click on use existing slot.In existing slot search for amazon.number and select this and add it. After adding it you will see it in slot types.

image

So we are done with slot types; in total, we are using 5 slot types. Now, move to the next step. Click on intent which we created, in my case it is smartswitch. At the bottom you will see intent slot.

 

image

 

  • Create a new slot, give it name "Switch_State" and map it to "switchstate" by using drop down button.

image

 

  • Create new slot, give it name "Sensor_Values" and map it to "measurement_type".

image

 

  • Create new slot, give it name "query" and map it to "query".

image

 

  • After that create new slot "tmp_scale" and map it to "tempscale".

image

 

  • Create new slot "Numbers" and map it to "Amazon.Number".

image

Now we are done with Intent slots. We are using 5 intent slots. After this we move to Sample Utterances.

image

 

Add this sample utterances.

 

set switch trigger to {Numbers} percent {tmp_scale}
{query} is the switch state
{Switch_State} switch trigger
set switch trigger to {Numbers} degree {tmp_scale}
turn switch {Switch_State}
{query} switch {Switch_State}
{query} is the current {Sensor_Values}

 

 

After this save model and build it.

image

 

Wait for model to be build after that click on configuration.

 

3. Endpoint

 

Select HTTPS and add link which was generated while creating Heroku app. In my case it is https://iottempswitch.herokuapp.com/. After adding link, for SSL certificate select second option and click on next.

image

We have successfully created our custom skill.

 

3. Arduino

 

For ESP8266

Step 1

  • Open Arduino IDE.Then go to File ==> Preference.

image

Step 2

  • In Additional Boards Manager, copy and paste the URL and click ok: http://arduino.esp8266.com/stable/package_esp8266com_index.json

 

image

image

Step 3

  • Open Board Manager by going to Tools ==> Board ==> Board Manager.

image

 

Step 4

  • Open Boards Manager and search for nodemcu.

image

Step 5

After that download ESP8266WiFi library.

  • Open library Manager : Sketch ==> Include library ==> Manage Libraries.

image

 

Step 6

  • Search for ESP8266WiFi library:

image

Step 7

  • Select board ==> Generic ESP8266 Module.

image

  • Before uploading the code we need three libraries: Libraries

Move these libraries to the libraries folder of Arduino.

image

You have to change three things in code SSID, PWD and your heroku app link. After that upload the code. For ESP modules you have to press flash button while uploading the code and then press reset button one time and then release the flash button. After uploading the code, open the terminal. you will see output.

 

image

4. Component Description

 

1. Relay

 

Relay is an electromagnetic device which is used to isolate two circuits electrically and connect them magnetically. They are very useful devices and allow one circuit to switch another one while they are completely separate. They are often used to interface an electronic circuit (working at a low voltage) to an electrical circuit which works at very high voltage. For example, a relay can make a 5V DC battery circuit to switch a 230V AC mains circuit.

 

How it works

 

A relay switch can be divided into two parts: input and output. The input section has a coil which generates magnetic field when a small voltage from an electronic circuit is applied to it. This voltage is called the operating voltage. Commonly used relays are available in different configuration of operating voltages like 6V, 9V, 12V, 24V etc. The output section consists of contactors which connect or disconnect mechanically. In a basic relay there are three contactors: normally open (NO), normally closed (NC) and common (COM). At no input state, the COM is connected to NC. When the operating voltage is applied the relay coil gets energized and the COM changes contact to NO. Different relay configurations are available like SPST, SPDT, DPDT etc, which have different number of changeover contacts. By using proper combination of contactors, the electrical circuit can be switched on and off. Get inner details about structure of a relay switch.

The COM terminal is the common terminal. If the COIL terminals are energized with the rated voltage, the COM and the NO terminals have continuity. If the COIL terminals are not energized, then the COM and the NO terminals have no continuity.

The NC terminal is the Normally Closed terminal. It is the terminal that can be powered on even if the relay doesn't receive any or sufficient voltage to operate.

The NO terminal is the Normally Open terminal. It is the terminal where you place the output that you want on when the relay receives its rated voltage. If there is no voltage to the COIL terminals or insufficient voltage, the output is open and receives no voltage. When the COIL terminals receive the rated voltage or a little under, the NO terminal receives sufficient voltage and can turn on the device on the output.

 

2. DHT temperature sensor

image

DHT11 is a Humidity and Temperature Sensor, which generates calibrated digital output. DHT11 can be interface with any micro controller like Arduino, Raspberry Pi, etc. and get instantaneous results. DHT11 is a low cost humidity and temperature sensor which provides high reliability and long term stability.

3. ESP8266 Complete Description

 

The ESP8266 WiFi Module is a self-contained SOC with integrated TCP/IP protocol stack that can give any microcontroller access to your WiFi network. The ESP8266 is capable of either hosting an application networking functions from another application. Each ESP8266 module comes pre-programmed with an AT command.

The ESP8266 supports APSD for VoIP applications and Bluetooth co-existence interfaces, it contains a self-calibrated RF allowing it to work under all operating conditions, and requires no external RF parts.

 

Features

  • 802.11 b/g/n
  • Wi-Fi Direct (P2P), soft-AP
  • Integrated TCP/IP protocol stack
  • Integrated TR switch, balun, LNA, power amplifier and matching network
  • Integrated PLLs, regulators, DCXO and power management units
  • +19.5dBm output power in 802.11b mode
  • Power down leakage current of <10uA
  • 1MB Flash Memory
  • Integrated low power 32-bit CPU could be used as application processor
  • SDIO 1.1 / 2.0, SPI, UART
  • STBC, 1×1 MIMO, 2×1 MIMO
  • A-MPDU & A-MSDU aggregation & 0.4ms guard interval
  • Wake up and transmit packets in < 2ms
  • Standby power consumption of < 1.0mW (DTIM3)

 

                                                                                                 image

For connecting ESP Module with Arduino UNO we need Lm117 3.3 voltage regulator or any regulator because Arduino is not capable of providing 3.3 v to ESP8266.

 

Note: While uploading the code press flash button and then press reset button one time and then release flash button.

                                                                     image

For connecting DHT11 sensor and relay we use two GPIO pins of ESP8266 Module. After uploading the code you can disconnect the RX, TX, GPIO0 pins.

I have used GPIO0 for DHT11 sensor and GPIO2 for relays. DHT11 sensor works fine with ESP8266 but for relays we need one extra thing, i.e, opto isolator or opto coupler.

image

 

5. Connections

 

ESP8266    === >  DHT11

  • GPIO0 === > Output pin

ESP8266  === >   Relay

  • GPIO2 ===> Input

ARDUINO  ===>  ESP8266

  • Gnd ===> Gnd
  • TX === > TX
  • RX === > RX
  • Reset Button === > RST
  • Flash Button === > GPIO0

 

image

Program

Program given in file in step 7

 

6. After setup all the thing

 

We have successfully created our app, skill and our hardware is ready. So, its time to check. For that your ESP8266 is powered on because our server is running on ESP8266. Here I have not connected any sensor to ESP8266 I am just checking whether its working or not but you can connect sensor, relay to ESP8266. Once it is connected to Heroku you will see connected. For testing go to Amazon skill which you created, then click on test page. Once it is verified its working I will connect my sensor to ESP8266. You can see results as shown below in Result section.

image

If you use it without connecting ESP8266 you get this error.

image

 

Utterance you can use

  • set switch trigger to {Numbers} percent {tmp_scale}

ex. set switch trigger to 50 percent humidity

  • {query} is the switch state

ex. on/off is the switch state

  • {Switch_State} switch trigger

ex. on/off switch trigger

  • set switch trigger to {Numbers} degree {tmp_scale}

ex. set switch trigger to 76 degree fahrenheitex. set switch trigger to 24 degree celcius

  • turn switch {Switch_State}

ex. turn switch on/off

image

 

image

image

image

7. While talking to Alexa

  • Alexa, ask arduino to turn switch trigger on/off
  • Alexa, ask arduino to set switch trigger to 24 degree celsius.
  • Alexa, ask arduino to set switch trigger to 50 percent humidity.
  • Alexa, ask arduino to turn switch on/off

File needed for GitHub. Please download from the link given below.

File

8. Results

 

Temperature and humidity reading.

image

Set trigger to 90 percent humidity.

 

image

Set trigger for 21 degree Celsius.

image

 

1. Set trigger for temperature and humidity

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

2. Set trigger to 20 degree celsius

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

3. Set trigger to 80 percent humidity

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply

Top Comments

  • vimarsh_
    vimarsh_ over 6 years ago +2
    Very useful and interesting project. Also documentation in EPIC.
  • dubbie
    dubbie over 6 years ago in reply to vimarsh_ +1
    Ruchir, I completely agree with you, epic instructions. It must have taken many months to work all this out as I think it would take me several weeks just to follow them. Dubbie
  • ruchir1674
    ruchir1674 over 6 years ago in reply to dubbie +1
    Thanks dubbie Happy that you liked it. Please share your blog when you make it changes are welcome. Regards Ruchir
  • ruchir1674
    ruchir1674 over 6 years ago in reply to rosey12

    Hey rosey12

     

    Which device are you using ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rosey12
    rosey12 over 6 years ago

    I am also facing this type of error on my device. It is showing Asus error code 55. Do you have any idea how to fix this?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ruchir1674
    ruchir1674 over 6 years ago in reply to dubbie

    Thanks dubbie

     

    Happy that you liked it. Please share your blog when you make it changes are welcome.

     

    Regards

    Ruchir

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • dubbie
    dubbie over 6 years ago in reply to vimarsh_

    Ruchir,

     

    I completely agree with you, epic instructions. It must have taken many months to work all this out as I think it would take me several weeks just to follow them.

     

    Dubbie

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ruchir1674
    ruchir1674 over 6 years ago in reply to vimarsh_

    Thanks vimarsh_

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube