How to make a full home automation system with Nodemcu esp8266 and Blynk app

How to make a full home automation system with Nodemcu esp8266 and Blynk app

Hello, welcome back. In this tutorial, we will learn how to make a Full home automation system using the Nodemcu ESP8266 and Blynk app. Through this system, we will be able to monitor and control everything in our home. Also, five sensors and a two-channel relay module are mainly used for this system. Therefore, we can monitor and control everything in the house such as temperature, humidity, amount of leaking gas, amount of water in the water tank, home security, and control of electrical appliances. For that, it uses ultrasonic, flame, PIR, MQ2, DHT11 sensors and uses a two-channel relay module to control household electrical appliances. Remember to select the correct relay module you want. Also, this system is mainly based on IoT technology. Therefore, we can control and monitor all factors through the internet. The Blynk app has been used for that. If you are not familiar with the Blynk app. For that, please visit the previous tutorials.

The process of this system

When powering up this system. the Nodemcu board connects to the Blynk app through the Blynk cloud. At that point, the sensors begin to receive values. Then, those values are sent by the Nodemcu board to the Blynk app and the LCD. Also, we can turn the relays and PIR security system ON / OFF with the buttons on the Blynk app interface.

OK, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components. To do this, use the circuit diagram below.

How to make a full home automation system with Nodemcu esp8266 and Blynk app

Step 3

Thirdly, let’s set up the Blynk app. For that, follow the steps below.

  • First, download and install the Blynk app on your phone. Then, sign up for this app using your Email address. After, run this app and click the “New project” button.
  • Next, enter the project name as you like. Then, select the device and connection type. Finally, click the “Confirm” button.
  • Now, you can see the project interface. Then, we include the widget. For that, click on the + icon in the corner and include three “Button” widgets and three “Level H” widgets. Next, include one “Level V” widget and a notification widget.
  • Okay, now let’s set up this widget. First, click the relay control buttons one by one and name them as you like. Then, change the relay control buttons PIN D5 and D6 respectively. Then, change the input value from 1 to 0 and switch mode.
  • Next, click the PIR sensor control button and name it as you like. Then, change the PIN as V0 and mode to switch.
  • Now, click the “level H” widgets one by one and name as Gas level, Temperature, and Humidity. Also, change the PINs as V1, V2, and V3 respectively. Then, change the input values from 0 to 100.
  • Next, click the “Level V” widget and name it Water Level. Then, change the PIN to V4 and the input value to the height of the water tank. Also, change the flip axis to the ON side.
  • Lastly, click the notification widget and change the priority to HIGH.
  • OK, now the Blynk app is ready. Then, customize these widgets as you like.

Step 4

Okay, now let’s create the program for this system. It is as follows.

/*Full home automation system with Nodemcu and Blynk
 * https://srituhobby.com
 */
 
#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>


char auth[] = "";//Enter your Auth token
char ssid[] = "";//Enter your WIFI name
char pass[] = "";//Enter your WIFI password

LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(D4, DHT11); //(sensor pin,sensor type)
BlynkTimer timer;
bool pirbutton = 0;

#define Buzzer 10
#define MQ2 A0
#define flame D0
#define PIR D3
#define trig D5
#define echo D6
#define relay1 D7
#define relay2 D8

BLYNK_WRITE(V0) {
  pirbutton = param.asInt();
}

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  pinMode(Buzzer, OUTPUT);
  pinMode(flame, INPUT);
  pinMode(PIR, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(100L, gassensor);
  timer.setInterval(100L, DHT11sensor);
  timer.setInterval(100L, flamesensor);
  timer.setInterval(100L, pirsensor);
  timer.setInterval(100L, ultrasonic);
}

void gassensor() {
  int value = analogRead(MQ2);
  Serial.println(value);
  value = map(value, 0, 1024, 0, 100);
  if (value <= 35) {
    digitalWrite(Buzzer, LOW);
  } else if (value > 35) {
    Blynk.notify("Warning! Gas leak detected");
    digitalWrite(Buzzer, HIGH);
  }
  Blynk.virtualWrite(V1, value);
  lcd.setCursor(9, 0);
  lcd.print("G :");
  lcd.print(value);
  lcd.print("  ");
}

void DHT11sensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);

  lcd.setCursor(0, 0);
  lcd.print("T :");
  lcd.print(t);

  lcd.setCursor(0, 1);
  lcd.print("H :");
  lcd.print(h);
}

void flamesensor() {
  bool value = digitalRead(flame );
  if (value == 1) {
    digitalWrite(Buzzer, LOW);
  } else if (value == 0) {
    Blynk.notify("Warning! Fire was detected");
    digitalWrite(Buzzer, HIGH);
  }
}
void pirsensor() {
  bool value = digitalRead(PIR);
  if (pirbutton == 1) {
    if (value == 0) {
      digitalWrite(Buzzer, LOW);
    } else if (value == 1) {
      Blynk.notify("Warning! Please check your security system");
      digitalWrite(Buzzer, HIGH);
    }
  }
}

