Relays, weather station. Ok code- keeps disconnecting

I’m using this code I created copying, pasting and modifying accordingly to my schematics.
It features control of 2 relays for front door and gate, an emergency button that opens the front door (in case internet down) a dallas DS18B20 and a Lm35, so I can have internal and ext temperatures display.

It worked perfectly until I changed it and added a bmp120 in place of the lm35.
Now it connects, but after a few minutes I lose connection

Could you please check it? I’ts currently taking 91% of my arduino uno space.


#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 3
OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);

 //BUTTON

 const int buttonPin = 5;  // 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 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);




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

//REDBUTTON EMERGENCY
// initialize the digital pin as an output for LED.
 

   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input
    
}

void loop()
{
  Blynk.run();

  Serial.println();

  
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);
   
   }

//BMP180
 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);
        }
      }
    }
  }
   
}

You need to edit your post (using the pencil icon at the bottom of the post) and format your code so that it displays correctly…
Blynk - FTFC

You also need to read-up on keeping your void loop clean and using timers to call functions.
Personally, I’d also attach an interrupt to your ‘emergency’ button, so that pressing it triggers a function rather than having to interrogate the digital pin on a regular basis.

Pete.

fixed the code diplay

1 Like

Good, now read these:


Pete.

I think I’ve learned how to use timers, now it’s not disconnecting anymore.

#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 3

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


//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 = 5;  // 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 bmpdallas()

{
  sensors.requestTemperatures();
  Dtemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(10, 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);
        }
      }
    }
  }

}
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);
  timerdallas.setInterval(2500L, bmpdallas);
}

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

//REDBUTTON EMERGENCY
// initialize the digital pin as an output for LED.
 
   pinMode(buttonPin, INPUT);  // initialize the pushbutton pin as an input


}

void loop()
{
  Blynk.run();
  timerbmp.run();
  timerdallas.run();
  
//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);
   
   }

}

But I might need some help with the interrupt implementation with my code. Is it the command attachInterrupt?

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();
}