Relays, weather station. Ok code- keeps disconnecting

I think this bit of code is redundant:

It looks like you’re using an Arduino UNO, which only allows interrupts to be attached to pins 2 and 3

You’ll therefore need to change this:
const int buttonPin = 5; // The number of the Pushbutton pin

to 2 or 3 instead of 5 and rewire your button to the appropriate pin

Change the last part of your void setup to look like this:

//REDBUTTON EMERGENCY
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
   digitalWrite(buttonPin, LOW); // Good practice to set it LOW to begin with, in case it's floating
   attachInterrupt(digitalPinToInterrupt(buttonPin), Emergency_Button, RISING) // Call the Emergency_Button function when button goes HIGH

This tells the code to run a function called void Emergency_Button() when the buttonPin goes from LOW to HIGH.

You’ll then need to add this function, and move your button code from void loop into it.

Your current Emergency Button code in void loop is a bit of a mess, so I’m not going to touch it. Your comments in that bit of code refer to blinking an LED, but I guess that what you’re really doing is actuating a relay for a gate release mechanism. If this is the case then just set this gate release pin HIGH, start a timeout timer for 4 seconds then set it LOW again.

Your code is using a hard-coded pin (6) for this, whereas it would be better to refer to a variable name for the pin, as you’ve done with buttonPin. Also, you don’t appear to be declaring this pin as an output in a pinMode statement, or setting it to LOW to initialise it.

You don’t actually need to do the timers in this way. The way you’ve done it works, bit it’s a bit more long-winded than you need.

Take a read of this:

As an aside, I created a similar gate release system and had some issues as my gate release mechanism works on AC (it buzzes when released, as opposed to a simple click). Even though my relay was opto-isolated, I was getting problems with the MCU resetting when the gate was released. It seemed to be caused by EMF issues and I solved it by putting a capacitor across the gate release output terminals on the relay.From memory it was around a 220uf polyester capacitor. If you have similar unexplained issues when the gate releases then you know where to look.

Also, I used to use an Arduino and Ethernet shield, until I saw the light and moved over to using Wemos D1 Mini’s. They’re cheaper, smaller, more powerful and connect via Wi-Fi - so no need for Ethernet cables.

Pete.

#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h>
#include <SFE_BMP180.h>

//BMP180
SFE_BMP180 bmp180;

int Altitude = 155; //current altitude in meters

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

#define W5100_CS  10
#define SDCARD_CS 4
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
SimpleTimer timerbmp;

//BUTTON

const int buttonPin = 3;  // The number of the Pushbutton pin
int buttonState = 0;  // Variable for reading the pushbutton status
// Variables will change:
long previousMillis = 0;        // will store last time relay was updated
long interval = 7000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;
int gatepin = 7;  //Gate is on NO relay
int doorpin = 6;   //Door is on NO relay



void bmp180DataSend()


{

  char status;

  double T, P;
  bool success = false;

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);

        if (status != 0) {
          float comp = bmp180.sealevel(P, Altitude);

          Blynk.virtualWrite(9, comp);

          Blynk.virtualWrite(8, T);
        }
      }
    }
  }

}

void setup()
{
  Blynk.begin(auth);
  // Debug console
  Serial.begin(9600);

  //BMP180
  bool success = bmp180.begin();

  if (success) {
    Serial.println("BMP180 init success");
  }

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // set initial state of gate to 1(Closed)
  digitalWrite(7, HIGH);
  {
    timerbmp.setInterval(1500L, bmp180DataSend);
  }

  //DALLAS
  delay(1000);
  sensors.begin();

  //REDBUTTON EMERGENCY
  pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
  digitalWrite(buttonPin, LOW); // Good practice to set it LOW to begin with, in case it's floating
  attachInterrupt(digitalPinToInterrupt(buttonPin), Emergency_Button, RISING); // Call the Emergency_Button function when button goes HIGH

}

void loop()
{
  Blynk.run();
  timerbmp.run();
  Serial.println();

  sensors.requestTemperatures();
  Dtemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(10, Dtemp);

}


void Emergency_Button()

//REDBUTTON EMERGENCY
{
  previousMillis = millis();

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  //The button is pushed
  while (buttonState == HIGH) {


    currentValue = millis() - previousMillis;

    if (currentValue > interval)
    {

      // save the last time you actuated the relay
      previousMillis = millis();
      digitalWrite(6, HIGH);
      delay(4000);
      digitalWrite(6, LOW);


    }

    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);

  }
}

