📦 Smart Home Automation System
📘 Complete Integration Code + Blynk Setup
🧠 System Includes
-
🔐 Smart Door Lock (Servo – Manual)
-
🌡 Temperature Monitoring (DHT11)
-
❄ AC Control (Manual + Automatic Mode)
-
💧 Water Tank Monitoring (Ultrasonic)
-
🚰 Pump Control (Manual + Automatic Mode)
-
🔥 Gas Leakage Detection (MQ3 – Safety Override)
-
📱 Full Blynk Dashboard
🟢 PART 1 – Blynk Setup (Step-by-Step)
🔹 Step 1 – Create Template
-
Go to Blynk Cloud
-
Create Template
-
Hardware: ESP32
-
Connection: WiFi
-
Save.
🔹 Step 2 – Create Datastreams
Create the following:
| Datastream | Virtual Pin | Type |
|---|---|---|
| Temperature | V0 | Double |
| AC Manual Button | V1 | Integer |
| AC Mode | V2 | Integer |
| Temp Setpoint | V3 | Double |
| Water Level | V4 | Double |
| Pump Manual Button | V5 | Integer |
| Pump Mode | V6 | Integer |
| Water Setpoint | V7 | Double |
| Gas Value | V8 | Integer |
| Gas Alert | V9 | String |
| Door Lock | V10 | Integer |
Save all.
🔹 Step 3 – Dashboard Widgets
Add:
-
Gauge → V0 (Temperature)
-
Slider → V3 (Temp Setpoint)
-
Button → V1 (AC Manual)
-
Switch → V2 (AC Mode)
-
Gauge → V4 (Water Level)
-
Slider → V7 (Water Setpoint)
-
Button → V5 (Pump Manual)
-
Switch → V6 (Pump Mode)
-
Label → V8 (Gas Value)
-
Label → V9 (Gas Alert)
-
Button → V10 (Door Lock)
🟢 PART 2 – Complete Integrated Code
⚠ Replace WiFi and Blynk credentials.
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "SmartHome"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"
#include <ESP32Servo.h>
// WiFi
char ssid[] = "YOUR_WIFI";
char pass[] = "YOUR_PASS";
// Pins
#define DHTPIN 4
#define DHTTYPE DHT11
#define TRIG 5
#define ECHO 18
#define MQ3_PIN 34
#define SERVO_PIN 13
#define AC_RELAY 26
#define PUMP_RELAY 27
DHT dht(DHTPIN, DHTTYPE);
Servo doorServo;
BlynkTimer timer;
// Variables
float temperature;
float waterLevel;
int gasValue;
int acManual = 0;
int acMode = 0;
float tempSetpoint = 28;
int pumpManual = 0;
int pumpMode = 0;
float waterSetpoint = 30;
int doorState = 0;
// ------------------ BLYNK WRITE ------------------
BLYNK_WRITE(V1) { acManual = param.asInt(); }
BLYNK_WRITE(V2) { acMode = param.asInt(); }
BLYNK_WRITE(V3) { tempSetpoint = param.asFloat(); }
BLYNK_WRITE(V5) { pumpManual = param.asInt(); }
BLYNK_WRITE(V6) { pumpMode = param.asInt(); }
BLYNK_WRITE(V7) { waterSetpoint = param.asFloat(); }
BLYNK_WRITE(V10) {
doorState = param.asInt();
if (doorState) doorServo.write(90);
else doorServo.write(0);
}
// ------------------ SENSOR READ ------------------
void readTemperature() {
temperature = dht.readTemperature();
Blynk.virtualWrite(V0, temperature);
}
void readWaterLevel() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long duration = pulseIn(ECHO, HIGH);
float distance = duration * 0.034 / 2;
waterLevel = 100 - distance; // adjust based on tank height
Blynk.virtualWrite(V4, waterLevel);
}
void readGas() {
gasValue = analogRead(MQ3_PIN);
Blynk.virtualWrite(V8, gasValue);
if (gasValue > 1200) {
Blynk.virtualWrite(V9, "⚠ GAS LEAK!");
digitalWrite(AC_RELAY, HIGH); // Turn OFF AC
} else {
Blynk.virtualWrite(V9, "Safe");
}
}
// ------------------ CONTROL LOGIC ------------------
void controlAC() {
if (acMode == 0) { // Manual
digitalWrite(AC_RELAY, acManual ? LOW : HIGH);
}
if (acMode == 1) { // Auto
if (temperature > tempSetpoint)
digitalWrite(AC_RELAY, LOW);
else
digitalWrite(AC_RELAY, HIGH);
}
}
void controlPump() {
if (pumpMode == 0) {
digitalWrite(PUMP_RELAY, pumpManual ? LOW : HIGH);
}
if (pumpMode == 1) {
if (waterLevel < waterSetpoint)
digitalWrite(PUMP_RELAY, LOW);
else
digitalWrite(PUMP_RELAY, HIGH);
}
}
// ------------------ SETUP ------------------
void setup() {
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(AC_RELAY, OUTPUT);
pinMode(PUMP_RELAY, OUTPUT);
digitalWrite(AC_RELAY, HIGH);
digitalWrite(PUMP_RELAY, HIGH);
dht.begin();
doorServo.attach(SERVO_PIN);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(2000L, readTemperature);
timer.setInterval(2000L, readWaterLevel);
timer.setInterval(2000L, readGas);
timer.setInterval(2000L, controlAC);
timer.setInterval(2000L, controlPump);
}
// ------------------ LOOP ------------------
void loop() {
Blynk.run();
timer.run();
}
🧠 Code Explanation (Simplified)
-
readTemperature()→ Sends temperature to Blynk -
readWaterLevel()→ Calculates tank level -
readGas()→ Safety override -
controlAC()→ Manual/Auto AC logic -
controlPump()→ Manual/Auto Pump logic -
BLYNK_WRITE()→ Receives mobile commands
No delay() used → Non-blocking → Professional.
🔐 Safety Logic
If gas detected:
AC automatically turns OFF
Alert shown in app
Safety has highest priority.
🎓 What Students Learn
-
Multi-sensor integration
-
Manual vs Automatic logic
-
Safety override design
-
Modular coding
-
Cloud dashboard design
-
Professional IoT architecture