IOT Hardwire Fire/CO Alarm

Hello Everyone,

I wanted to have the peace of mind of knowing when there is fire detected in the house while i m away. I need assistance to iron out any kinks or logic errors on the below code. it compiles and uploads on wemos d1 mini. however there are times when it randomly says fire detected and then it just stays that way until i have to restart to go back to no fire detected mode.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "";

char ssid[] = "";
char pass[] = "";

BlynkTimer Mytimer; 

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 10;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 100;  

boolean lockLow = true;
boolean takeLowTime;  

//the digital pin connected to the PIR sensor's output
int pirPin = 13;  

unsigned long lastMillis = 0;

void connect();

void setup()
  // Debug console
  Blynk.begin(auth, ssid, pass);
 Mytimer.setInterval(1000L, switchstate); 
  pinMode(pirPin, INPUT);
  digitalWrite(pirPin, LOW);

void switchstate(){

  if(digitalRead(pirPin) == HIGH)
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;            
           Blynk.virtualWrite(2, "No Fire Detected");
     // delay(50);
    takeLowTime = true;

  if(digitalRead(pirPin) == LOW)
      lowIn = millis();          //save the time of the transition from high to LOW
      takeLowTime = false;       //make sure this is only done at the start of a LOW phase
    //if the sensor is low for more than the given pause, 
    //we assume that no more motion is going to happen
    if(!lockLow && millis() - lowIn > pause)
      //makes sure this block of code is only executed again after 
      //a new motion sequence has been detected
      lockLow = true;                        
      Blynk.notify("Fire Detected");  
      Blynk.virtualWrite(2, "Fire Detected");

void loop()

At first glance (looking at the code on an iPad) it looks okay.

I’d throw in some serial prints to the code at key points and have the MCU connected to a PC (that’s not allowed to sleep) to see what’s happening.