Garage door notification inaccurate timer

Hi blynkers I’m using wemos d1 mini to open my garage and get notifications if the garage is open from 5 minutes or 10 minutes via a magnetic sensor on digitalwrite 5 with the use of blynk.timer but the problem is that many times when the garage is open I get the notification before the 5 minutes or 10 minutes set how can I fix it?
Thanks

Android smartphone
blynk server

#ifndef Pins_Arduino_h
#define Pins_Arduino_h
#define PIN_WIRE_SDA (4)
#define PIN_WIRE_SCL (5)
static const uint8_t SDA = PIN_WIRE_SDA; static const uint8_t SCL = PIN_WIRE_SCL;
static const uint8_t D0   = 16;  static const uint8_t D1   = 5;  static const uint8_t D2   = 4;
static const uint8_t D3   = 0;   static const uint8_t D4   = 2;  static const uint8_t D5   = 14;
static const uint8_t D6   = 12;  static const uint8_t D7   = 13; static const uint8_t D8   = 15;
static const uint8_t RX   = 3;   static const uint8_t TX   = 1;
#include "../generic/common.h"
#endif /* Pins_Arduino_h */

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

char ssid[] = "rete";
char pass[] = "**********";
char auth[] = "********************";

#include <DHT.h>
#define DHTPIN D3          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11
  DHT dht(DHTPIN, DHTTYPE);
  BlynkTimer timer;
  void sendSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
  Serial.println("Impossibile leggere il sensore DHT !");
  return;}
  Blynk.virtualWrite(V2, t);  Blynk.virtualWrite(V3, h);}
  

 BlynkTimer timergarage1;
 int flag = 0;
 
 void notifyUptime(){
 int isButtonPressed = digitalRead(D5);
 if (digitalRead(D5)==LOW && flag ==0) {Blynk.notify ("Attenzione : Garage aperto da 5 minuti ");
 flag =1 ;}
 if (isButtonPressed ==HIGH ){flag =0;}
}

 BlynkTimer timergarage2;
 int flag2 = 0;
 
 void notifyUptime2(){ 
 int isButtonPressed2 = digitalRead(D5);
 if (digitalRead(D5)==LOW && flag2 ==0) {Blynk.notify (" Attenzione : Garage aperto da 10 minuti");
 flag2 =1 ;}
 if (isButtonPressed2 ==HIGH ){flag2 =0;}
}

void setup() { 
  Serial.begin(115200); 
  pinMode (D2,INPUT_PULLUP); // Sensore magnetico inferiore
  pinMode(D3,INPUT);         // DHT 11
  pinMode (D5,INPUT_PULLUP); // Sensore magnetico superiore
  digitalWrite (D1,LOW);     // Pulsante garage

  Serial.println("Booting");
  WiFi.mode(WIFI_STA); 
  WiFi.begin(ssid, pass);
  
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
  Serial.println("Connesseione fallita ! Riavvio...");
  delay(5000);
  ESP.restart();}

  Blynk.begin(auth, ssid, pass);
 
  Blynk.email("GARAGE", "Accensione microcontrollore ESP8266 Garage ");

  dht.begin();
  timer.setInterval(5020L, sendSensor);// Setup a function to be called every  5 second
  
  timergarage1.setInterval(300010L, notifyUptime); // 5 minuti
  timergarage2.setInterval(600000L, notifyUptime2); //10 minuti
  
  // Port defaults to 8266// ArduinoOTA.setPort(8266); 
  ArduinoOTA.setHostname("Garage");
  ArduinoOTA.setPassword((const char *)"*****");
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Blynk.run();
  ArduinoOTA.handle();
  timer.run(); //DHT11
  timergarage1.run(); 
  timergarage2.run();
  
  //INIZIO PULSANTE D1 (5)//
  if (digitalRead(D1)==HIGH){
  delay (300);
  digitalWrite (D1,LOW);} 
  //FINE PULSANTE D1 (5)
    }

