Delay not working?

Hi all,

I have a 8 hour delay in my program, but after 8 hours the stuff after the delay command is not executing!

Have I exceeded the delay time?

Section:

		/* IF REBOOT ALL COMMAND RECEIVED */
		if (req.indexOf("/OFFOVERNIGHT") != -1) { //checks if you clicked ON
			WiFi.disconnect();
			/* TURN ALL WIFI COMPONENTS OFF (RELAY NC)*/
			digitalWrite(relay1, LOW);
			digitalWrite(relay2, LOW);
			digitalWrite(relay3, LOW);
			digitalWrite(relay4, LOW);
			Serial.println("Off Overnight");
			
			/* DELAY 8 HOURS */
			delay(offMilliseconds);

			/* TURN THE WIFI COMPONENTS ALL BACK ON WITH A 30 SECOND DELAY (RELAY NC) */
			digitalWrite(relay1, HIGH);
			delay(delayTime);
			digitalWrite(relay2, HIGH);
			delay(delayTime);
			digitalWrite(relay3, HIGH);
			delay(delayTime);
			digitalWrite(relay4, HIGH);
			delay(60000); //wait for WiFi to come back up
			ResetWiFi();
		}

All code:

/*
D1 = GPIO 5
D2 = GPIO 4
D3 = GPIO0
D4 = GPIO2
*/

#include <ESP8266WiFi.h>
const char* ssid = "****"; //ssid
const char* password = "****"; //password
const char* host = "****"; //ESP8266 IP address

bool startServer = false; //varaible used to tell if the the server to restart if WiFi connection lost the connected

/* INITIALIZE RELAY PINS */
int relay1 = 5;
int relay2 = 4;
int relay3 = 0;
int relay4 = 2;

int delayTime = 30000; //delay between each relay turning on

unsigned long offMilliseconds = 28800000L; //delay for the "off overnight" function

WiFiServer server(301); //just pick any port number you like

void setup() {
	/* BEGIN SERIAL */
	Serial.begin(115200);
	delay(10);
	Serial.println(WiFi.localIP());

	/* SET OUTPUT PINS */
	pinMode(relay1, OUTPUT);
	pinMode(relay2, OUTPUT);
	pinMode(relay3, OUTPUT);
	pinMode(relay4, OUTPUT);

	/* TURN ALL WIFI COMPONENTS OFF (RELAY NC)*/
	digitalWrite(relay1, LOW);
	digitalWrite(relay2, LOW);
	digitalWrite(relay3, LOW);
	digitalWrite(relay4, LOW);
	Serial.println("Reboot All");
	/* TURN THE WIFI COMPONENTS ALL BACK ON WITH A 30 SECOND DELAY (RELAY NC) */
	delay(delayTime);
	digitalWrite(relay1, HIGH);
	delay(delayTime);
	digitalWrite(relay2, HIGH);
	delay(delayTime);
	digitalWrite(relay3, HIGH);
	delay(delayTime);
	digitalWrite(relay4, HIGH);

	delay(60000); //wait for WiFi to come back up

	/* CONNECT TO WIFI */
	Serial.println();
	Serial.println();
	Serial.print("Connecting to ");
	Serial.println(ssid);

	WiFi.begin(ssid, password); //try to connect to WiFi

	while (WiFi.status() != WL_CONNECTED) { //if not connected to WiFi
		delay(500);
		Serial.print(".");
	}
	Serial.println("");
	Serial.println("WiFi connected");

	server.begin(); //start the server
	Serial.println("Server started");

	Serial.println(WiFi.localIP()); //print your IP
}

