My ESP8266 project has stopped working correctly, as I have been relying on time from the wifi router to determine if a process should commence. It worked fine for weeks, but started behaving strangely. Now when I reboot the ESP8266 it always starts at midnight, and increments current time from there. I have rebooted the Wifi router and the esp is connecting fine.
Start your code here
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
const char* ssid = “xxxxxxxxxxxxxxxxxxxxxx”;
const char* password = “yyyyyyyyyyyyyyyyyyy”;
const unsigned long flapOpenTime = 20000; //in addition to timing of piR pot
unsigned long previousTime = 0; //priortime flap opened
unsigned long priorTime = 0; // prior time for WiFi timeout
const long timeoutTime = 2000; // Define timeout time in milliseconds (example: 2000ms = 2s)
const int HBridge2 = D1; //LOW state switches HBridge to linear actuator to close flap GPIO 5
const int HBridge1 = D2; //LOW state switches HBridge to linear actuator to open flap GPIO 4
const int pIRoUT = D5; // HIGH signal when motion detected GPIO 14
const int pIRiN = D6; // HIGH signal when motion detected GPIO 12
const int masterSwitch = D3; //manual switch to close flap- GPIO 0 – boot fails if pulled low – so should be switch off when starting system
const int doorSensor = D7; // reed switch to check if door is closed before starting system GPIO 13
const int motorEnable = D8;//used to switch on L298N motor driver
int priorpIRiNStatus;//used to see if the PIR status has changed
int priorpIRoUTStatus; //used to see if the PIR status has changed
int priorSystemState;//only required to allow serial print when state changes
int bedTime = 22;//system doesn’t allow exit after this time
int curfewTime = 24; //system wont open after this time
int wakeTime = 7; //system wont open before this time
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “pool.ntp.org”);
void openFlap()
{
delay (25);
digitalWrite(motorEnable, HIGH); //energise actuator
digitalWrite(HBridge1, LOW);
digitalWrite(HBridge2, HIGH);// turn acuator to extend
}//openFlap()
void closeFlap()
{
delay (25);
digitalWrite(motorEnable, HIGH);// energise actuator
digitalWrite(HBridge1, HIGH);// turn acuator to contract
digitalWrite(HBridge2, LOW);//
}//closeFlap()
enum _state_enum
{
START,
SYSTEM_READY, //checks door is closed, master switch is on and correct time of day
MONITOR_ACTIVITY, // system waiting for valid activity from PIR sensors
CMD_OPEN_FLAP, //transition state when flap is opening
WAKE_UP_TIME,//open flap in the morning to alert dogs that system is back on
BED_TIME,
MONITOR_OPEN_FLAP,//make sure flap doesn’t close while dog is walking through
CMD_CLOSE_FLAP, //transition state when flap is closing
};
_state_enum systemState = START;
// convert State Enu into Char for Serial print
const char * messages[] = {
“START”,
“SYSTEM_READY”,
“MONITOR_ACTIVITY”,
“CMD_OPEN_FLAP”,
“WAKE_UP_TIME”,
“BED_TIME”,
“MONITOR_OPEN_FLAP”,
“CMD_CLOSE_FLAP”
};
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.print (“Connected to: “);
Serial.println(WiFi.localIP());
Serial.println (” wait 30 seconds for PIRs to stabalize”);
pinMode(HBridge1, OUTPUT);// set pin as output for HBridge 1
pinMode(HBridge2, OUTPUT);// set pin as output for HBridge 2
pinMode(pIRiN, INPUT_PULLUP);
pinMode(pIRoUT, INPUT_PULLUP);
pinMode(doorSensor, INPUT_PULLUP);
pinMode(masterSwitch, INPUT_PULLUP);
pinMode(motorEnable,OUTPUT);
digitalWrite(motorEnable, LOW); //turn off HBridge
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT 0 = 0
timeClient.setTimeOffset(0);
closeFlap();
delay(30000); //wait for PIRs to stabalize
}
void loop()
{
timeClient.update();
int currentHour = timeClient.getHours();
int currentMin = timeClient.getMinutes();
int currentSecs = timeClient.getSeconds();
int doorSensorStatus = digitalRead(doorSensor);
int masterSwitchStatus = digitalRead(masterSwitch);
unsigned long currentTime = millis(); // Current time
int pIRiNStatus = digitalRead(pIRiN);
int pIRoUTStatus = digitalRead(pIRoUT);
if (systemState != priorSystemState)
{
Serial.print (F(“States – FROM:- “));
Serial.print (messages[priorSystemState]);
Serial.print (F(” TO -> “));
Serial.println (messages[systemState]);
Serial.print (F(“doorSensorStatus:- “));
Serial.println (doorSensorStatus);
Serial.print (F(“masterSwitchStatus:- “));
Serial.println (masterSwitchStatus);
Serial.print (F(“Current Time:- “));
Serial.print (currentHour);
Serial.print (F(“H:”));
Serial.print (currentMin);
Serial.print (F(“m:”));
Serial.print (currentSecs);
Serial.println (F(“s:”));
Serial.print (F(“PriorPIR / CurrentPIR – INSIDE “));
Serial.print (priorpIRiNStatus);
Serial.print (F(” / “));
Serial.println (pIRiNStatus);
Serial.print (F(“PriorPIR / CurrentPIR – OUTSIDE “));
Serial.print (priorpIRoUTStatus);
Serial.print (F(” / “));
Serial.println (pIRoUTStatus);
Serial.print (F(“HBridge 1 / HBridge 2:- “));
Serial.print (digitalRead(HBridge1));
Serial.print (F(” / “));
Serial.println (digitalRead(HBridge2));
Serial.print (F(“Flap Elapsed Time: “));
Serial.println (currentTime – previousTime);
Serial.print(F(“motorEnable Pin: “));
Serial.println(digitalRead(motorEnable));
Serial.println (F(“***********************”));
}
switch (systemState)
{
//Check that system is switched on
case START:
priorSystemState = systemState;//used to conditionally serial print current State
if(masterSwitchStatus == 0 && doorSensorStatus == 0)
{
systemState = SYSTEM_READY;
}
else
systemState = START;
break;
case SYSTEM_READY:
priorSystemState = systemState; //used to conditionally serial print current State
if(masterSwitchStatus == 0 && doorSensorStatus == 0
&& currentHour >= wakeTime && currentHour <= bedTime)
{
systemState = MONITOR_ACTIVITY;
}
else if(currentHour >= bedTime && currentHour <= curfewTime)
{
systemState = BED_TIME;
}
else systemState = START;
break;
case MONITOR_ACTIVITY: // If either PIR sensor went from LOW to HIGH then open the flap.
priorSystemState = systemState;
if (((pIRiNStatus != priorpIRiNStatus && pIRiNStatus == 1
||
(pIRoUTStatus != priorpIRoUTStatus && pIRoUTStatus == 1))
&&
currentHour >= wakeTime && currentHour <= bedTime))
{
systemState = CMD_OPEN_FLAP;
previousTime = currentTime;
}
priorpIRiNStatus = pIRiNStatus;
priorpIRoUTStatus = pIRoUTStatus;
break;
case BED_TIME: // If OUTSIDE PIR sensor went from LOW to HIGH then open the flap.
//Leeway time to let the dogs in ONLY, but does not allow them out after 11pm.
priorSystemState = systemState;
if ((pIRoUTStatus != priorpIRoUTStatus && pIRoUTStatus == 1)//only monitor outside PIR
&& (currentHour >= bedTime && currentHour <= curfewTime))
{
systemState = CMD_OPEN_FLAP;
previousTime = currentTime;
}
else if (currentHour >= bedTime && currentHour <= curfewTime)
{
systemState = BED_TIME;
}
else
{
systemState = SYSTEM_READY;
}
priorpIRoUTStatus = pIRoUTStatus;
break;
case WAKE_UP_TIME:
priorSystemState = systemState;
if(currentHour == wakeTime)
{
systemState = CMD_OPEN_FLAP;
previousTime = currentTime;
}
break;
case CMD_OPEN_FLAP:
priorSystemState = systemState;
openFlap();
systemState = MONITOR_OPEN_FLAP;
break;
case MONITOR_OPEN_FLAP:
priorSystemState = systemState;
if (((pIRiNStatus == 0 && pIRoUTStatus == 0) && (currentTime – previousTime >= flapOpenTime))
|| masterSwitchStatus == 1
|| doorSensorStatus == 1)
{
systemState = CMD_CLOSE_FLAP;
}
break;
case CMD_CLOSE_FLAP:
priorSystemState = systemState;
closeFlap();
systemState = START;
break;
priorpIRiNStatus = pIRiNStatus;
priorpIRoUTStatus = pIRoUTStatus;
}
}
Hi.
So, you say that this same code worked just fine before?
Can you try a simple code to get the time and see if it gets the time right?
You can use this example: https://randomnerdtutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/.
Regards,
Sara
Hi Sara,
Yes it had been running fine for weeks. I wonder if the Millis() overfilled? (excuse lack of technical lingo!)
I disconnected ESP from my project and reloaded code and it then began to read correct time. I can’t really point to any reason why this worked.
For now, I’m happy to close the query!
Thank you
Paul