Hello everyone,
I am having some trouble with System.sleep(). I have a battery-powered Photon that needs to take some readings every 5 minutes and upload them to a server, but every minute the Photon must send a pulse to reset an external watchdog timer.
So I wrote a code that looks like this:
SYSTEM_THREAD(ENABLED);
int wakeUpPin = A0;
int pulsePin = WKP;
int wakeCount = 4;
boolean photonWasAsleep = true;
void readSensors() {
// here goes the previously tested and working reading routine
photonWasAsleep = true;
WiFi.off();
System.sleep(wakeUpPin, CHANGE, 60);
}
void setup() {
// initialize I/Os, serial, etc.
}
void loop() {
if(photonWasAsleep == true) {
photonWasAsleep = false;
wakeCount++;
if(wakeCount == 5) {
wakeCount = 0;
Particle.connect();
readSensors();
}
else {
digitalWrite(pulsePin, HIGH);
delay(100);
digitalWrite(pulsePin, LOW);
photonWasAsleep = true;
WiFi.off();
System.sleep(wakeUpPin, CHANGE, 60);
}
}
}
This code works perfectly for the first two cycles (meaning that the Photon wakes after 60 seconds to send the pulse and then goes back to sleep). The problem is that the third time the Photon goes to sleep it does not wake after 60 seconds, but rather after 120 seconds. In my setup this cannot happen because the external watchdog will reset the Photon if it does not get the pulse in time.
I don’t really have a readSensors() function in my code, I just placed it there as a reference of what is going on. But I can confirm that the rest of the code works without issues. Any clues on what could be wrong here? I would really appreciate your help