Wifi + Voice Controlled Home Automation with ESP8266 & Android
This post is all about Wifi & Voice Controlled Home Automation Using NodeMCU & Android. Here we will do the same, but instead of using Alexa, we will develop our own App on an Android smartphone to control our locally (using buttons or voice), our home devices.
In this project, we will be an interfacing 4-channel relay with NodeMCU ESP8266 12E Wifi Module and control 4 different LEDs. These 4 different LEDs are similar to 4 different Home Appliances. We will use an Android app having 5+5 ON+OFF Buttons to send a signal to NodeMCU in order to control relay output. Every NodeMCU has a particular IP Address. We will assign this IP Address to Android App for access control. Similarly, NodeMCU is needed to be connected to local Wifi.
But before that, you can check some of our Home Automation Projects made using ESP8266 or ESP32:
1. Home Automation using NodeMCU & Alexa
2. Home Automation with Arduino IoT Cloud using ESP32
3. Home Automation using ESP8266 WebServer
4. Home Automation using Blynk & NodeMCU
5. Home Automation using Google Firebase & NodeMCU
6. Industrial Automation using ESP8266 & AWS IoT Core
Bill of Materials
S.N. | Components Name | Quantity | Purchase Links |
---|---|---|---|
1 | NodeMCU ESP8266 Board | 1 | Amazon | AliExpress |
2 | 4 Channel Relay Module | 1 | Amazon | AliExpress |
3 | LED 5mm any Color | 4 | Amazon | AliExpress |
4 | Resistor 220-ohm | 4 | Amazon | AliExpress |
5 | 5V Power Supply | 1 | Amazon | AliExpress |
6 | Connecting Wires | 10 | Amazon | AliExpress |
7 | Breadboard | 1 | Amazon | AliExpress |
Block Diagram:
Here is a block diagram for this project “Wifi & Voice Controlled Home Automation Using ESP8266”. It is simple and easy to understand.
NodeMCU is interfaced with 4 channel Relay using GPIO Pins of NodeMCU. The output pin of the NodeMCU is connected to 4 different LEDs via a 220-ohm resistor. The NodeMCU is connected to Local Wifi and is capable of receiving a signal via the Internet.
The Android Device has a “Home Automation Control” Android app installed on it. To control the NodeMCU input/output, the IP address of NodeMCU is entered on the Android App IP box.
Once this setup is done, you can control the Android App staying in any part of the world. Your Android Phone acts as a remote and NodeMCU as a receiver and signal are transferred via the Internet.
Circuit: Wifi & Voice Controlled Home Automation Using ESP8266
We will use a 4 Channel relay module to control 4 LEDs, simulating 4 home devices.
Connect Relay Input Pin with NodeMCU as follows
1 2 3 4 |
NodeMCU GPIO Pin 1 - Relay Input Pin 1 NodeMCU GPIO Pin 3 - Relay Input Pin 2 NodeMCU GPIO Pin 12 - Relay Input Pin 3 NodeMCU GPIO Pin 14 - Relay Input Pin 4 |
Similarly, Connect the 4 output to 4 different LED via 4 resistors as shown in the figure above.
Home Automation PCB & Gerber File
If you don’t want to assemble the circuit on a breadboard and you want PCB for the project, then here is the PCB for you. The PCB Board for the Home Automation Project is designed using EasyEDA online Circuit Schematics & PCB designing tool. The front side and back side of the PCB is given below.
The Gerber File for the PCB Board is given below. You can simply download the Gerber File.
The Android App for Wifi & Voice Controlling:
The Android App will send a string that must be interpreted by the code to activate each one of the relays defined as follows:
Relay1:
Turn-On: “r1on”;
Turn-Off: “r1off”;
Relay2:
Turn-On: “r2on”;
Turn-Off: “r2off”
Relay3:
Turn-On: “r3on”;
Turn-Off: “r3off”
Relay4:
Turn-On: “r4on”;
Turn-Off: “r4off”
If the Android App sends as a command: “r1on”, Relay1 must be turn on. We have also defined “group commands” to turn-on (“allon”) and turn-off (“all off”) simultaneity all devices. Similarly, the voice input image is also given which when clicked gives a pop up for Google Assistant to Accept Voice Command.
Creating The Android App Using MIT App Inventor:
This step is optional. I have given the link below to download the Android app directly. I have also added a .aia link that can directly be downloaded and modified according to your need.
To create Android App Visit http://ai2.appinventor.mit.edu/ and create a project.
Main Components on Screen are these as shown in the figure above:
The internal button contains an on-off switch for Relay as well as voice input command as shown in the figure below. You can watch the video below for more detailed information.
Android App Download Link:
If you are a beginner and you don’t want to make any Android App, you can simply download the App from the link below.
Code for Finding the Device IP Address:
Simply copy this code and upload it to Arduino IDE. After Code is uploaded simply click on Serial Monitor and get the IP. Copy this IP as it is required for your Android App.
NOTE: Please Disconnect Relay Power Pin while uploading the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <ESP8266WiFi.h> WiFiClient client; WiFiServer server(80); const char* ssid = "how2electronics"; const char* password = "12345678"; void setup() { Serial.begin(115200); connectWiFi(); server.begin(); } void loop() { } /* connecting WiFi */ void connectWiFi() { Serial.println("Connecting to WIFI"); WiFi.begin(ssid, password); while ((!(WiFi.status() == WL_CONNECTED))) { delay(300); Serial.print(".."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("NodeMCU Local IP is : "); Serial.print((WiFi.localIP())); } |
Source Code/Program:
Here is a source code for Wifi & Voice-Controlled Home Automation Using NodeMCU & Android. Compile and upload this code to your NodeMCU board.
WARNING: Please Disconnect Relay Power Pin while uploading the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#include <ESP8266WiFi.h> WiFiClient client; WiFiServer server(80); const char* ssid = "how2electronics"; const char* password = "alhabibi"; String command =""; // Command received from Android device // Set Relay Pins int relay1 = 1; int relay2 = 3; int relay3 = 12; int relay4 = 14; void setup() { Serial.begin(115200); pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); digitalWrite(relay1,HIGH); digitalWrite(relay2,HIGH); digitalWrite(relay3,HIGH); digitalWrite(relay4,HIGH); connectWiFi(); server.begin(); } void loop() { client = server.available(); if (!client) return; command = checkClient (); if (command == "r1on" || command == "turn on relay 1" || command == "relay 1 on") digitalWrite(relay1,0); else if (command == "r1off" || command == "turn off relay 1" || command == "relay 1 off") digitalWrite(relay1,1); else if (command == "r2on" || command == "turn on relay 2" || command == "relay 2 on") digitalWrite(relay2,0); else if (command == "r2off" || command == "turn off relay 2" || command == "relay 2 off") digitalWrite(relay2,1); else if (command == "r3on" || command == "turn on relay 3" || command == "relay 3 on") digitalWrite(relay3,0); else if (command == "r3off" || command == "turn off relay 3" || command == "relay 3 off") digitalWrite(relay3,1); else if (command == "r4on" || command == "turn on relay 4" || command == "relay 4 on") digitalWrite(relay4,0); else if (command == "r4off" || command == "turn off relay 4" || command == "relay 4 off") digitalWrite(relay4,1); else if (command == "allon" || command == "Turn on all devices" || command == "all on") { digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); digitalWrite(relay3,LOW); digitalWrite(relay4,LOW); } else if (command == "alloff" || command == "Turn off all devices" || command == "all off") { digitalWrite(relay1,HIGH); digitalWrite(relay2,HIGH); digitalWrite(relay3,HIGH); digitalWrite(relay4,HIGH); } sendBackEcho(command); // send command echo back to android device command = ""; } /* connecting WiFi */ void connectWiFi() { Serial.println("Connecting to WIFI"); WiFi.begin(ssid, password); while ((!(WiFi.status() == WL_CONNECTED))) { delay(300); Serial.print(".."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("NodeMCU Local IP is : "); Serial.print((WiFi.localIP())); } /* check command received from Android Device */ String checkClient (void) { while(!client.available()) delay(1); String request = client.readStringUntil('\r'); request.remove(0, 5); request.remove(request.length()-9,9); return request; } /* send command echo back to android device */ void sendBackEcho(String echo) { client.println("HTTP/1.1 200 OK "); client.println("Content-Type: text/html"); client.println(""); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println(echo); client.println("</html>"); client.stop(); delay(1); } |
17 Comments
The Voice Control is Not Working please update the code
is the voice control not working ?
Android App Download Link: not working pls give new link
Download .aia file directly and export to MIT app inventor and then you can generate android app from there.
how to extend the device to control 8 relays. which pins can be used?
I can’t control the relay via relay. help me.
The Voice Control is Not Working 😦
How if use module esp2866-01 anything to add or change in source code ?
is ther any solution for the voice control please update if there’s any way to make the voice control working i need it so badly for my final year project . please help.
Thanks for this great tutorial. Its worth trying but the Android App is not downloadable. Can you please provide a better link or guide me in downloading it. Or better still you can send to my mail [email protected]. Thanks in advance.
Voice control is not working..
When we upload the code shall we have to remove all the power pin since one board get damage
So many viewers has complaint regarding voice which is not triggering.
Have you updated the app code OR something to resolve their issues?
I have found out that http does not recognize blank spaces. It replaces them with %20. So you have to replace the blank spaces in every command with %20. Like this….
if (command == “r1on” || command == “turn%20on%20relay%201” || command == “turn%20on%20relay%20one”)
digitalWrite(relay1, HIGH);
else if (command == “r1off” || command == “turn%20off%20relay%20one” || command == “relay%202%20on”)
digitalWrite(relay1, LOW);
else if (command == “r2on” || command == “turn%20on%20relay%202” || command == “relay%202%20on”)
digitalWrite(relay2, HIGH);
else if (command == “r2off” || command == “turn%20off%20relay%202” || command == “relay%202%20off”)
digitalWrite(relay2, LOW);
else if (command == “r3on” || command == “turn%20on%20relay%203” || command == “relay%203%20on”)
digitalWrite(relay3, HIGH);
else if (command == “r3off” || command == “turn%20off%20relay%203” || command == “relay%203%20off”)
digitalWrite(relay3, LOW);
else if (command == “r4on” || command == “turn%20on%20relay%204” || command == “relay%204%20on”)
digitalWrite(relay4, HIGH);
else if (command == “r4off” || command == “turn%20off%20relay%204” || command == “relay%204%20off”)
digitalWrite(relay4, LOW);
else if (command == “allon” || command == “turn%20on%20all%20devices” || command == “all%20on”)
{
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
}
else if (command == “alloff” || command == “turn%20off%20all%20devices” || command == “all%20off”)
{
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
}
Great job Alex. Your projects helped me in my final year
Change the if statements in the Arduino code to the code portions below:
if (command == “r1on” || command == “turn%20on%20relay%201” || command == “turn%20on%20relay%20one”)
digitalWrite(relay1, HIGH);
else if (command == “r1off” || command == “turn%20off%20relay%20one” || command == “relay%202%20on”)
digitalWrite(relay1, LOW);
else if (command == “r2on” || command == “turn%20on%20relay%202” || command == “relay%202%20on”)
digitalWrite(relay2, HIGH);
else if (command == “r2off” || command == “turn%20off%20relay%202” || command == “relay%202%20off”)
digitalWrite(relay2, LOW);
else if (command == “r3on” || command == “turn%20on%20relay%203” || command == “relay%203%20on”)
digitalWrite(relay3, HIGH);
else if (command == “r3off” || command == “turn%20off%20relay%203” || command == “relay%203%20off”)
digitalWrite(relay3, LOW);
else if (command == “r4on” || command == “turn%20on%20relay%204” || command == “relay%204%20on”)
digitalWrite(relay4, HIGH);
else if (command == “r4off” || command == “turn%20off%20relay%204” || command == “relay%204%20off”)
digitalWrite(relay4, LOW);
else if (command == “allon” || command == “turn%20on%20all%20devices” || command == “all%20on”)
{
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
}
else if (command == “alloff” || command == “turn%20off%20all%20devices” || command == “all%20off”)
{
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
}
I found out that http replaces blank spaces with “%20”. I tried it and it worked perfectly.
Thanks Alex. Your works have been a blessing to my final year project.
Change the if statements in the Arduino code to the code portions below:
if (command == “r1on” || command == “turn%20on%20relay%201” || command == “turn%20on%20relay%20one”)
digitalWrite(relay1, HIGH);
else if (command == “r1off” || command == “turn%20off%20relay%20one” || command == “relay%202%20on”)
digitalWrite(relay1, LOW);
else if (command == “r2on” || command == “turn%20on%20relay%202” || command == “relay%202%20on”)
digitalWrite(relay2, HIGH);
else if (command == “r2off” || command == “turn%20off%20relay%202” || command == “relay%202%20off”)
digitalWrite(relay2, LOW);
else if (command == “r3on” || command == “turn%20on%20relay%203” || command == “relay%203%20on”)
digitalWrite(relay3, HIGH);
else if (command == “r3off” || command == “turn%20off%20relay%203” || command == “relay%203%20off”)
digitalWrite(relay3, LOW);
else if (command == “r4on” || command == “turn%20on%20relay%204” || command == “relay%204%20on”)
digitalWrite(relay4, HIGH);
else if (command == “r4off” || command == “turn%20off%20relay%204” || command == “relay%204%20off”)
digitalWrite(relay4, LOW);
else if (command == “allon” || command == “turn%20on%20all%20devices” || command == “all%20on”)
{
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
}
else if (command == “alloff” || command == “turn%20off%20all%20devices” || command == “all%20off”)
{
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
}
I found out that http replaces blank spaces with “%20”. I tried it and it worked perfectly.
Leave everything in the Android code the same
Thanks Alex. Your works have been a blessing to my final year project
what do we have to with the ip address to connect the app to nodemcu because after all the steps my app is still not working