Using ESP8266 and MIT App Inventor to control a Relay | IoT

In home automation, wireless home automation (using Wi-Fi or internet connection to connect electrical appliances in your home) is becoming increasingly popular. This next project focuses on using ESP8266 (a low-cost Wi-Fi microchip) to control a Relay.

Project Overview

ESP8266 (ESP-01) is a small module that allows microcontrollers to connect to a Wi-Fi network and make simple IP (Internet Protocol) connections. It can be programmed by using Arduino, NodeMCU IDE or ESP8266 SDK. Several other modules like ESP-02, ESP-07 were also released. All these are essentially based on ESP8266, the only difference is the number of GPIO pins.

We will also use an app developed by MIT App Inventor through an Android device in this project. This app can be installed on your android device and then by connecting it to ESp8266, you can control a Relay through the same Wi-Fi connection your phone uses.

Project Schematic Diagram

The circuit diagram for the project “Control a Relay using ESP8266” is shown in the image below. You can see that I’ve used a single channel relay module in the circuit diagram.

Components Required

  • ESP8266
  • Arduino UNO
  • Resistors (1KΩ and 2.2KΩ) – both are ¼ Watt Resistors
  • Jumper Wires
  • Relay Module
  • Small 5V Bulb
  • Push Button
  • SPDT Switch
  • Android App
  • Android Phone
  • Computer with Internet

Components Description:

1: ESP8266:

ESP8266 (ESP-01) Wi-Fi module will be used to connect your Relay and to control it through your Wi-Fi network. The client will send instructions or commands through the Android App which the ESP8266 will receive and will carry out the operation. For more information Read: Learn How to Setup the Wifi Module ESP8266 by Using Just Arduino IDE

2: Relay:

The Relay module used in this project has two channels but only one channel shall be used. The relay module can be used with AC supply, however, in this project, we have used a small 5V bulb to show output. The Relay module used here is an Active LOW one.

Circuit Diagram:

The ESP8266 ESP-01 module had 8 pins. These are VCC, GND, TX, RX, RST, CH_PD, GPIO0 and GPIO2. Connect the VCC and GND to 3.3V (the ESP-01 module is not compatible with 5V so 3.3V is preferred). The rest of the connections are made accordingly (GND to GND, TX pin to TX of Arduino). The only difference is the GPIO2 pin of is connected to the INPUT of Relay.

Source Code

Project Source Code is very clean and easy to understand but you need to do some minor changes before uploading your source code into a module. First you need to Add your wifi router Name and password in place of “SSID” and “Password” respectively. The most important thing is the allocation of unused Static IP Address to ESP8266 module, in our code we have used  “192,168,1,254″ as our Static IP. Good, If this ip works for you otherwise use different IP address. 

#include <ESP8266WiFi.h>

const char* ssid = "SSID";//type your ssid
const char* password = "PASSWORD";//type your password

int relayPin = 2; // GPIO2 of ESP8266
WiFiServer ESPserver(80);//Service Port

void setup() 
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);

Serial.print("Connecting to: ");

WiFi.begin(ssid, password);

 The following four line of the 
 code will assign a Static IP Address to 
 the ESP Module. If you do not want this, 
 comment out the following four lines.  

IPAddress ip(192,168,1,254);   
IPAddress gateway(192,168,1,1);   
IPAddress subnet(255,255,255,0);   
WiFi.config(ip, gateway, subnet);

while (WiFi.status() != WL_CONNECTED) 
Serial.println("WiFi connected");

// Start the server
Serial.println("Server started");

// Print the IP address
Serial.print("The URL to control ESP8266: ");

void loop() 
// Check if a client has connected
WiFiClient client = ESPserver.available();
if (!client) 

// Wait until the client sends some data
Serial.println("New Client");

// Read the first line of the request
String request = client.readStringUntil('\r');

// Match the request

int value = LOW;
if (request.indexOf("/RELAYON") != -1) 
Serial.println("LAMP is ON");
digitalWrite(relayPin, LOW);
value = LOW;
if (request.indexOf("/RELAYOFF") != -1)
Serial.println("LAMP is OFF");
digitalWrite(relayPin, HIGH);
value = HIGH;

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); //  IMPORTANT
client.println("<!DOCTYPE HTML>");

client.print("Status of the Lamp: ");

if(value == LOW) 

Serial.println("Client disconnected");

Android App using MIT App Inventor:

For using the MIT App Inventor, you are required to link your Google account to the App Developer Application Next, create a new project by selecting “Start a New Project” A new window shall open which will show different layout objects like Buttons, Sliders, and Textbox etc.

Create an interface that consists of three buttons (we have used “Lamp ON”, “Lamp OFF”, and “EXIT”), a few Labels and a Web Component. Arrange all components and app layout. In the top right corner switch to Blocks section.

Here, create blocks and enter your static IP Address (a permanent number assigned to a computer by an ISP) into the URL section of the block as shown in the given image.  Next, debug the App directly from the Browser and your Android. Then install the APK file on your computer and Android. You can open and use Android Apk file in computer by using BlueStack. 

NOTE: To download .apk file, go to Build and select App (save .apk to my computer).


After setting your circuit design, upload the program to the ESP8266 module. Once, you upload the program, you will receive a confirmation message for the IP address and Wi-Fi connection.

Now, you can test your program. Open the Android App installed on your device. When you choose the “Lamp ON” button, the Relay gets a LOW Logic signal and the lamp is turned ON. Similarly, when you push the “Lamp OFF” button, the Lamp is turned OFF.

NOTE: The Relay Module used in this project is an Active LOW one.


This project focuses on the applications of ESP8266 in controlling a Relay system through Wi-Fi connection using Android App developed by MIT App Inventor. ESP8266 is capable of hosting applications and is trending in the IoT. You can program ESP8266 like any other microcontroller and use a Wi-Fi connection to control it. You can connect to the Internet, host a web server, and connect your smartphone to it.  You can make your own projects and use them in home automation systems. Not only that, with ESP8266 you can control your relay from anywhere in the world.

Next Article: IoT Voice Activated Home Automation Project by using Arduino For Smart Home

Related posts

Arduino Opta PLC Pros & Cons

Breakthrough of Ardino-Pro Opta: Micro-PLC with industrial IoT proficiency

Top Arduino sensors – the ultimate list 2021


Zack November 29, 2020 - 9:12 pm

Hi, can you send me the proteus library for the esp8266-01?

William Forrest January 11, 2023 - 1:14 pm

I want to recieve text back from the ESP8266 giving me a message the Relay is ON. This project does not seem to do that. Or at least I do not get a response back on my anroide display saying that. am I overlooking something?? I am New to MIT App Inventor.

Add Comment