Subhajit Das
Published © GPL3+

Home Automation Using Arduino and WiFi Module ESP01

Control 5 home appliances from the manual switches, smartphone, IR remote. Also, control relays with room temperature and ambient light.

IntermediateFull instructions provided16 hours3,412
Home Automation Using Arduino and WiFi Module ESP01

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
LDR, 1 Mohm
LDR, 1 Mohm
×1
4-CHANNEL RELAY CONTROLLER FOR I2C
ControlEverything.com 4-CHANNEL RELAY CONTROLLER FOR I2C
×1

Software apps and online services

Arduino IDE
Arduino IDE
Blynk
Blynk

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit for Smart Relay V5 with Arduino ESP01

Circuit for Smart Relay V5 with Arduino ESP01

Code

Code_Arduino_OLED0.96_SmartRelay_V5.ino

Arduino
This is the code for Arduino (Atmega328P)
/**********************************************************************************
 *  Code for Arduino. (Smart Relay V5)
 *  TITLE: Arduino ESP8266 control Relay Module with Blynk, IR remote, Manual Switch, DHT11 Sensor and LDR| Real-Time Feedback
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/mZrQpLq0KMk
 *  Related Blog : https://easyelectronicsproject.com/projects/arduino-projects/
 *  by Tech StudyCell
 *  
 *  Download the libraries
 *  https://github.com/contrem/arduino-timer
 *  https://github.com/z3t0/Arduino-IRremote
 *  https://github.com/adafruit/DHT-sensor-library
 *  https://github.com/adafruit/Adafruit_SSD1306
 **********************************************************************************/

#include <Adafruit_SSD1306.h>
#include <DHT.h>
#include <IRremote.h>
#include <arduino-timer.h>

auto timer = timer_create_default(); // create a timer with default settings

//Auto Control settings
const float maxTemp = 32.5;
const float minTemp = 31.7;
const int ldrOn = 800;
const int ldrOff = 950;

//Define PIN constant
const int RELAY_PIN_1 = 2;
const int RELAY_PIN_2 = 3;
const int RELAY_PIN_3 = 4;
const int RELAY_PIN_4 = 5;
const int RELAY_PIN_5 = 6;

const int mswitch_1 = 7;
const int mswitch_2 = 8;
const int mswitch_3 = 9;
const int mswitch_4 = 10;
const int mswitch_5 = 11;
const int cmode = 12;
const int smode = 13;
int RECV_PIN = A0; // Pin connected to the IR Receiver
int LDR_PIN = A2;  // Pin connected to the LDR 
int DHTPIN  = A1;  // Pin connected to the DHT sensor 

// Uncomment the type of sensor in use:
#define DHTTYPE    DHT11     // DHT 11

//Define IR receiver and Result Objects
IRrecv irrecv(RECV_PIN);
decode_results results;


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


DHT dht(DHTPIN, DHTTYPE);

char toggleState_1 = 'a'; //Define integer to remember the toggle state for switch 1
char toggleState_2 = 'b'; //Define integer to remember the toggle state for switch 2
char toggleState_3 = 'c'; //Define integer to remember the toggle state for switch 3
char toggleState_4 = 'd'; //Define integer to remember the toggle state for switch 4
char toggleState_5 = 'e'; //Define integer to remember the toggle state for switch 5

String pinStatus = "abcde000.0000.00";

int switchMode = 0;
int ModeFlag = 0;
float temperatureRead;
float humidityRead;
float ldrVal;
String displayText;

