Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_AUTH_TOKEN "FDd6o9jx9g318oot3_sqs3Zo3VSusw3V"
- #define BLYNK_TEMPLATE_ID "TMPL6lz94cbxq"
- #define BLYNK_TEMPLATE_NAME "Home Automation"
- #define BLYNK_PRINT Serial
- #include <BlynkSimpleEsp32.h>
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_I2CDevice.h>
- #include <Adafruit_SSD1306.h>
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #include <ESP32Servo.h>
- #include <Wire.h>
- #define DHTPIN 15
- #define DHTTYPE DHT11
- char auth[] = BLYNK_AUTH_TOKEN;
- char pass[] = "METAMORPHOSIS";
- char ssid[] = "@177013";
- int buttonPin = 4;
- int buzzerPin = 5;
- int servoPin = 33;
- int pirSensorPin = 36;
- int tiltSensorPin = 27;
- int soundSensorPin = 34;
- int redPin = 18;
- int greenPin = 13;
- int bluePin = 12;
- bool isDoorLocked = true;
- Adafruit_SSD1306 oled(128, 64, &Wire, -1);
- DHT dht(DHTPIN, DHTTYPE);
- Servo myServo;
- int debounceDelay = 50;
- int flameSensorValue;
- int lastFlameSensorValue = 1;
- int notificationVirtualPin = V0;
- int tiltSensorValue;
- int pirSensorValue;
- int soundSensorValue;
- int awayHomeMode = 0;
- BlynkTimer timer;
- void flame_control();
- void rgb_low();
- void sens();
- void tamperingAlert(int, int);
- void updateOLED(float, bool);
- void test_area();
- void setup() {
- Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
- dht.begin();
- pinMode(buttonPin, INPUT);
- pinMode(flameSensorPin, INPUT);
- pinMode(pirSensorPin, INPUT);
- pinMode(soundSensorPin, INPUT);
- pinMode(tiltSensorPin, INPUT_PULLUP);
- pinMode(buzzerPin, OUTPUT);
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- myServo.attach(servoPin);
- Serial.begin(9600);
- Wire.begin(21, 22);
- oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
- Serial.println(F("OLED initialization failed!"));
- while (true);
- }
- oled.clearDisplay();
- oled.setTextSize(1);
- oled.setTextColor(SSD1306_WHITE);
- }
- BLYNK_CONNECTED() {
- Blynk.syncVirtual(V0);
- }
- BLYNK_WRITE(V0) {
- awayHomeMode = param.asInt();
- }
- void loop() {
- float temp = dht.readTemperature();
- sens();
- test_area();
- Blynk.run();
- timer.run();
- updateOLED(temp, isDoorLocked);
- int buttonValue = digitalRead(buttonPin);
- if (buttonValue == 1) {
- isDoorLocked = false;
- myServo.write(180);
- }
- else {
- isDoorLocked = true;
- myServo.write(0);
- }
- if (temp > 54.44) { // gin balyuan ko kay idk kun anon nangyayari flamesens na on off every cycle, dire constant
- myServo.write(180);
- isDoorLocked = false;
- Blynk.virtualWrite(notificationVirtualPin, "Your house is on fire!");
- Blynk.logEvent("fire_alert");
- while (temp > 54.44) {
- sens();
- test_area();
- tamperingAlert(redPin, redPin);
- }
- }
- else if (temp > 45) { // initially > 54.44
- Blynk.virtualWrite(notificationVirtualPin, "Our sensors senses high temperature, can possible cause fire!");
- Blynk.logEvent("early_fire_warning");
- while (temp > 45) {
- tone(buzzerPin, 1130);
- delay(130);
- tone(buzzerPin, 0);
- delay(130);
- }
- }
- if (isDoorLocked && ((tiltSensorValue && pirSensorValue > 100) || tiltSensorValue)) {
- Blynk.virtualWrite(notificationVirtualPin, "A window has been opened, check your house for intruders!");
- Blynk.logEvent("breaking_alert");
- sens();
- test_area();
- tamperingAlert(bluePin, redPin);
- }
- else if (isDoorLocked && ((pirSensorValue > 1300 && soundSensorValue) || soundSensorValue)) {
- Blynk.virtualWrite(notificationVirtualPin, "Our sensor senses sound and motion, check your house for intruders!");
- Blynk.logEvent("motion_detected");
- sens();
- test_area();
- for (int h = 0; h < 23; h++) {
- tamperingAlert(bluePin, redPin);
- }
- }
- else if (isDoorLocked && pirSensorValue > 100) {
- Blynk.virtualWrite(notificationVirtualPin, "Our sensor senses motion, check your house for intruders!");
- Blynk.logEvent("motion_detected");
- sens();
- test_area();
- // for (int h = 0; h < 23; h++) {
- tamperingAlert(bluePin, redPin);
- // }
- }
- if (!isDoorLocked) {
- rgb_low();
- digitalWrite(greenPin, HIGH);
- }
- else {
- rgb_low();
- digitalWrite(redPin, HIGH);
- }
- }
- void flame_control() {
- int reading = digitalRead(flameSensorPin);
- if (reading != lastFlameSensorValue) {
- delay(debounceDelay);
- if (digitalRead(flameSensorPin) == reading) {
- flameSensorValue = reading;
- lastFlameSensorValue = reading;
- }
- }
- }
- void rgb_low() {
- digitalWrite(redPin, LOW);
- digitalWrite(greenPin, LOW);
- digitalWrite(bluePin, LOW);
- }
- void sens() {
- // flameSensorValue = digitalRead(flameSensorPin);
- // tiltSensorValue = digitalRead(tiltSensorPin);
- pirSensorValue = analogRead(pirSensorPin);
- // soundSensorValue = digitalRead(soundSensorPin);
- }
- void tamperingAlert(int leftPin, int rightPin) {
- rgb_low();
- digitalWrite(leftPin, HIGH);
- tone(buzzerPin, 1130);
- delay(130);
- rgb_low();
- digitalWrite(rightPin, HIGH);
- tone(buzzerPin, 0);
- delay(130);
- rgb_low();
- digitalWrite(leftPin, LOW);
- tone(buzzerPin, 1130);
- delay(130);
- rgb_low();
- digitalWrite(rightPin, LOW);
- tone(buzzerPin, 0);
- delay(130);
- }
- void updateOLED(float temperature, bool lockState) {
- oled.clearDisplay();
- oled.setCursor(0, 0);
- oled.print("Temp: ");
- oled.print(temperature);
- oled.println(" C");
- oled.print("Status: ");
- oled.println(lockState ? "Unlocked" : "Locked");
- oled.display();
- }
- void test_area() {
- Serial.println(flameSensorValue);
- Serial.println(tiltSensorValue);
- Serial.println(pirSensorValue);
- Serial.println(soundSensorValue);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement