Nodemcu with auto power off and smoke detector

DHTesp dht
int mq135 = A0; // smoke sensor is connected with the analog pin A0
int data = 0;
//Let’s say you have your push button on pin 4
int switchState = 0; // actual read value from pin4
int oldSwitchState = 0; // last read value from pin4
int RelayON = 0; // is the switch on = 1 or off = 0

int TANK1 = 14; //D5(gpio14)
int TANK1flowState = 0;
int buzzerstate = 0;
int virtualpin4 = 0; // virtual pin for 4 feed

/* Bylnk key credentials */
char auth[] = “sadfsafdsafdasfdasfafa”;

/* WiFi credentials */
char ssid[] = “sadfdsaf”;
char pass[] = “safsdafsdf”;

SimpleTimer timer;

void setup() {
Serial.begin(115200);
pinMode(BUTTON, INPUT); // push button
pinMode(WaterFullLed, OUTPUT); // Set buzzer – D6
pinMode(buzzerPin, OUTPUT); // Set buzzer – D8
pinMode(RelayPin, OUTPUT); // anything you want to control using a switch e.g. a RelayPin
pinMode(TANK1, INPUT);

//set default value
digitalWrite(buzzerPin, LOW); // set buzzer off
digitalWrite(RelayPin, HIGH); // set relay off
digitalWrite(WaterFullLed,LOW); // set water led indicate off

WiFi.begin(ssid, pass); //Connect to your WiFi router
Serial.println(“”);
Blynk.begin(auth, ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(WiFi.localIP()); //IP address assigned to your ESP

timer.setInterval(10L, getSwitchData);
dht.setup(DHTpin, DHTesp::DHT11);
timer.setInterval(2000L, getSendData);

}

void loop()
{

timer.run(); // Initiates SimpleTimer
Blynk.run();

}//end of void loop

/*

  • Send Sensor data to Blynk
    /
    void getSendData()
    {
    float h = dht.getHumidity(); //Read humidity and put it in h variable
    float t = dht.getTemperature(); //Read temperature and put it in t variable
    Blynk.virtualWrite(V5, t); //Send t value to blynk in V5 virtual pin
    Blynk.virtualWrite(V6, h); //Send h value to blynk in V6 virtual pin if (isnan(h) ||isnan(t)) {
    Serial.println(“Cant read from DHT11”);
    }else{
    Serial.println(h);
    Serial.println(t);
    } data = analogRead(mq135);
    Serial.println(data);
    Blynk.virtualWrite(V2, data); //virtual pin V3 if (data > 300 ) //Blynk will send notify if data>40
    {
    tone(buzzerPin, 2000); // Send 1KHz sound signal…
    delay(1000); // …for 1 sec
    noTone(buzzerPin); // Stop sound…
    delay(1000); // …for 1sec
    Blynk.notify(“Smoke Detected!!!”); } if (t>40) {
    tone(buzzerPin, 1000); // Send 1KHz sound signal…
    delay(1000); // …for 1 sec
    noTone(buzzerPin); // Stop sound…
    delay(1000); // …for 1sec
    Blynk.notify(“The Temperature is too high”); //Blynk will send notify if t>40
    }

}
/* End of DHT11 */

void getSwitchData()
{
switchState = digitalRead(BUTTON); // read the pushButton State
TANK1flowState = digitalRead(TANK1); // read the flowButton State
if (switchState != oldSwitchState) // catch change
{
oldSwitchState = switchState;
if (switchState == HIGH)
{
// toggle
RelayON = !RelayON;
}
}
if (TANK1flowState == HIGH && buzzerstate == 0) {
buzzerstate = millis();
}

if (TANK1flowState == HIGH)
{
// toggle
switchState = LOW;
digitalWrite(WaterFullLed,HIGH);
Serial.println(“Water FULL”);

RelayON = 0;
//buzzer activation time in ms 15 secc(15000L)
if(millis() – buzzerstate < 15000L) {
digitalWrite(buzzerPin,HIGH);
delay(100);
digitalWrite(buzzerPin,LOW);
delay(100);
}

} else  {
    digitalWrite(buzzerPin,LOW);
    digitalWrite(WaterFullLed,LOW);
    buzzerstate = 0;
}

if(RelayON)
{
digitalWrite(RelayPin, LOW); // set the RelayPin on
Serial.println(“WaterTANK ON”);
Blynk.virtualWrite(V4, RelayON);
} else {
digitalWrite(RelayPin, HIGH); // set the RelayPin off
Blynk.virtualWrite(V4, RelayON);
}
}//end of getswitch data

BLYNK_WRITE(V4) //Button Widget is writing to pin V4
{
int virtualpin4 = param.asInt();
switchState = virtualpin4;
Serial.println(switchState);
if (switchState == HIGH)
{
// toggle
RelayON = !RelayON;
}else{
RelayON = !RelayON;
}

 if (RelayON)
 {
  Serial.println("Relay High");
  digitalWrite(RelayPin, HIGH); // set the RelayButtonOnOFF on
 }else {
  Serial.println("Relay Low");
  digitalWrite(RelayPin, LOW); // set the RelayButtonOnOFF off
 }

}
/* End of bylnk button sync */

Leave a Reply

Your email address will not be published. Required fields are marked *