void ultrasonic() {
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
  Blynk.virtualWrite(V4, cm);
  lcd.setCursor(9, 1);
  lcd.print("W :");
  lcd.print(cm);
  lcd.print("  ");
}


void loop() {
  Blynk.run();
  timer.run();
}

Code explanation

Firstly, the library files are included.

#include <LiquidCrystal_I2C.h>
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

Secondly, includes the Blynk Auth token and WI-FI connections details.

char auth[] = "tiMY12sK15MYJpQVS49larATTXB35U02";//Enter your Auth token
char ssid[] = "Dialog 4G 025";//Enter your WIFI name
char pass[] = "jangu1234";//Enter your WIFI password

Thirdly, objects are created for LCD, DHT11, and Blynk libraries

LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(D4, DHT11); //(sensor pin,sensor type)
BlynkTimer timer;

Next, the sensors, relays, and buzzer PINs are defined.

#define Buzzer 10
#define MQ2 A0
#define flame D0
#define PIR D3
#define trig D5
#define echo D6
#define relay1 D7
#define relay2 D8

In the setup function,

void setup() {
  Serial.begin(9600);//The serial monitor is begin
//The LCD display is initialize
  lcd.init();
  lcd.backlight();
//sensors and relays pins are set as input and output pins
  pinMode(Buzzer, OUTPUT);
  pinMode(flame, INPUT);
  pinMode(PIR, INPUT);
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
//The relays are turns off
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
//Blynk and DHT11 libraries are begin
  Blynk.begin(auth, ssid, pass);
  dht.begin();
//Main functions are called. These functions are described below
  timer.setInterval(100L, gassensor);
  timer.setInterval(100L, DHT11sensor);
  timer.setInterval(100L, flamesensor);
  timer.setInterval(100L, pirsensor);
  timer.setInterval(100L, ultrasonic);
}

MQ2 sensor function,

void gassensor() {
//Gets the sensor values
  int value = analogRead(MQ2);
  Serial.println(value);
//This analog value is converted from 0 to 100
  value = map(value, 0, 1024, 0, 100);
//This value is checked using the IF condition
  if (value <= 35) {
    digitalWrite(Buzzer, LOW);
  } else if (value > 35) {
    Blynk.notify("Warning! Gas leak detected");
    digitalWrite(Buzzer, HIGH);
  }
//This value is sent to the Blynk app
  Blynk.virtualWrite(V1, value);
//This value is printed on the LCD
  lcd.setCursor(9, 0);
  lcd.print("G :");
  lcd.print(value);
  lcd.print("  ");
}

DHT11 sensor function,

void DHT11sensor() {
//Gets the sensor values
  float h = dht.readHumidity();
  float t = dht.readTemperature();
//DHT11 sensor status checked
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
//These values are sent to the Blynk app
  Blynk.virtualWrite(V2, t);
  Blynk.virtualWrite(V3, h);
//These values are printed on the LCD
  lcd.setCursor(0, 0);
  lcd.print("T :");
  lcd.print(t);

  lcd.setCursor(0, 1);
  lcd.print("H :");
  lcd.print(h);
}

Flame sensor function,

void flamesensor() {
//Gets the sensor values
  bool value = digitalRead(flame );
//These values are checked using the IF condition
  if (value == 1) {
    digitalWrite(Buzzer, LOW);
  } else if (value == 0) {
    Blynk.notify("Warning! Fire was detected");
    digitalWrite(Buzzer, HIGH);
  }
}

PIR sensor function,

void pirsensor() {
//Gets the sensor values
  bool value = digitalRead(PIR);
//These values are checked using the IF condition
  if (pirbutton == 1) {
    if (value == 0) {
      digitalWrite(Buzzer, LOW);
    } else if (value == 1) {
      Blynk.notify("Warning! Please check your security system");
      digitalWrite(Buzzer, HIGH);
    }
  }
}

Ultrasonic sensor function,

void ultrasonic() {
//Gets the distance from the ultrasonic sensor
  digitalWrite(trig, LOW);
  delayMicroseconds(4);
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long t = pulseIn(echo, HIGH);
  long cm = t / 29 / 2;
//These values are sent to the Blynk app
  Blynk.virtualWrite(V4, cm);
//These values are printed on the LCD
  lcd.setCursor(9, 1);
  lcd.print("W :");
  lcd.print(cm);
  lcd.print("  ");
}

In the loop function, the Blynk app is run.

void loop() {
  Blynk.run();
  timer.run();
}

Step 5

Now, select board and port. After, upload this code to the Nodemcu board.

Step 6

Lastly, run the Blynk app interface and enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

How to make a full home automation system with Nodemcu esp8266 and Blynk app

Similar Posts

One Comment

  1. congratulations!! your projects are very good, when you have new ones send them to me I will be very grateful

Leave a Reply

Your email address will not be published. Required fields are marked *