Time Delay Relay - Best Solution

Hi There,

Hoping for some guidance. I have a setup displaying room temp/humidity to thinger.io which also turns on an exhaust fan in the event the Humidity gets too high.

I am wanting to also add in a timed exhaust to provide fresh air at 4 hour intervals in addition this (Room houses Mealworms for fishing)

I am using an Arduino Mega with an Ethernet Shield with a DFRobot SHT20 Sensor.

Wondering is it best/easiest to link the Mega to an NTP or something or can I set it up to count this long?

TIA - Code Below

#include <Wire.h>
#include "DFRobot_SHT20.h" 
#include <Ethernet.h>
#include <ThingerEthernet.h>

// SHT20 Config
DFRobot_SHT20    sht20;
const int relay1 = 8; //sets relay to pin 8 - Exhaust Relay

// thinger.io config
ThingerEthernet thing("minibeasts", "MB_MEALWORM_ROOM", "U5OiK6+DSLDl");

void setup() {
 sht20.initSHT20();                                  // Init SHT20 Sensor
    sht20.checkSHT20();                                 // Check SHT20 Sensor
  pinMode(relay1, OUTPUT); //sets pin8 as relay output
  thing["MB_MEALWORM_ROOM"] >> [](pson& out){
    out["Humidity"] = sht20.readHumidity();
    out["Temperature"] = sht20.readTemperature();

void loop() {  

if(sht20.readHumidity() > 81) digitalWrite(relay1, HIGH);

delay (300000);

digitalWrite(relay1, LOW);//Reset Alarm Relay

No experience with networking on Arduino.

You can use a millis() based timing for your four hours.

Please edit your post and embed your code between [code] and [/code], the so called code tags.


Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.


Please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

the key here is a routine called blink without delay.

you want to look at the time, what In call 'then' everything is in the past.....

millis() is the system clock and is 'now'

if ( now - then >= 4 hours ) {
turn on the fan
then = now

as you can see, you only set 'then' once, every 4 hours.

you have to have a second one for the duration for how long the fan is to run.