#define BLYNK_TEMPLATE_ID "TMPL60Pne1P_6"
#define BLYNK_TEMPLATE_NAME "Fire Alarm"
#include <Adafruit_NeoPixel.h>
#include <ESP32Servo.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define AIRQUALITY_PIN V0
#define TEMPARATURE_PIN V1
#define SHUTDOWN_PIN V2
#define BUZZER_PIN 5
#define FAN_SERVO_PIN 12
#define SPRINKLER_SERVO_PIN 14
#define DHT_PIN 4
#define PINPIXELS 13
#define NUM_LEDS 24
Servo fanServo;
Servo sprinklerServo;
DHT dht(DHT_PIN, DHT22);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PINPIXELS, NEO_GRB + NEO_KHZ800);
char auth[] = "kGxYwcnOnGpoOaDm9i6M62ZTes-G7oS1";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
bool systemOn = true;
BLYNK_WRITE(SHUTDOWN_PIN){
if(param.asInt() == HIGH){
ESP.restart();
}
}
int pos = 0;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
fanServo.attach(FAN_SERVO_PIN);
sprinklerServo.attach(SPRINKLER_SERVO_PIN);
dht.begin();
pixels.begin();
pixels.show();
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
float tempValue = dht.readTemperature();
float gasValue = dht.readHumidity(); //act as gasSensor, so set >0
Blynk.virtualWrite(AIRQUALITY_PIN, gasValue);
Blynk.virtualWrite(TEMPARATURE_PIN, tempValue);
if (gasValue > 0 && tempValue > 35.0) {
tone(BUZZER_PIN, 300, 100);
activateSprinkler();
activateFan();
repeatCombinedPixels();
}
else if(gasValue > 0){
tone(BUZZER_PIN, 300, 200);
repeatYellowPixels();
deactivateSprinkler();
activateFan();
delay(10);
}
else if (tempValue > 35.0) {
tone(BUZZER_PIN, 300, 300);
repeatRedPixels();
deactivateFan();
activateSprinkler();
delay(10);
}
else{
noTone(BUZZER_PIN);
for (int i = 0; i < NUM_LEDS; i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Turn off NeoPixels
}
pixels.show();
deactivateFan();
deactivateSprinkler();
}
}
void repeatYellowPixels()
{
for(int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(255, 255, 0));
pixels.show();
delay(10);
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
}
void repeatRedPixels()
{
for(int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(255, 0, 0));
pixels.show();
delay(10);
pixels.setPixelColor(i, pixels.Color(0,0,0));
}
}
void repeatYellowRedPixels(){
for (int i = 0; i < pixels.numPixels(); i += 2)
{
pixels.setPixelColor(i, pixels.Color(255, 255, 0));
pixels.setPixelColor(i + 1, pixels.Color(255, 0, 0));
}
pixels.show();
delay(10);
for (int i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
pixels.show();
}
void repeatCombinedPixels()
{
for (int j = 0; j < 4; j++) // Repeat the pattern 4 times
{
repeatYellowRedPixels();
}
}
void activateFan(){
fanServo.attach(FAN_SERVO_PIN);
fanServo.write(180);
delay(10);
}
void deactivateFan() {
fanServo.detach();
}
void activateSprinkler(){
sprinklerServo.attach(SPRINKLER_SERVO_PIN);
sprinklerServo.write(180);
delay(10);
}
void deactivateSprinkler() {
sprinklerServo.detach();
}