Home Automation System Using
Home Automation System Using
Buzzer
Alerts users in case of a gas leak or other critical conditions.
Blynk App Setup
2. Add Widgets
3. Connect to ESP8266
Code
#define BLYNK_TEMPLATE_ID "TMPL3CfFycCJF“
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
BlynkTimer timer;
#define Buzzer D0
#define MQ2 A0
#define trig D4
#define echo D5
#define relay1 D7
#define relay2 D8
void checkConnection()
{ if (WiFi.status() != WL_CONNECTED)
WiFi.begin(ssid, pass);
int timeout = 0;
delay(500);
Serial.print("."); timeout++;
if (WiFi.status() == WL_CONNECTED)
if (!Blynk.connected())
Blynk.connect();
}
}
pinMode(Buzzer, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, pass);
delay(500);
Serial.print(".");
dht.begin();
timer.setInterval(1000L, gassensor);
timer.setInterval(1000L, DHT11sensor);
timer.setInterval(1000L, ultrasonic);
void gassensor()
{
Serial.println(value);
digitalWrite(Buzzer, HIGH);
else
digitalWrite(Buzzer, LOW);
Blynk.virtualWrite(V1, value);
void DHT11sensor()
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
return;
Blynk.virtualWrite(V2, t);
Blynk.virtualWrite(V3, h);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(h);
Serial.println(" %");
void ultrasonic()
digitalWrite(trig, LOW);
delayMicroseconds(4);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long cm = t / 29 / 2;
Serial.print(cm);
Serial.println(" cm");
BLYNK_WRITE(V5)
}
BLYNK_WRITE(V6)
Serial.println(state);
void loop()
checkConnection();
Blynk.run();
timer.run();
QUESTIONS
1. NodeMCU and Blynk Integration
Q: Explain how the NodeMCU ESP8266 connects to the Blynk cloud. What steps are required to ensure
stable WiFi connectivity and remote control functionality?
Write the code to establish a connection between NodeMCU and Blynk, ensuring reconnection
in case of a WiFi failure.
Q: The MQ-2 sensor is used for gas detection. Describe how it works, how the system alerts users in case
of a gas leak, and how the Blynk app handles notifications.
Write the code to read gas levels from the MQ-2 sensor and send alerts to the Blynk app when
dangerous levels are detected.
Q: How does the HC-SR04 ultrasonic sensor measure water level? Explain its working principle and how
the system displays the data in the Blynk app.
Write the code to measure the water level using the HC-SR04 sensor and update the Blynk app
with real-time readings.
Q: What role do relay modules play in this home automation system? Explain how they are controlled
through the Blynk app and how the system ensures user safety.
Write the code to control a relay module using the Blynk app, allowing users to turn an
appliance ON/OFF remotely.
Q: Compare Blynk and ThingSpeak in terms of functionality. In which scenarios would it be more
beneficial to use ThingSpeak instead of Blynk?
Write the code to send sensor data (e.g., temperature, gas levels) to ThingSpeak for data logging
and visualization.