@seby please edit your post using the pencil icon at the bottom of the post and insert triple backticks at the beginning and end of your code.
Triple backticks look like this:
```

Pete.

Thanks Pete, I think it’s correct

I think you’re using the timer in the wrong way.
For example:
timergarage1 will have timeout @ minute No. 5, and the sensor (D5) has been activated from minute No. 4. So, at minute No. 5 (1 minute from D5 activated, the notification will be sent). This time difference is random, depending on when D5 is activated compared to timergarage1 timeout (and we have no control).

Try this modified code from yours and note those lines have been changed.

I just have a look at the timers’ issue, you can still improve the setup() a little bit.

#ifndef Pins_Arduino_h
  #define Pins_Arduino_h
  #define PIN_WIRE_SDA (4)
  #define PIN_WIRE_SCL (5)
  
  static const uint8_t SDA = PIN_WIRE_SDA; 
  static const uint8_t SCL = PIN_WIRE_SCL;
  static const uint8_t D0   = 16;  
  static const uint8_t D1   = 5;  
  static const uint8_t D2   = 4;
  static const uint8_t D3   = 0;   
  static const uint8_t D4   = 2;  
  static const uint8_t D5   = 14;
  static const uint8_t D6   = 12;  
  static const uint8_t D7   = 13; 
  static const uint8_t D8   = 15;
  static const uint8_t RX   = 3;   
  static const uint8_t TX   = 1;
  
  #include "../generic/common.h"
#endif /* Pins_Arduino_h */

#define BLYNK_PRINT   Serial    // Comment this out to disable prints and save space
//#define BLYNK_DEBUG

#define USE_BLYNK_WM      false       // https://github.com/khoih-prog/Blynk_WM

// Not use #define USE_SPIFFS  => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS    false => using EEPROM for configuration data in WiFiManager
// #define USE_SPIFFS    true  => using SPIFFS for configuration data in WiFiManager
// Be sure to define USE_SPIFFS before #include <BlynkSimpleEsp8266_WM.h>

//#define USE_SPIFFS                  true
#define USE_SPIFFS                  false

#if (!USE_SPIFFS)
  // EEPROM_SIZE must be <= 4096 and >= CONFIG_DATA_SIZE (currently 172 bytes)
  #define EEPROM_SIZE    (4 * 1024)
  // EEPROM_START + CONFIG_DATA_SIZE must be <= EEPROM_SIZE
  #if USE_SSL
    #define EEPROM_START   (EEPROM_SIZE - 512)
  #else
    #define EEPROM_START   (EEPROM_SIZE - 1024)
  #endif
#endif

// Force some params in Blynk, only valid for library version 1.0.1 and later
#define TIMEOUT_RECONNECT_WIFI                    10000L
#define RESET_IF_CONFIG_TIMEOUT                   true
#define CONFIG_TIMEOUT_RETRYTIMES_BEFORE_RESET    5
// Those above #define's must be placed before #include <BlynkSimpleEsp8266_WM.h>

#if USE_BLYNK_WM
#include <BlynkSimpleEsp8266_WM.h>
#else
#include <BlynkSimpleEsp8266.h>

char ssid[] = "rete";
char pass[] = "**********";
char auth[] = "********************";
#endif

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#include <Ticker.h>

#include <DHT.h>
#define DHTPIN D3          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11

DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;
//BlynkTimer timergarage1;
//BlynkTimer timergarage2;

// Add BUTTON_PIN to control relay
#define BUTTON_PIN            D7   
#define RELAY_PIN               D1
#define MAG_SENSOR_LOW_PIN      D2
#define MAG_SENSOR_HIGH_PIN     D5

int timergarage1;
int timergarage2;

int flag = 0;
int flag2 = 0;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Impossibile leggere il sensore DHT !");
    return;
  }
  
  Blynk.virtualWrite(V2, t);  Blynk.virtualWrite(V3, h);
}

void notifyUptime()
{
  int isButtonPressed = digitalRead(MAG_SENSOR_HIGH_PIN);
  
  if (isButtonPressed == LOW && flag == 0) 
  {
    Blynk.notify ("Attenzione : Garage aperto da 5 minuti ");
    flag =1 ;
  }

#if 0
  if (isButtonPressed == HIGH )
  {
    flag =0;
  }
#endif
}

void notifyUptime2()
{ 
  int isButtonPressed2 = digitalRead(MAG_SENSOR_HIGH_PIN);
  
  if (isButtonPressed2 == LOW && flag2 == 0) 
  {
    Blynk.notify (" Attenzione : Garage aperto da 10 minuti");
    flag2 =1;
  }

#if 0
  if (isButtonPressed2 == HIGH )
  {
    flag2 =0;
  }
#endif
}

Ticker relayTicker;
#define RELAY_CLICK_TIME_MS       300L

void controlRelay(byte relayState) 
{ 
  Serial.println("Relay "+ relayState? "HIGH" : "LOW");
  digitalWrite(RELAY_PIN, relayState); 
}

void processing()
{
  int isButtonPressed = digitalRead(MAG_SENSOR_HIGH_PIN);

  if (isButtonPressed == HIGH )
  {
    flag  = 0;
    flag2 = 0;
  }
  else if (isButtonPressed == LOW && flag == 0 && flag2 == 0) 
  {
    timer.restartTimer(timergarage1);
    timer.restartTimer(timergarage2);
  }

  if (digitalRead(BUTTON_PIN) == LOW) 
  {
    controlRelay(HIGH);
    relayTicker.once_ms(RELAY_CLICK_TIME_MS, controlRelay, (byte) LOW);
  }


#if 0
  //INIZIO PULSANTE RELAY_PIN (D1) (5)//
  if (digitalRead(RELAY_PIN)==HIGH)
  {
    delay (RELAY_CLICK_TIME_MS);
    digitalWrite (RELAY_PIN,LOW);
  }
  //FINE PULSANTE RELAY_PIN (D1) (5)
#endif
}

void setup() 
{
  Serial.begin(115200); 
  pinMode(MAG_SENSOR_LOW_PIN, INPUT_PULLUP); // Sensore magnetico inferiore
  pinMode(DHTPIN,INPUT);         // DHT 11
  pinMode(MAG_SENSOR_HIGH_PIN,INPUT_PULLUP); // Sensore magnetico superiore
  
  pinMode(BUTTON_PIN,INPUT_PULLUP); // Sensore magnetico superiore
  pinMode(RELAY_PIN,OUTPUT); // Sensore magnetico superiore
   
  digitalWrite(RELAY_PIN,LOW);     // Pulsante garage
  
  Serial.println("Booting");
  
  #if USE_BLYNK_WM
    Blynk.begin();
  #else
    WiFi.mode(WIFI_STA); 
    WiFi.begin(ssid, pass);
    
    while (WiFi.waitForConnectResult() != WL_CONNECTED) 
    {
      Serial.println("Connesseione fallita ! Riavvio...");
      delay(5000);
      ESP.restart();
    }
    
    Blynk.begin(auth, ssid, pass);
  #endif
  
  //Blynk.email("GARAGE", "Accensione microcontrollore ESP8266 Garage ");
  
  dht.begin();
  timer.setInterval(5020L, sendSensor);// Setup a function to be called every  5 second
  
  //timergarage1.setInterval(300010L, notifyUptime); // 5 minuti
  //timergarage2.setInterval(600000L, notifyUptime2); //10 minuti

  timergarage1 = timer.setInterval(300010L, notifyUptime); // 5 minuti
  timergarage2 = timer.setInterval(600000L, notifyUptime2); //10 minuti
  timer.setInterval(1011L, processing); // Setup a function to be called every 1 second
  
  // Port defaults to 8266// ArduinoOTA.setPort(8266); 
  ArduinoOTA.setHostname("Garage");
  ArduinoOTA.setPassword((const char *)"*****");
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() 
{
  Blynk.run();
  ArduinoOTA.handle();
  
  timer.run(); //DHT11

  //timergarage1.run(); 
  //timergarage2.run();  
}

This is just a quick and easy fix. It’s better to use Ticker for those functions notifyUptime() and notifyUptime2(). But you can modify the code later.

Updated
Just remove the mess out of loop()

2 Likes

Hi khoih Thanks for your precious help, unfortunately loading the sketch I don’t receive notifications on blynk, but thanks to your help I discovered the #include <Ticker.h> library where through the Ticker example, I managed to make the timer work optimally