Advertisement
CHU2

Untitled

Jan 16th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 5.71 KB | Source Code | 0 0
  1. #define BLYNK_AUTH_TOKEN "FDd6o9jx9g318oot3_sqs3Zo3VSusw3V"
  2. #define BLYNK_TEMPLATE_ID "TMPL6lz94cbxq"
  3. #define BLYNK_TEMPLATE_NAME "Home Automation"
  4. #define BLYNK_PRINT Serial
  5.  
  6. #include <BlynkSimpleEsp32.h>
  7. #include <WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <Adafruit_GFX.h>
  10. #include <Adafruit_I2CDevice.h>
  11. #include <Adafruit_SSD1306.h>
  12. #include <Adafruit_Sensor.h>
  13. #include <DHT.h>  
  14. #include <ESP32Servo.h>
  15. #include <Wire.h>
  16.  
  17. #define DHTPIN 15
  18. #define DHTTYPE DHT11
  19.  
  20. char auth[] = BLYNK_AUTH_TOKEN;
  21. char pass[] = "METAMORPHOSIS";
  22. char ssid[] = "@177013";
  23.  
  24. int buttonPin = 4;
  25. int buzzerPin = 5;
  26. int servoPin = 33;
  27. int pirSensorPin = 36;
  28. int tiltSensorPin = 27;
  29. int soundSensorPin = 34;
  30.  
  31. int redPin = 18;
  32. int greenPin = 13;
  33. int bluePin = 12;
  34.  
  35. bool isDoorLocked = true;
  36.  
  37. Adafruit_SSD1306 oled(128, 64, &Wire, -1);
  38. DHT dht(DHTPIN, DHTTYPE);
  39. Servo myServo;
  40.  
  41. int debounceDelay = 50;
  42. int flameSensorValue;
  43. int lastFlameSensorValue = 1;
  44.  
  45. int notificationVirtualPin = V0;
  46. int tiltSensorValue;
  47. int pirSensorValue;
  48. int soundSensorValue;
  49.  
  50. int awayHomeMode = 0;
  51. BlynkTimer timer;
  52.  
  53. void flame_control();
  54. void rgb_low();
  55. void sens();
  56. void tamperingAlert(int, int);
  57. void updateOLED(float, bool);
  58.  
  59. void test_area();
  60.  
  61. void setup() {
  62.   Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  63.  
  64.   dht.begin();
  65.  
  66.   pinMode(buttonPin, INPUT);
  67.   pinMode(flameSensorPin, INPUT);
  68.   pinMode(pirSensorPin, INPUT);
  69.   pinMode(soundSensorPin, INPUT);
  70.   pinMode(tiltSensorPin, INPUT_PULLUP);
  71.   pinMode(buzzerPin, OUTPUT);
  72.  
  73.   pinMode(redPin, OUTPUT);
  74.   pinMode(greenPin, OUTPUT);
  75.   pinMode(bluePin, OUTPUT);
  76.  
  77.   myServo.attach(servoPin);
  78.  
  79.   Serial.begin(9600);
  80.  
  81.   Wire.begin(21, 22);
  82.   oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  83.   if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  84.     Serial.println(F("OLED initialization failed!"));
  85.     while (true);
  86.   }
  87.   oled.clearDisplay();
  88.   oled.setTextSize(1);
  89.   oled.setTextColor(SSD1306_WHITE);
  90. }
  91.  
  92. BLYNK_CONNECTED() {
  93.   Blynk.syncVirtual(V0);
  94. }
  95.  
  96. BLYNK_WRITE(V0) {
  97.   awayHomeMode = param.asInt();
  98. }
  99.  
  100. void loop() {
  101.   float temp = dht.readTemperature();
  102.  
  103.   sens();
  104.   test_area();
  105.  
  106.   Blynk.run();
  107.   timer.run();
  108.   updateOLED(temp, isDoorLocked);
  109.  
  110.   int buttonValue = digitalRead(buttonPin);
  111.  
  112.   if (buttonValue == 1) {
  113.     isDoorLocked =  false;
  114.     myServo.write(180);
  115.   }
  116.   else {
  117.     isDoorLocked =  true;
  118.     myServo.write(0);
  119.   }
  120.  
  121.   if (temp > 54.44) { // gin balyuan ko kay idk kun anon nangyayari flamesens na on off every cycle, dire constant
  122.     myServo.write(180);
  123.     isDoorLocked = false;
  124.  
  125.     Blynk.virtualWrite(notificationVirtualPin, "Your house is on fire!");
  126.     Blynk.logEvent("fire_alert");
  127.    
  128.     while (temp > 54.44) {
  129.       sens();
  130.       test_area();
  131.       tamperingAlert(redPin, redPin);
  132.     }
  133.   }
  134.   else if (temp > 45) { // initially > 54.44
  135.     Blynk.virtualWrite(notificationVirtualPin, "Our sensors senses high temperature, can possible cause fire!");
  136.     Blynk.logEvent("early_fire_warning");
  137.  
  138.     while (temp > 45) {
  139.       tone(buzzerPin, 1130);
  140.       delay(130);
  141.       tone(buzzerPin, 0);
  142.       delay(130);
  143.     }
  144.   }
  145.  
  146.   if (isDoorLocked && ((tiltSensorValue && pirSensorValue > 100) || tiltSensorValue)) {
  147.     Blynk.virtualWrite(notificationVirtualPin, "A window has been opened, check your house for intruders!");
  148.     Blynk.logEvent("breaking_alert");
  149.  
  150.     sens();
  151.     test_area();
  152.     tamperingAlert(bluePin, redPin);
  153.   }
  154.  
  155.   else if (isDoorLocked && ((pirSensorValue > 1300 && soundSensorValue) || soundSensorValue)) {
  156.     Blynk.virtualWrite(notificationVirtualPin, "Our sensor senses sound and motion, check your house for intruders!");
  157.     Blynk.logEvent("motion_detected");
  158.  
  159.     sens();
  160.     test_area();
  161.  
  162.     for (int h = 0; h < 23; h++) {
  163.       tamperingAlert(bluePin, redPin);
  164.     }
  165.   }
  166.   else if (isDoorLocked && pirSensorValue > 100) {
  167.     Blynk.virtualWrite(notificationVirtualPin, "Our sensor senses motion, check your house for intruders!");
  168.     Blynk.logEvent("motion_detected");
  169.  
  170.     sens();
  171.     test_area();
  172.  
  173.     // for (int h = 0; h < 23; h++) {
  174.       tamperingAlert(bluePin, redPin);
  175.     // }
  176.   }
  177.  
  178.   if (!isDoorLocked) {
  179.     rgb_low();
  180.     digitalWrite(greenPin, HIGH);
  181.   }
  182.   else {
  183.     rgb_low();
  184.     digitalWrite(redPin, HIGH);
  185.   }
  186.  
  187. }
  188.  
  189. void flame_control() {
  190.   int reading = digitalRead(flameSensorPin);
  191.  
  192.   if (reading != lastFlameSensorValue) {
  193.     delay(debounceDelay);
  194.  
  195.     if (digitalRead(flameSensorPin) == reading) {
  196.       flameSensorValue = reading;
  197.       lastFlameSensorValue = reading;
  198.     }
  199.   }
  200. }
  201.  
  202. void rgb_low() {
  203.   digitalWrite(redPin, LOW);
  204.   digitalWrite(greenPin, LOW);
  205.   digitalWrite(bluePin, LOW);
  206. }
  207.  
  208. void sens() {
  209.   // flameSensorValue = digitalRead(flameSensorPin);
  210.   // tiltSensorValue = digitalRead(tiltSensorPin);
  211.   pirSensorValue = analogRead(pirSensorPin);
  212.   // soundSensorValue = digitalRead(soundSensorPin);
  213. }
  214.  
  215. void tamperingAlert(int leftPin, int rightPin) {
  216.   rgb_low();
  217.   digitalWrite(leftPin, HIGH);
  218.   tone(buzzerPin, 1130);
  219.   delay(130);
  220.   rgb_low();
  221.   digitalWrite(rightPin, HIGH);
  222.   tone(buzzerPin, 0);
  223.   delay(130);
  224.   rgb_low();
  225.   digitalWrite(leftPin, LOW);
  226.   tone(buzzerPin, 1130);
  227.   delay(130);
  228.   rgb_low();
  229.   digitalWrite(rightPin, LOW);
  230.   tone(buzzerPin, 0);
  231.   delay(130);
  232. }
  233.  
  234. void updateOLED(float temperature, bool lockState) {
  235.   oled.clearDisplay();
  236.   oled.setCursor(0, 0);
  237.   oled.print("Temp: ");
  238.   oled.print(temperature);
  239.   oled.println(" C");
  240.   oled.print("Status: ");
  241.   oled.println(lockState ? "Unlocked" : "Locked");
  242.   oled.display();
  243. }
  244.  
  245. void test_area() {
  246.   Serial.println(flameSensorValue);
  247.   Serial.println(tiltSensorValue);
  248.   Serial.println(pirSensorValue);
  249.   Serial.println(soundSensorValue);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement