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.