Smart Office Project
Smart Office Project
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "TMPL0jxQDNHs"
#define BLYNK_TEMPLATE_NAME "IOT project"
#define BLYNK_AUTH_TOKEN "5CkcY4KNRMQ3aKHGxQLmRiTH5O-LD-IC"
//------------------------------------------------------------------------------
//--------------------------------------------------
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//------------------------------------------------------
boolean buzzer_state = false;
unsigned long buzzer_timer = 0;
//------------------------------------------------------
bool PIR_BUTTON = 0;
int mq2_limit = 55;
int temp_limit = 50;
//------------------------------------------------------
#include <DHT.h>
#define DHTTYPE DHT11
DHT dht(DHT_PIN, DHTTYPE);
//--------------------------------------------------------------------------
// This function is called every time the device is connected
//to the Blynk.Cloud Request the latest state from the server
BLYNK_CONNECTED() {
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
Blynk.syncVirtual(VPIN_PIR);
//Blynk.syncAll();
}
//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(VPIN_BUTTON_1) {
STATE_RELAY_1 = param.asInt();
digitalWrite(RELAY_1, STATE_RELAY_1);
Serial.print("Relay1 State = "); Serial.println(STATE_RELAY_1);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(VPIN_BUTTON_2) {
STATE_RELAY_2 = param.asInt();
digitalWrite(RELAY_2, STATE_RELAY_2);
Serial.print("Relay2 State = "); Serial.println(STATE_RELAY_2);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(VPIN_BUTTON_3) {
STATE_RELAY_3 = param.asInt();
digitalWrite(RELAY_3, STATE_RELAY_3);
Serial.print("Relay3 State = "); Serial.println(STATE_RELAY_3);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(VPIN_BUTTON_4) {
STATE_RELAY_4 = param.asInt();
digitalWrite(RELAY_4, STATE_RELAY_4);
Serial.print("Relay4 State = "); Serial.println(STATE_RELAY_4);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(VPIN_PIR) {
PIR_BUTTON = param.asInt();
Serial.print("PIR Button = "); Serial.println(PIR_BUTTON);
}
//--------------------------------------------------------------------------
/
***********************************************************************************
*****************
* setup() Function
***********************************************************************************
******************/
void setup(){
// Debug console
Serial.begin(115200);
//--------------------------------------------------------------------
pinMode(BUTTON_1, INPUT_PULLUP);
pinMode(BUTTON_2, INPUT_PULLUP);
pinMode(BUTTON_3, INPUT_PULLUP);
pinMode(BUTTON_4, INPUT_PULLUP);
//--------------------------------------------------------------------
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
//--------------------------------------------------------------------
//During Starting all Relays should TURN OFF
digitalWrite(RELAY_1, HIGH);
digitalWrite(RELAY_2, HIGH);
digitalWrite(RELAY_3, HIGH);
digitalWrite(RELAY_4, HIGH);
//--------------------------------------------------------------------
pinMode(FLAME, INPUT);
pinMode(PIR, INPUT);
pinMode(BUZZER, OUTPUT);
//--------------------------------------------------------------------
dht.begin();
//--------------------------------------------------------------------
Blynk.begin(auth, ssid, pass);
// We can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
//--------------------------------------------------------------------
//Blynk.virtualWrite(VPIN_BUTTON_1, STATE_RELAY_1);
//Blynk.virtualWrite(VPIN_BUTTON_2, STATE_RELAY_2);
//Blynk.virtualWrite(VPIN_BUTTON_3, STATE_RELAY_3);
//Blynk.virtualWrite(VPIN_BUTTON_4, STATE_RELAY_4);
//--------------------------------------------------------------------
timer.setInterval(100L, MQ2GasSensor);
timer.setInterval(100L, DHT11Sensor);
timer.setInterval(100L, PirSensor);
timer.setInterval(100L, FlameSensor);
}
/
***********************************************************************************
*****************
* loop() Function
***********************************************************************************
******************/
void loop() {
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
listen_push_buttons();
if (buzzer_state == true) {
if (millis() - buzzer_timer > 5000) {
digitalWrite(BUZZER, LOW);
buzzer_state = false;
buzzer_timer = 0;
}
}
/
***********************************************************************************
*****************
* DHT11Sensor Function
***********************************************************************************
******************/
void DHT11Sensor() {
//-----------------------------------------------------------------------
//Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
//-----------------------------------------------------------------------
//Read temperature as Celsius (the default)
temp_old = temp_new;
temp_new = dht.readTemperature();
//-----------------------------------------------------------------------
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(temp_new)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
//-----------------------------------------------------------------------
//Serial.print("Temperature: "); Serial.println(temp_new);
//Serial.print("Humidity: "); Serial.println(h);
//Serial.println("------------------------------------------");
//-----------------------------------------------------------------------
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(VPIN_TEMPERATURE, temp_new);
Blynk.virtualWrite(VPIN_HUMIDITY, h);
//-----------------------------------------------------------------------
//Serial.print(String(temp_old)+"-");Serial.println(temp_new);
if(temp_old < temp_limit && temp_new >= temp_limit) { //LOW to HIGH
String text = "Temperature is very high";
Serial.println(text);
Blynk.logEvent(temp_event, text);
digitalWrite(BUZZER, HIGH);
buzzer_state = true;
buzzer_timer = millis();
}
}
/
***********************************************************************************
*****************
* PirSensor Function
***********************************************************************************
******************/
void PirSensor() {
if (PIR_BUTTON == 1) {
pir_old = pir_new; // store old state
pir_new = digitalRead(PIR); //read new state
//------------------------------------------------------------------------
if(pir_old == LOW && pir_new == HIGH) {
String text = "Motion is Detected.";
Serial.println(text);
Blynk.logEvent(pir_event, text);
digitalWrite(BUZZER, HIGH);
buzzer_state = true;
buzzer_timer = millis();
}
//------------------------------------------------------------------------
//else if(pir_old == HIGH && pir_new == LOW) { //HIGH to LOW
//Serial.println("Motion stopped!");
//digitalWrite(BUZZER, LOW);
//}
//------------------------------------------------------------------------
}
}
/
***********************************************************************************
*****************
* MQ2GasSensor Function
***********************************************************************************
******************/
void MQ2GasSensor() {
mq2_old = mq2_new; // store old state
mq2_new = analogRead(MQ2); //read new state
mq2_new = map(mq2_new, 0, 4095, 0, 100);
//Serial.print("MQ2GasSensor Value: "); Serial.println(mq2_new);
//--------------------------------------------------------------
if(mq2_old < mq2_limit && mq2_new >= mq2_limit) { //LOW to HIGH
String text = "Gas leakage is detected";
Serial.println(text);
Blynk.logEvent(gas_event, text);
digitalWrite(BUZZER, HIGH);
buzzer_state = true;
buzzer_timer = millis();
}
//--------------------------------------------------------------
//else if(mq2_old > mq2_limit && mq2_new <= mq2_limit) { //HIGH to LOW
//Serial.println("Fire stopped!");
//digitalWrite(BUZZER, LOW);
//}
//--------------------------------------------------------------
Blynk.virtualWrite(VPIN_MQ2, mq2_new);
}
/
***********************************************************************************
*****************
* FlameSensor Function
***********************************************************************************
******************/
void FlameSensor() {
//--------------------------------------------------------------
flame_old = flame_new; // store old state
flame_new = digitalRead(FLAME); //read new state
//--------------------------------------------------------------
if(flame_old == HIGH && flame_new == LOW) { //HIGH to LOW
String text = "Fire is detected";
Serial.println(text);
Blynk.logEvent(fire_event, text);
digitalWrite(BUZZER, HIGH);
buzzer_state = true;
buzzer_timer = millis();
}
//--------------------------------------------------------------
//else if(flame_old == LOW && flame_new == HIGH) { //LOW to HIGH
//Serial.println("Fire stopped!");
//digitalWrite(BUZZER, LOW);
//}
//--------------------------------------------------------------
Blynk.virtualWrite(VPIN_FLAME, !flame_new);
//--------------------------------------------------------------
}
/
***********************************************************************************
*****************
* listen_push_buttons() function
***********************************************************************************
******************/
void listen_push_buttons(){
//-----------------------------------------------------
if(digitalRead(BUTTON_1) == LOW)
{ControlRelay(1, RELAY_1, STATE_RELAY_1, VPIN_BUTTON_1);}
//-----------------------------------------------------
else if (digitalRead(BUTTON_2) == LOW)
{ControlRelay(2, RELAY_2, STATE_RELAY_2, VPIN_BUTTON_2);}
//-----------------------------------------------------
else if (digitalRead(BUTTON_3) == LOW)
{ControlRelay(3, RELAY_3, STATE_RELAY_3, VPIN_BUTTON_3);}
//-----------------------------------------------------
else if (digitalRead(BUTTON_4) == LOW)
{ControlRelay(4, RELAY_4, STATE_RELAY_4, VPIN_BUTTON_4);}
//-----------------------------------------------------
}
/
***********************************************************************************
*****************
* ControlRelay Function
***********************************************************************************
******************/
void ControlRelay(int number, int relay_pin, int &status, int virtual_pin){
delay(200);
status = !status;
digitalWrite(relay_pin, status);
delay(50);
Blynk.virtualWrite(virtual_pin, status); //update button state
Serial.print("Relay"+String(number)+" State = "); Serial.println(status);
}