void relayOnOff(int relay){

    switch(relay){
      case 1: 
             if(toggleState_1 == 'a'){
              digitalWrite(RELAY_PIN_1, HIGH); // turn on relay 1
              toggleState_1 = 'A';
              }
             else{
              digitalWrite(RELAY_PIN_1, LOW); // turn off relay 1
              toggleState_1 = 'a';
              }
      break;
      case 2: 
             if(toggleState_2 == 'b'){
              digitalWrite(RELAY_PIN_2, HIGH); // turn on relay 2
              toggleState_2 = 'B';
              }
             else{
              digitalWrite(RELAY_PIN_2, LOW); // turn off relay 2
              toggleState_2 = 'b';
              }
      break;
      case 3: 
             if(toggleState_3 == 'c'){
              digitalWrite(RELAY_PIN_3, HIGH); // turn on relay 3
              toggleState_3 = 'C';
              }
             else{
              digitalWrite(RELAY_PIN_3, LOW); // turn off relay 3
              toggleState_3 = 'c';
              }
      break;
      case 4: 
             if(toggleState_4 == 'd'){
              digitalWrite(RELAY_PIN_4, HIGH); // turn on relay 4
              toggleState_4 = 'D';
              }
             else{
              digitalWrite(RELAY_PIN_4, LOW); // turn off relay 4
              toggleState_4 = 'd';
              }
      break;
      case 5: 
             if(toggleState_5 == 'e'){
              digitalWrite(RELAY_PIN_5, HIGH); // turn on relay 5
              toggleState_5 = 'E';
              }
             else{
              digitalWrite(RELAY_PIN_5, LOW); // turn off relay 5
              toggleState_5 = 'e';
              }
      break;
      default : break;      
      } 
      delay(200);
}
void changeMode(){      
  delay(200);
  if (switchMode == 0){
    switchMode = 1;
  }
  else if (switchMode == 1) {
    switchMode = 0;
  }
}
String modeDecode(int count){
  if (count == 0){
    return " Manual Mode ";
  }
  else if (count == 1){
    return " Auto Mode ";
  }
}

void readSensor(){
    
  ldrVal = analogRead(LDR_PIN);
  
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  
  if (isnan(h) || isnan(t)) {
    //Serial.println("Failed to read from DHT sensor!");
    humidityRead = 0;
    temperatureRead = 0;
    return;
  }
  else {
    humidityRead = h;
    temperatureRead = t;
  }  
}

void displayData(){
  readSensor();
  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(8,2);
  display.print(temperatureRead);
  display.print(" ");
  display.print("C    ");
  display.print(humidityRead); // display humidity
  display.print(" %");
  
  display.drawLine(0,18, display.width()-1,18, WHITE);
   
  display.setCursor(15,24);
  display.print(modeDecode(switchMode));   
  display.display();
}

void sendStatus(){  
  displayData();
  Serial.print(pinStatus);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  irrecv.enableIRIn(); // Enable the IR receiver
  
  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(RELAY_PIN_2, OUTPUT);
  pinMode(RELAY_PIN_3, OUTPUT);
  pinMode(RELAY_PIN_4, OUTPUT);
  pinMode(RELAY_PIN_5, OUTPUT);

  pinMode(smode, INPUT);
  pinMode(cmode, INPUT);

  pinMode(mswitch_1, INPUT);
  pinMode(mswitch_2, INPUT);
  pinMode(mswitch_3, INPUT);
  pinMode(mswitch_4, INPUT);
  pinMode(mswitch_5, INPUT);
 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Address 0x3C for 128x32
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.clearDisplay(); // Clear display buffer
  dht.begin();

  // call the toggle_led function every 2000 millis (2 seconds)
  timer.every(2000, sendStatus);  
}