void loop() {

	/* CHECK IF A CLIENT HAS CONNECTED */
	WiFiClient client = server.available();
	if (!client) {
		return;
	}

	/* WAIT UNTIL THE CLIENT SENDS SOME DATA */
	while (!client.available()) {
		delay(1);
	}

	/* READ THE FIRST LINE OF THE REQUEST */
	String req = client.readStringUntil('\r');
	client.flush();

	/* MATCH THE REQUEST */
	if (req.indexOf("") != -10) {  //checks if you're on the main page

		/* IF REBOOT MODEM COMMAND RECEIVED */
		if (req.indexOf("/REBOOTMODEM") != -1) { //checks if you clicked REBOOT MODEM
			WiFi.disconnect();
			digitalWrite(relay1, LOW);
			Serial.println("Reboot Modem");
			delay(delayTime);
			digitalWrite(relay1, HIGH);
			delay(60000); //wait for WiFi to come back up
			ResetWiFi();
		}

		/* IF REBOOT ALL COMMAND RECEIVED */
		if (req.indexOf("/OFFOVERNIGHT") != -1) { //checks if you clicked ON
			WiFi.disconnect();
			/* TURN ALL WIFI COMPONENTS OFF (RELAY NC)*/
			digitalWrite(relay1, LOW);
			digitalWrite(relay2, LOW);
			digitalWrite(relay3, LOW);
			digitalWrite(relay4, LOW);
			Serial.println("Off Overnight");
			
			/* DELAY 8 HOURS */
			delay(offMilliseconds);

			/* TURN THE WIFI COMPONENTS ALL BACK ON WITH A 30 SECOND DELAY (RELAY NC) */
			digitalWrite(relay1, HIGH);
			delay(delayTime);
			digitalWrite(relay2, HIGH);
			delay(delayTime);
			digitalWrite(relay3, HIGH);
			delay(delayTime);
			digitalWrite(relay4, HIGH);
			delay(60000); //wait for WiFi to come back up
			ResetWiFi();
		}

		/* IF REBOOT ALL COMMAND RECEIVED */
		if (req.indexOf("/REBOOTALL") != -1) { //checks if you clicked ON
			WiFi.disconnect();
			/* TURN ALL WIFI COMPONENTS OFF (RELAY NC)*/
			digitalWrite(relay1, LOW);
			digitalWrite(relay2, LOW);
			digitalWrite(relay3, LOW);
			digitalWrite(relay4, LOW);
			Serial.println("Reboot All");
			/* TURN THE WIFI COMPONENTS ALL BACK ON WITH A 30 SECOND DELAY (RELAY NC) */
			delay(delayTime);
			digitalWrite(relay1, HIGH);
			delay(delayTime);
			digitalWrite(relay2, HIGH);
			delay(delayTime);
			digitalWrite(relay3, HIGH);
			delay(delayTime);
			digitalWrite(relay4, HIGH);
			delay(60000); //wait for WiFi to come back up
			ResetWiFi();
		}
	}

	else {
		Serial.println("invalid request");
		client.stop();
		return;
	}

	/* PREPARE THE RESPONSE */
	String response = "HTTP/1.1 200 OK\r\n";
	response += "Content-Type: text/html\r\n\r\n";
	response += "<!DOCTYPE HTML>\r\n<html>\r\n";
	response += "
<p style=' border: 2px dashed blue; vertical-align:center; text-align:center; color:blue; font-size:42px; margin: 20px 5px; padding: 30px 10px;'>Please close this window once you have selected an option!</p>";
	response += "

";
	response += "
<p style='text-align:center;'><input type=\"button\" style='text-align: center; font-size: 40px; width: 1000px; height: 70px; color: blue' name=\"rebootall\" value=\"Reboot All\" onclick=\"location.href='/REBOOTALL'\"></p>";
	response += "

";
	response += "
<p style='text-align:center;'><input type=\"button\" style='text-align: center; font-size: 40px; width: 1000px; height: 70px; color: blue' name=\"rebootmodem\" value=\"Reboot Modem\" onclick=\"location.href='/REBOOTMODEM'\"></p>";
	response += "

";
	response += "
<p style='text-align:center;'><input type=\"button\" style='text-align: center; font-size: 40px; width: 1000px; height: 70px; color: blue' name=\"offovernight\" value=\"Off Overnight (8 Hrs)\" onclick=\"location.href='/OFFOVERNIGHT'\"></p>";
	response += "</html>\n"; 


	client.flush();

	/* SEND THE RESPONSE TO THE CLIENT */
	client.print(response);
	delay(1);
}

void ResetWiFi() {
	WiFi.begin(ssid, password); //try to connect to WiFi
	while (WiFi.status() != WL_CONNECTED) { //if not connected to WiFi
		delay(500);
		Serial.print(".");
		startServer = true;
	}

	if (startServer == true) {
		Serial.println("");
		Serial.println("WiFi connected");
		server.begin(); //start the server
		Serial.println("Server started");
		Serial.println(WiFi.localIP()); //print your IP
		startServer = false;
	}
}

Any help with this would be awesome because I'm stumped!

Thanks,

Zeb

The time delay time is assigned at the top of the program like this:

unsigned long offMilliseconds = 28800000L;

Does a much shorter delay, say 5 minutes, work ?

You are not re-trying the WiFi.begin(). You try it once and assume it will eventually connect. The "WiFi101->WiFiWebServer example uses this:

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);


    // wait 10 seconds for connection:
    delay(10000);
  }

Note: The global 'status' variable is initialized to WL_IDLE_STATUS

int status = WL_IDLE_STATUS;

If 'status' was not initialized it would be more proper to write:

  // attempt to connect to WiFi network:
  do {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  } while (status != WL_CONNECTED);

Or at least initialize 'status' to something other than WL_CONNECTED before the loop. If you don't and the last time status was set it was left as WL_CONNECTED then the loop would not execute and therefore the WiFi.begin() would not be called!