Introduction: Home Automation With Android and Arduino: Open the Gate When You Get Home

About: DIY lover. Sometimes Engineer.

This Instructable is about setting up an home automation system to be controlled via smartphone, using an internet connection, so that it can be accessed from everywhere you need it. Moreover, it will perform certain actions whenever a criteria is met (like, for example, turning on the light when the smartphone connects to home wifi network, opening the gate when you enter a GPS determined area, or everything else you might want).

An existing android application will be used, which requires the least amount of coding possible: just upload the code and you're done. The brain - the microcontroller - will be an Arduino, or Arduino-compatible board, such as an Aruino Uno with an Ethernet shield or a NodeMCU ESP8266.

To trigger the system when a condition is met (GPS position, time, ecc...) we will be using the notorious Tasker; more on that later.

In realizing the system the following key points have been held in mind:

  • It has to be cheap.
  • It has to be accessible from outside the local home network (i.e. your wifi).
  • It has to be REALLY easy and fast to build and set up.
  • It has to be reliable.

That being said, the whole project will cost about 20€ (7.50€ for an ESP8266, 8€ for a relay board, the remainder for additional hardware), and will take you about 30 mins to set it all up - not bad at all.

So, if you are interested, follow along this simple and fast guide, and set up you own!

Step 1: Setting Up the Hardware

Once you've gathered all your components, the first thing to do is to wire it all up.

In this example we will be hooking up a lightbulb to the ESP8266; keep in mind that you have to be extremely careful when working with main voltages - houses use 220V, that can kill you! Always cut the power before working on it, and if you are not confident search for an expert help!

That said: to handle such levels of tension and current (which will burn out the tiny ESPR8266) we need to use a suitable relay (like this which is 5V logic, suitable for the standard Arduino, or this, a 3.3V logic level relay, suitable for the 3.3V pinout of the ESP82666); the connections are pretty easy, follow the diagram we have attached.

Note that some relay boards (like the one we have linked) are ACTIVE LOW; this means you have to connect the relay to ground to have it turn on, and vice versa. If this is your case, Homotica offers a nice solution; we will see which in the next paragraph.

Step 2: ESP8266 Configuration

Now that we have set up the hardware of the automation system, we have to program the microcontroller.

In order to do that, we will need the Arduino software to upload the provided sketch to the ESP8266; so navigate to the Arduino Download Page and grab the version most suitable for you.

Once downloaded, install it.

Now that we have our IDE installed, we need the library needed for the sketch to work; to install it open the App Github Repo and choose Download from the green button on the right side.

Head to the Download folder on your PC, and using a software like WinRar or WinZip unzip the file; open the newly created folder "homotica-master" and copy the inner folder named "Homotica" to the Arduino Editor Libraries folder (you can usually find it under C:\Users\your_user_name\Documents\Arduino\libraries). Delete the remaining files in "homotica-master", we won't need them

One step is missing from uploading the code to the ESP8266: we need to obtain the library to have the Arduino IDE communicate with the board, for it's not officially supported.

To do that, follow these steps (credits: Github ES8266 Arduino Repo):

  • Start Arduino and open Files>Preferences window.
  • Enter "" (without quotes) into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
  • Open Boards Manager from Tools > Board menu and install esp8266 platform (search for "esp8266" and download "esp8266 by ESP8266 Community").

Everything is set up. Let's take care of the sketch code.

Open File>Example>Homotica>Homotica ESP8266, copy ALL the code in a new sketch and modify the following parameters:

  • ssid: insert here the name of your wireless network
  • password: your wifi password
  • ip, gateway, subnet: you probably won't have to touch these setting; change ip if you want your ESP8266 to have a different address
  • mUdpPort: the port that we will open in the router later on; if you don't know what it is, do not touch it
  • code: a unique 8-chars code that will be used to authenticate your app; you can choose whatever you want.

If you're using an ACTIVE LOW setup, do not forget to call homotica.setActiveLow() as shown in the example code!