void loop() {
  
  if (digitalRead(cmode) == HIGH){ //change the Mode (Manual or Auto)
    changeMode();
  }
  else if (digitalRead(smode) == HIGH){
    digitalWrite(RELAY_PIN_1, LOW);  //Turn Off Relay 1
    toggleState_1 = 'a';
    digitalWrite(RELAY_PIN_2, LOW);  //Turn Off Relay 2
    toggleState_2 = 'b';
    digitalWrite(RELAY_PIN_3, LOW);  //Turn Off Relay 3
    toggleState_3 = 'c';
    digitalWrite(RELAY_PIN_4, LOW);  //Turn Off Relay 4
    toggleState_4 = 'd';
    digitalWrite(RELAY_PIN_5, LOW);  //Turn Off Relay 5
    toggleState_5 = 'e';
    switchMode = 0;
    pinStatus = String(toggleState_1) + String(toggleState_2) + String(toggleState_3) + String(toggleState_4) + String(toggleState_5) + String(switchMode) + String(temperatureRead) + String(humidityRead);
  }     
  else{

    if (switchMode == 0){  // Manual Mode
      //Push Button Control
      if (digitalRead(mswitch_1) == HIGH){
        delay(200);
        relayOnOff(1);      
      }
      else if (digitalRead(mswitch_2) == HIGH){
        delay(200);
        relayOnOff(2);
      }
      else if (digitalRead(mswitch_3) == HIGH){
        delay(200);
        relayOnOff(3);
      }
      else if (digitalRead(mswitch_4) == HIGH){
        delay(200);
        relayOnOff(4);
      }
      else if (digitalRead(mswitch_5) == HIGH){
        delay(200);
        relayOnOff(5);
      }
   //WiFi Control
      if(Serial.available()>0)
      {     
        char c_val= Serial.read(); // reading the data received from the bluetooth module
        switch(c_val)
        {
          case 'A': digitalWrite(RELAY_PIN_1, HIGH);  toggleState_1 = 'A'; break; // when A is pressed on the app Turn on Pin 2
          case 'a': digitalWrite(RELAY_PIN_1, LOW);   toggleState_1 = 'a'; break; // when a is pressed on the app Turn off Pin 2
          case 'B': digitalWrite(RELAY_PIN_2, HIGH);  toggleState_2 = 'B'; break; // when B is pressed on the app Turn on Pin 3
          case 'b': digitalWrite(RELAY_PIN_2, LOW);   toggleState_2 = 'b'; break; // when b is pressed on the app Turn off Pin 3
          case 'C': digitalWrite(RELAY_PIN_3, HIGH);  toggleState_3 = 'C'; break; // when C is pressed on the app Turn on Pin 4
          case 'c': digitalWrite(RELAY_PIN_3, LOW);   toggleState_3 = 'c'; break; // when c is pressed on the app Turn off Pin 4
          case 'D': digitalWrite(RELAY_PIN_4, HIGH);  toggleState_4 = 'D'; break; // when D is pressed on the app Turn on Pin 5
          case 'd': digitalWrite(RELAY_PIN_4, LOW);   toggleState_4 = 'd'; break; // when d is pressed on the app Turn off Pin 5
          case 'E': digitalWrite(RELAY_PIN_5, HIGH);  toggleState_5 = 'E'; break; // when E is pressed on the app Turn on Pin 6
          case 'e': digitalWrite(RELAY_PIN_5, LOW);   toggleState_5 = 'e'; break; // when e is pressed on the app Turn off Pin 6
          default : break;
        }
        delay(100);
      }

     //IR Control
     if (irrecv.decode(&results)) {
      switch(results.value){
        case 0x80BF49B6: relayOnOff(1); break;
        case 0x80BFC936: relayOnOff(2); break;
        case 0x80BF33CC: relayOnOff(3); break;
        case 0x80BF718E: relayOnOff(4); break;
        case 0x80BFF10E: relayOnOff(5); break;
        default : break;      
        }
      irrecv.resume(); // Receive the next value
      }
    }
    else if (switchMode == 1){  //Auto Mode
     // Serial.println(ldrVal);
      //LDR Control
      if (ldrVal < ldrOn){
          if(toggleState_3 == 'c'){
            digitalWrite(RELAY_PIN_3, HIGH); // turn on relay 3
            toggleState_3 = 'C';
          }
          if(toggleState_4 == 'd'){
            digitalWrite(RELAY_PIN_4, HIGH); // turn on relay 4
            toggleState_4 = 'D';
          }
      }
      else if (ldrVal >= ldrOff){
        if(toggleState_3 == 'C'){
            digitalWrite(RELAY_PIN_3, LOW); // turn off relay 3
            toggleState_3 = 'c';
          }
        if(toggleState_4 == 'D'){
            digitalWrite(RELAY_PIN_4, LOW); // turn off relay 4
            toggleState_4 = 'd';
          }
      }
      //DHT11 Control
      if (temperatureRead >= maxTemp){
        if(toggleState_1 == 'a'){
          digitalWrite(RELAY_PIN_1, HIGH); // turn on relay 1
          toggleState_1 = 'A';
        }
        else if(toggleState_2 == 'b'){
          digitalWrite(RELAY_PIN_2, HIGH); // turn on relay 2
          toggleState_2 = 'B';
        }
      }
      else if (temperatureRead < minTemp){
        if(toggleState_1 == 'A'){
            digitalWrite(RELAY_PIN_1, LOW); // turn on relay 1
            toggleState_1 = 'a';
          }
        else if(toggleState_2 == 'B'){
            digitalWrite(RELAY_PIN_2, LOW); // turn on relay 1
            toggleState_2 = 'b';
         }
       }
     }
     pinStatus = String(toggleState_1) + String(toggleState_2) + String(toggleState_3) + String(toggleState_4) + String(toggleState_5) + String(switchMode) + String(temperatureRead) + String(humidityRead);
   }  
  timer.tick(); // tick the timer
}