my em button isn’t working anymore.
I assume it’s because it needs 2 rising edges. 1 to enter the function and 1 to initialize the timed intervall for relay command.
But the second time I press the button i’m outside the interrupt again.
Hmmm…

You don’t need to do the digitalRead on the button anymore, you know it was pressed which is what fired the interrupt. You don’t need the millis() stuff either and shouldn’t be using the ```delay(4000);`` but instead using a timer.settimeout for this.

As I said before:

Also, this needs to be removed from your void loop:

and be called by timers instead. Read the links I provided about keeping your void loop clean and how to send any sensor data.

Pete.

#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#define W5100_CS  10
#define SDCARD_CS 4
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8

OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
SimpleTimer timerbmp;



//BMP180
SFE_BMP180 bmp180;

int Altitude = 155; //current altitude in meters

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";



 //BUTTON

const int buttonPin = 3;  // The number of the Pushbutton pin
int buttonState = 0;  // Variable for reading the pushbutton status
// Variables will change:
long previousMillis = 0;        // will store last time LED was updated
long interval = 7000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;

void bmp180DataSend()

{

  char status;

  double T, P;
  bool success = false;

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);

        if (status != 0) {
          float comp = bmp180.sealevel(P, Altitude);

          Blynk.virtualWrite(9, comp);

          Blynk.virtualWrite(8, T);
        }
      }
    }
  }
sensors.requestTemperatures();
  Dtemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(10, Dtemp);

//REDBUTTON EMERGENCY
  previousMillis = millis();
  
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   //The button is pushed
   while (buttonState == HIGH) {
      
      
     currentValue = millis() - previousMillis;
     
     if (currentValue > interval)
     {
     
      // save the last time you blinked the LED 
      previousMillis = millis();   
      digitalWrite(6, HIGH);
      delay(4000);
      digitalWrite(6, LOW);
      
      
     }
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   }

}
void setup()

{
  Blynk.begin(auth);
  // Debug console
  Serial.begin(9600);

  //BMP180
  bool success = bmp180.begin();

  if (success) {
    Serial.println("BMP180 init success");
  }

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // set initial state of gate to 1(Closed)
   digitalWrite(7, HIGH);


{
  timerbmp.setInterval(2500L, bmp180DataSend);
}

//DALLAS
  delay(1000);
  sensors.begin();

}

void loop()
{
  Blynk.run();
  timerbmp.run();
  }

This is the best I could do. It’s a bit more optimized. I’m happy I’ve learned how to use simpletimers

Your emergency button code is inside the function that reads the BMP180 every 2.5 seconds.
This means that the the button will have to be pressed for up to 2.5 seconds before the fact that it’s pressed is registered.
Also, it has a 4 second blocking delay built in to the gate release process, which will probably cause a Blynk disconnect every time it’s pressed.

If you don’t want to use interrupts on this button (I’m not sure why you wouldn’t) then at least put this code in a function of it’s own and call it every 500ms or so with a timer:

//REDBUTTON EMERGENCY
   buttonState = digitalRead(buttonPin);  // Check the state of the button
   
   if (buttonState == HIGH)   // If the big red button is pressed, execute this bit of code... 
   {
      digitalWrite(6, HIGH);  // Release the gate

      timer.setTimeout(4000L, []()  // Non-blocking timer function for 4 seconds
      {  
        digitalWrite(6, LOW);   // close the gate once the timer has expired 
      });  // END Timer Function
   }

You should also check that your existing code does actually execute, and allow the emergency gate release to work, when the Ethernet cable is disconnected and the Arduino is then rebooted. The Blynk.begin command may block it from working. If this is the case then report back and we’ll point you in the right direction of using Blynk.config instead.

Pete

1 Like

I don’t know why, but the moment I try more than one function, blynk keeps disconnecting


#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#define W5100_CS  10
#define SDCARD_CS 4
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8

OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
SimpleTimer timerbmp;
SimpleTimer timer;

//BMP180
SFE_BMP180 bmp180;

int Altitude = 155; //current altitude in meters

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";



 //BUTTON

const int buttonPin = 3;  // The number of the Pushbutton pin
int buttonState = 0;  // Variable for reading the pushbutton status
// Variables will change:
long previousMillis = 0;        // will store last time LED was updated
long interval = 7000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;

void bmp180DataSend()

{

  char status;

  double T, P;
  bool success = false;
  double tcorr;

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);

        if (status != 0) {
          float comp = bmp180.sealevel(P, Altitude);

          Blynk.virtualWrite(9, comp);
          tcorr = T-4,6;

          Blynk.virtualWrite(8, tcorr);
        }
      }
    }
  }


}

void emergency()

{
buttonState = digitalRead(buttonPin);  // Check the state of the button
   
   if (buttonState == HIGH)   // If the big red button is pressed, execute this bit of code... 
   {
      digitalWrite(6, HIGH);  // Release the gate

      timer.setTimeout(4000L, []()  // Non-blocking timer function for 4 seconds
      {  
        digitalWrite(6, LOW);   // close the gate once the timer has expired 
      });  // END Timer Function
   }
  
}
void setup()

{
  Blynk.begin(auth);
  // Debug console
  Serial.begin(9600);

  //BMP180
  bool success = bmp180.begin();

  if (success) {
    Serial.println("BMP180 init success");
  }

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // set initial state of gate to 1(Closed)
   digitalWrite(7, HIGH);


{
  timerbmp.setInterval(2500L, bmp180DataSend);
  timer.setInterval(500L, emergency);
}

//DALLAS
  delay(1000);
  sensors.begin();

}

void loop()
{
  Blynk.run();
  timerbmp.run();
  timer.run();  


}

Your use of the char variable ‘status’ in the code that attempts to read the temperature and humidity is very odd!
Try putting some meaningful serial print messages after each of the lines where the value of status is changed and see what you get - I don’t think it will be what you expect.

Pete.

frankly, my temperature reading 5°C off

why do you need 2 timers to run in the void loop?

That does seem a big differance… but it could depend on the sensors proximetry to other heat sources like the MCU itself.

I also do not quite understand your routines for reading the sensor values. I have a similar sensor BME280 and can successfully read all aspects, and send a bunch of data to multiple sources, every 4 seconds without any need for delay() nested if() or while() commands. And no disconnection issues. Take a look and see if you can benefit from some of the basic (AKA less code and resources) sensor read functions I use.

@Blynk_Coeur has a point… With Blynk Timer, each named timer can have up to sixteen independent instances before needing to have a separately named one. I can’t be positive, but having two named timers, each for only a single instance, may be more resource heavy than one timer with two instances.

1 Like

So… after monitoring for a week my program I’ve decided to take heed and modify it a little, as the conenction was resetting 1/2 times a day.

#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#define W5100_CS  10
#define SDCARD_CS 4
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8

OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
SimpleTimer timer;



//BMP180
SFE_BMP180 bmp180;

int Altitude = 155; //current altitude in meters

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";



 //BUTTON

const int buttonPin = 3;  // The number of the Pushbutton pin
int buttonState = 0;  // Variable for reading the pushbutton status
// Variables will change:
long previousMillis = 0;        // will store last time LED was updated
long interval = 7000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;

void bmp180DataSend()

{

  char status;

  double P;
  double T;
  bool success = false;
  float tcorr;

  status = bmp180.startTemperature();

  if (status != 0) {
    delay(1000);
    status = bmp180.getTemperature(T);

    if (status != 0) {
      status = bmp180.startPressure(3);

      if (status != 0) {
        delay(status);
        status = bmp180.getPressure(P, T);

        if (status != 0) {
          float comp = bmp180.sealevel(P, Altitude);

          Blynk.virtualWrite(9, comp);
          tcorr = T-6.2;   //this corrects the temperature wrong display value

          Blynk.virtualWrite(8, tcorr);
        }
      }
    }
  }

}

void emergencybtn()

{
  //REDBUTTON EMERGENCY
  previousMillis = millis();
  
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   //The button is pushed
   while (buttonState == HIGH) {
      
      
     currentValue = millis() - previousMillis;
     
     if (currentValue > interval)  //If the button has been pressed for over 7 secs, open it
     {
     
      // save the last time relays has been turned on
      previousMillis = millis();   
      digitalWrite(6, HIGH);      //opendoor
      delay(4000);     //give time to get in
      digitalWrite(6, LOW);    //close it
      
      
     }
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   }
}

void dallasoutside()

{
  sensors.requestTemperatures();
  Dtemp = sensors.getTempCByIndex(0);  //get temperature outside
  Blynk.virtualWrite(10, Dtemp);   //and pass it to blynk
}
  

void setup()

{
  Blynk.begin(auth);
  // Debug console
  Serial.begin(9600);

  //BMP180
  bool success = bmp180.begin();

  if (success) {
    Serial.println("BMP180 init success");
  }

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  // set initial state of gate to 1(Closed)
   digitalWrite(7, HIGH);


{
  timer.setInterval(4000L, bmp180DataSend);
  timer.setInterval(2000L, emergencybtn);
  timer.setInterval(3000L, dallasoutside);
  
  }

//DALLAS
  delay(1000);
  sensors.begin();

}

void loop()
{
  Blynk.run();
  timer.run();
  


}

as you can see i’ve implemented instances. Hopefully the dc will vanish

What is left is to fix 2 minor problems:

the delay which needs to be removed with a more compatible blynk command
and the temerature reading which is well off. Even though I’ve partially fixed it with a -6.1 correction

I’ve been given this part of code:

//REDBUTTON EMERGENCY
   buttonState = digitalRead(buttonPin);  // Check the state of the button
   
   if (buttonState == HIGH)   // If the big red button is pressed, execute this bit of code... 
   {
      digitalWrite(6, HIGH);  // Release the gate

      timer.setTimeout(4000L, []()  // Non-blocking timer function for 4 seconds
      {  
        digitalWrite(6, LOW);   // close the gate once the timer has expired 
      });  // END Timer Function
   }

This would sove the problem of the delay. It doesn’t do what I really need. This code would open a door immediately after red button is pressed. I need (for security reasons) that the relay opens after the button has been HELD for over 7seconds. Can you help?

Yes, i recall many posts about wanting a “secure” button… and I believe many have done exactly that via code. It is a hard thing to search for as the terms are generic, but here is one that references that feature.

And If I get around to it soon, I will add some code of my own to my examples topic.

1 Like

Attach a Change interrupt to the button pin.
When the interrupt triggers, check if it’s a press or release event.
If it’s a press even then start a 7 second countdown timer. If it’s a release event then delete the countdown timer.
If the 7 second countdown timer completed then run your existing gate release code.

Pete.

2 Likes

I’d like to check my code again:

I’m trying to take my code onto a wemos d1
Could you please double check it?
It seems that I cannot initialize the relays neighter low or high. Are there any errors?

#define BLYNK_PRINT Serial
#include <SimpleTimer.h>
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 3


OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
SimpleTimer timer;


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
char ssid[] = "JOENET";
char pass[] = "windurst08";

// PINS
const int gate = D6;  // The number of the gate pin 
const int door = D7;  // The number of the door  pin
const int buttonPin = D1;  // The number of the Pushbutton pin


// Variables will change:
int buttonState = 0;  // Variable for reading the pushbutton status
long previousMillis = 0;        // will store last time LED was updated
long interval = 7000;           // interval at which to blink (milliseconds)
long currentValue;
float Dtemp;



  

void emergencybtn()

{
  //REDBUTTON EMERGENCY
  previousMillis = millis();
  
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   //The button is pushed
   while (buttonState == HIGH) {
      
      
     currentValue = millis() - previousMillis;
     
     if (currentValue > interval)  //If the button has been pressed for over 7 secs, open it
     {
     
      // save the last time relays has been turned on
      previousMillis = millis();   
      digitalWrite(door, HIGH);      //opendoor
      delay(4000);     //give time to get in
      digitalWrite(door, LOW);    //close it
      
      
     }
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   }
}

void dallasoutside()

{
  sensors.requestTemperatures();
  Dtemp = sensors.getTempCByIndex(0);  //get temperature outside
  Blynk.virtualWrite(10, Dtemp);   //and pass it to blynk
}
  

void setup()

{

  // Debug console
  Serial.begin(9600);

    Blynk.begin(auth,ssid,pass);
  pinMode(buttonPin, INPUT);
  pinMode(gate, OUTPUT);
  pinMode(door, OUTPUT);


  // set initial state of gate to 1(Closed)
   digitalWrite(gate, LOW);
   digitalWrite(door, HIGH);



{
  timer.setInterval(2000L, emergencybtn);
  timer.setInterval(3000L, dallasoutside);

  
  }

//DALLAS
  delay(1000);
  sensors.begin();

}

void loop()
{
  Blynk.run();
  timer.run();
}

Do you mean a Wemos D1 Mini, or one of those strange Arduino shields that Wemos made?

Pete.

D1 Mini

What bugs me is the 3.3v usage, while almost all my modules are 5v.
Atm I’m using a usb cable that feeds wemos, while 5v feed is taken from the same cable, I stripped a 5v feed upstream of the usb socket. is that ok?

I really need more info to be able to comment correctly.

The D1 Mini has a 5v pin which is connected directly to the 5v rail from the USB socket. This can be used either as a 5v input if you don’t want to have a USB cable plugged-in to the board, or as a 5v output if you are using the USB power.
It’s worth noting that the 5v supply then goes through a 5v —> 3.3v regulator and the output from this is connected to the 3.3v pin. This 3.3v pin is really meant to be an input pin so that you can power the board from a 3.3v supply, but it is also able to be used as a logical HIGH level if you what to attach a switch between it and one of the GPIO pins. It’s not intended to be used to power other 3.3v devices and using it in this way will probably result in the on-board voltage regulator getting burnt-out as it will be overloaded.
I realise you’re not using this 3.3v pin in this way, it’s just a cautionary note for anyone else reading this.

Power supply problems are a common cause of reliability issues with MCUs and are difficult to diagnose. This is where it gets difficult to recommend the best solution for you, as I don’t now what other devices you’re trying to power, and what sort of supply you’re using for your USB power.
If you’re using a relay board then I’d certainly recommend a separate power supply for that, but you should make sure that you connect thye ground of your D1 Mini to the ground on the supply for the relay board.

The D1 Mini uses 3.3v logic levels for the digital GPIO pins, although they are able to tolerate 5v. However, if you’re doing this then make sure that you’re using a good quality 5v supply, as the cheaper one may be delivering 5.5 or even 6v, which isn’t good. You can always use a simple voltage divider to lower the logic level voltage, or use a cheap level shifter board. If you’re using the analogue input pin on the D1 Mini then you’ll almost certainly nee to go down the voltage divider/level shifter route.

I must admit, when I first changed from Arduino to Wemos D1 Mini, the 3.3v thing bugged me, but now it very rarely proves to be a problem. The only occasion when I’ve needed to use a level shifter was when I was connecting an RFID reader to a D1 Mini.

Pete.

I thought I’d answer your coding questions separately from your power supply question.

I’m not a fan of using the “D” pin references in you’re code when using ESP/MCU boards. It works, but it doesn’t help with code portability and makes it difficult to diagnose when inappropriate pins ave been selected.

I do it this way instead…

const int gate = 12;  // The number of the gate pin (Pin D6 on Wemos D1 Mini)

You’ve selected good GPIO pins (12 (D6), 13 (D7) and 5 (D1)), so presumably you’ve read this:

When you’re struggling to diagnose what’s happening with the program flow then it’s a good idea to add lots of Serial.print statements to your code and see what appears in the serial monitor; like this:

void emergencybtn()

{
  //REDBUTTON EMERGENCY
  previousMillis = millis();
  
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   //The button is pushed
   while (buttonState == HIGH) {
      
      currentValue = millis() - previousMillis;
      Serial.print("The button is pressed, currentValue = ");
      Serial.println(currentValue);
     
     if (currentValue > interval)  //If the button has been pressed for over 7 secs, open it
     {
      // save the last time relays has been turned on
      previousMillis = millis();   
      Serial.println("Opening the door....")
      digitalWrite(door, HIGH);      //opendoor
      delay(4000);     //give time to get in
      digitalWrite(door, LOW);    //close it
      Serial.println("The door is closed again")
      }
   
   // read the state of the pushbutton value:
   buttonState = digitalRead(buttonPin);
   
   }
}

I’d also change the serial baud rate from 9600 to 74880 as this is the default for the D1 Mini and it allows you to see the D1 Mini’s system messages in the same serial monitor as your debug messages.

This will almost certainly lead to Blynk disconnection problems at some point. As I’ve said before, a countdown timer is the best way to do this stuff, but as you don’t seem to be happy to go down that route, then use a second millis() comparison to determine how long the gate has been open for, in the same way that you check how long the button has been pressed.

You may also need to throw some Blynk.run(); commands into your emergency button routine to keep Blynk fed with processor time.

One final observation, you’re checking to see if your emergency button is pressed every 2 seconds:

This means that it could be 2 seconds before the 7 second test begins, requiring a button press of between 7 and 9 seconds in total to release your door. If that’s a problem then reduce this to a few hundred milliseconds.

Pete.

Thanks for the reply.

So… concerning my power supply doubts, I have 2 relays, one pushbutton and 2 dht18b20.

I believe I can feed these devices directly from the 5v passthrough of the D1 mini as the current drawshould be less than 0.2A.
I will try later today diagnosing my coils as I finally have some time off from work