To finish it up: add homotica.addUsedPin(5) (as shown in the example) to tell the ESP8266 which GPIO it has to handle, between the while loop and homotica.set(mUdpPort, code); delete all the homotica.simulateUdp(...) lines.

This is the code you should be left with:

#include <Homotica.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "mywifiname";           
const char* password = "wifipassword";
IPAddress ip(192, 168, 1, 20);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
unsigned int mUdpPort = 5858;
static String code = "aBc159";
Homotica homotica;

void setup() {
  WiFi.config(ip, gateway, subnet);
  WiFi.begin(ssid, password);
	while (WiFi.status() != WL_CONNECTED) {
  homotica.setActiveLow();  //<-- only if you need it
  homotica.set(mUdpPort, code);

void loop() {

When everything is correctly set up, change the compiler settings from the Tools menu according to the screenshot attached, and select the port your ESP8266 is connected on your computer in Tools>Port.

Now, click upload. Your microcontroller is all set up and ready to run!

Step 3: Router and IP

To communicate to the ESP8266 from every network, we will need to tell the router to let in the command we send to it.

To do that, navigate to your router configuration page (usually and look for something like "virtual server" or "port forwarding"; you can locate the precise setting for your router model searching in Google.

In port forwarding, create a new rule wich allows all connections to the ESP8266 (the one configured earlier) through the ESP8266 port (again, the one configured earlier). Name it Homotica, insert the ESP8266 IP in the IP field, and save.

If the router you're using does not allow you to do this, don't worry: you won't be able to use the app from the mobile network, but it will work perfectly fine from inside your home wifi.

Now, to refer to our router from the outside world we need to know its public IP; the downside is that the majority of internet provider do not give you a static IP, but a dynamic one instead, which changes over time.

But wait, we can solve this!

Head to NoIp, create a new account, then create a new hostname (see attached image). Take note of what hostname you have (in my example:, and proceed to the next step.

(Note: you might want your pc to automatically sync your router IP with your NoIp hostname: download their free app to do that)

Step 4: App Configuration

Let's take care of the app, shall we?

Download the app from the Play Store Page, and open it up.

Open the left menu, select Boards, and create a new one. We will fill in the parameters we previously defined in the ESP8266 sketch code:

  • Arduino Name: what you want (let this be ESP8266)
  • Host IP: this could be
    • the router static IP referenced via NoIp link -->
    • the ESP8266 IP if you want to use it only from inside your home wifi network -->
  • Host port: the one we set up and opened earlier --> 5858
  • Auth code: the 8-char code we defined in the sketch --> aBc195

Save. Open the menu again, create a new device; we will attach a lightbulb for demonstration:

  • Name: light
  • Button name: we will use On, choose what you prefer
  • Pin number: the pin to wich we attached the lightbulb. Be careful! On the ESP8266 the pin labels (D1, D2...) do NOT correspond to the GPIO Pin name! Search in Google to determine wich pin is which GPIO (in our example: pin 5 is labelled D1)
  • Behavior: you can chose between turning on, off, changing state of "pushing" (turning on then off) the device.

Save. If you have correctly set up everything until now, pressing On the lightbulb should turn on.

Cool, isn't it?

Now you can have fun adding more devices, even more boards, and arranging them in scenes.

But to use at full power what you just created, we will have to use Tasker.

Step 5: Tasker Integration

If you own Tasker, or want to buy it, keep reading! We will use it to tell Homotica what to do and when to do it.

In this example we're going to turn the light on whenever we connect to our home wifi AND the time is between 4pm and 6pm.

Open Tasker; create a new Task (lets name it Turn On Light), select Add>Plugin>Homotica, press the pencil incon to configure the plugin. Select Light>On and Save. Back to the main menu create a new Profile, select State>WiFi connected, insert tour Wifi name in the field SSID; press back and select Turn On Light as enter activity. Now, long press on the left part of the newly created profile, select Add>Time>From 4pm to 6pm, then close.

We are done. Our light will turn on when we enter home in the time we set.

That's handy!

Now it's your turn: get creative with Homotica and Tasker, and don't forget to show us what you created!

Microcontroller Contest

Participated in the
Microcontroller Contest