Code_ESP8266_SmartRelay_V5.ino

Arduino
This is the code for ESP8266 (ESP01)
/**********************************************************************************
 *  Code for ESP8266 (ESP01). (Smart Relay V5)
 *  TITLE: Arduino ESP8266 control Relay Module with Blynk, IR remote, Manual Switch, DHT11 Sensor and LDR| Real-Time Feedback
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/mZrQpLq0KMk
 *  Related Blog : https://easyelectronicsproject.com/projects/arduino-projects/
 *  by Tech StudyCell
 **********************************************************************************/
/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

 *************************************************************
  Note: This requires ESP8266 support package:
    https://github.com/esp8266/Arduino

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
 *************************************************************/

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int pinState = 0; 

#define VPIN_BUTTON_1    V1 
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3 
#define VPIN_BUTTON_4    V4
#define VPIN_BUTTON_5    V5
#define VPIN_BUTTON_6    V6
#define VPIN_TEMPERATURE V7
#define VPIN_HUMIDITY    V8

// You should get Auth Token in the Blynk App.
char auth[] = "AUTH TOKEN";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "WIFI NAME";
char pass[] = "WIFI PASSWORD";

bool connectedFlag = false;
String pinStatus ;

// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('A');
  }
  else if(pinState == 0){
    Serial.write('a');
  }   
}

BLYNK_WRITE(VPIN_BUTTON_2) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('B');
  }
  else if(pinState == 0){
    Serial.write('b');
  }  
}

BLYNK_WRITE(VPIN_BUTTON_3) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('C');
  }
  else if(pinState == 0){
    Serial.write('c');
  }  
}

BLYNK_WRITE(VPIN_BUTTON_4) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('D');
  }
  else if(pinState == 0){
    Serial.write('d');
  }  
}
BLYNK_WRITE(VPIN_BUTTON_5) {  
  pinState = param.asInt();
  if(pinState == 1){
    Serial.write('E');
  }
  else if(pinState == 0){
    Serial.write('e');
  }  
}

void updateState(String state){
  if (state.substring(0,1) == "A"){
    Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
  }

  if (state.substring(1,2) == "B"){
    Blynk.virtualWrite(VPIN_BUTTON_2, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_2, LOW);
  }

  if (state.substring(2,3) == "C"){
    Blynk.virtualWrite(VPIN_BUTTON_3, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_3, LOW);
  }
  
  if (state.substring(3,4) == "D"){
    Blynk.virtualWrite(VPIN_BUTTON_4, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_4, LOW);
  }
  if (state.substring(4,5) == "E"){
    Blynk.virtualWrite(VPIN_BUTTON_5, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_5, LOW);
  }
  if (state.substring(5,6) == "1"){
    Blynk.virtualWrite(VPIN_BUTTON_6, HIGH);
  }
  else{
    Blynk.virtualWrite(VPIN_BUTTON_6, LOW);
  }
  
  Blynk.virtualWrite(VPIN_TEMPERATURE, state.substring(6,11));
  Blynk.virtualWrite(VPIN_HUMIDITY, state.substring(11,16));
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  
  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  while(Serial.available()) 
  {
    pinStatus = Serial.readString();
    updateState(pinStatus);
    connectedFlag = true;
  }
  if (connectedFlag){  
    Blynk.run();
  }
}

Credits

Subhajit Das

Subhajit Das

7 projects • 9 followers
I have studied Electrical Engineering. I do love to make different DIY electronics projects & share it on my YouTube channel Tech StudyCell.

Comments

Add projectSign up / Login