Creating Smart Home Devices using IFTTT and Arduino

Rahul Tarak
Etwas
Published in
6 min readJul 20, 2019

In the following section, I am going to demonstrate how to automate lights, fans, air conditioners, any electronic for really cheap with barely any code and minimal electrical engineering background.

For the scope of this tutorial, I will be covering how to automate a regular light bulb.

Components

  1. MicroController, Any Micro Controller should do but for this tutorial, I will be using an ESP based Wesmos D1 Variant of the Arduino, this allows for direct WIFI integration but you could also just get an Arduino UNO and an ESP8266 chip.
  2. Relay, Now the configuration of the relay you get depends on the number of electronics you want to automate and their current draws. However, in this case, 1 single-channel 5v relay should do.
  3. Jumper Cables, I’d recommend getting a pack of 120 of Amazon or eBay they come pretty cheap.
  4. Copper Wire, make sure to get enough distance for the wire to reach from the light bulb to the relay.

These are the core components for the system but I’d recommend getting a google home or amazon Alexa to add to the easy of the system.

Configuring your Assistant

Let’s start with the easy part of the project first. For this solution you need the If This Then That App(IFTTT) which is available on both IOS and Android.

You will also have to create an account on AdaFruit. Once you create your AdaFruit account, create a new dashboard as shown below

Create AdaFruit Dashboard

The name of this dashboard can be anything, once the dashboard is created follow the below gif to create your block.

Creating a new block

It is important to remember to set the ON value to 1 and off Value to 0, this will come in handy later in the project.

Now just toggle the switch a couple of times to ensure it works.

Now it is time to switch to IFTTT on your phone. Once IFTTT is open, navigate to my applets section and create a new applet.

I prefer to use google assistant, so I am a demonstration with google assistant but it should work the same with Amazon Alexa.

Setting Trigger Phrase
Setting the data sent to AdaFruit and Verifying the Applet

Important note: At least with google assistants integration with IFTTT, the trigger phrase has to be exactly said for the desired outcome so I’d recommend creating a routine which similar phrases on the google home app which trigger this phrase.

As shown about the data sent to AdaFruit was 1, if you wanted to turn the light off you set that data to 0.

Finally, use your assistant of choice on your device of choice and verify that the switch on adafruit gets toggled.

Wiring and Designing the Circuit

Word of Caution: While you don’t have to cut any main power lines or even touch the phase line, you are still interacting with the main AC power connection and must take precautions to ensure there are no injuries

The circuit essentially works like an AND gate for the switch so when both the relay and the physical switch are on, the light is on and otherwise, it is off. This allows you as the user to use the physical switch anytime there is a glitch with the solution.

The following is a link to a detail explanation on the working of the 5v relay and would be worth a read if you have the time, but if you don’t just follow the below circuit diagram.

Circuit Diagram, the blurred out section is a Motion sensor and I blurred it out not to confuse anyone

You can connect the green cable to any of the Digital Pins, as shown above, just make sure to note this number as it will have to be modified in code later.

So far you have configured your smart assistance and wired your light/s now just a bit of code and then you should be done.

Code

Note: If using the Wesmos D1 for this project, follow this link to add the board to the Arduino IDE

There is very minimal programming required for this project and if you copy the below source code you will at maximum have to change two or three lines to get the system working.

The below code is based on the MQTT Library and AdaFruit’s Implementation for the ESP8266, this will work on both boards the Arduino Uno and Wemos D1 but there will be minor differences that I will point out as they arise.

The entire code required for this project can be found below or at https://gist.github.com/CryogenicPlanet/dc8b24d30f0c13f6a0a63ed2647419dd

Most of this code doesn’t need to be edited and can be directly pasted.

However, the following lines do

Line 25

int relayInp1 = D13;

This line refers to the pin at which your relay is connected and will have to be changed based on which pin you connected to. If you have multiple devices just define multiple relayInputs.

Note: The above syntax of D13 is for the Wemos D1

Of Course, you need to change your wifi credentials shown on the left(Lines 31, 32)

#define WLAN_SSID       "" //Wifi Name
#define WLAN_PASS "" // Wifi Password

Same for your AdaFruit Credentials(Lines 40,41)

#define AIO_USERNAME    "" // AdaFruit Usernmae
#define AIO_KEY ""// AdaFruit Key
Get AdaFruit Key
Adafruit_MQTT_Subscribe mediumTutorial= Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/mediumTutorial");

This line will have to be duplicated for multiple fields and the URL will have to change based on the feed you set in AdaFruit(/feeds/${varName})

Don’t forget to activate your pins as shown below

pinMode(relayInp1, OUTPUT);

Similarly, you have to activate your AfaFruit Subscriptions also for them to listen

mqtt.subscribe(&mediumTutorial);

Finally, controlling the light itself

if (subscription == &mediumTutorial) {
Serial.print(F("Got: "));
char * temp = (char *)mediumTutorial.lastread;
Serial.println(temp);
uint16_t state = atoi(temp);
if (state == 1){
Serial.println("Switching mediumTutorial on ...");
digitalWrite(relayInp1,HIGH);
} else {
digitalWrite(relayInp1,LOW);
}
}

Change the variable names of &mediumTutorial passed on what you set above.

digitalWrite(relayInp1,HIGH)

The above line turns on the pin which tells the relay to turn on the light.

That’s it your done, and you don’t have to spend a fortune buying Phillip hue lights to have a set of automated lights at home.

Note: This same technique works for any electronics but higher current drawing appliances like Air Conditioners need a modified circuit

Demo

If all the above steps are correctly followed and there are no errors, your final product should look something like this.

If you have any questions or queries leave them down below or on the gist, I will try and answer them.

--

--