Firstly - apologies if these are dumb questions, but I am running into a few issues with sleep mode.
The set up is simple, a push button wired to D0. On setup() it sends a connect request to a node web server. On each button press the Photon should wake up from sleep and send a request, then go back to sleep again.
If i remove the 5 second delay before sending the Photon to sleep it refuses to wake on interrupt. It WILL go to sleep, but it will never wake up. Adding the delay sends it to sleep and wakes it again. Any idea why this is and whether there is a better way?
The first time the Photon is sent into sleep within the loop it will always wake up again (without interrupt), run the loop then go back to sleep.
The code above works, but sends an unnecessary request when the device is first started up. I want to avoid the request going out unless the user has pressed the push button.
I’m guessing that the http request doesn’t block, and the loop is executing very very fast. You’ve received a 200 response, but it’s possible the rest of the response hasn’t been received yet. Can you wait to sleep until you’re sure the full response was received? Or try flushing / closing the http request after the response is received?
You might also need to make sure that “response” is cleared / re-initialized on startup, to make sure you’re not going to sleep before you’ve even made the request, etc, etc.
I don’t think the issue is related to the HTTPClient as this is more about the photon coming back out of sleep without the interrupt signal and the need for the 5 second delay or the device won’t wake up at all.
Here’s a stripped down version of setup() and loop() to illustrate the problem better:
void setup() {
// initialize the pushbutton pin as an input:
pinMode(D0, INPUT);
deviceIdString = Spark.deviceID();
Serial.begin(9600);
}
void loop() {
Serial.println("Within Loop");
if (firstLoop) {
Serial.println("firstLoop is true");
firstLoop = false;
//delay(5000);
System.sleep(D0,RISING);
}
}
In this example the Photon goes to sleep correctly but will never wake up from the D0 interrupt. If i uncomment the delay(5000) the device goes to sleep, but then immediately wakes up and runs through the loop again.
I am sure I am missing something obvious in how sleep works, but would love your thoughts.
For now I can continue to concept with a bit of a hack, but obviously want to remove it as soon as I can.
Below is the latest code, works fine - but having to ignore the 2nd loop due to the Photon coming out of sleep unnecessarily and still stuck with the 5 second delays.
void setup() {
// initialize the pushbutton pin as an input:
pinMode(D0, INPUT);
deviceIdString = Spark.deviceID();
request.hostname = "olcollective.doesntexist.com";
request.port = 80;
request.path = "/connect";
request.body = "{\"deviceId\":\""+deviceIdString+"\"}";
http.post(request, response, headers);
}
void loop() {
loopCount++;
// Ignore first 2 loops
// First loop is expected, 2nd loop is a bug?
if (loopCount <3) {
delay(5000);
System.sleep(D0,RISING);
return;
}
bool sleep = false;
request.hostname = "olcollective.doesntexist.com";
request.port = 80;
request.path = "/request";
request.body = "{\"deviceId\":\""+deviceIdString+"\"}";
// Post request
http.post(request, response, headers);
if (response.status == 200) {
sleep = true;
}
if (sleep) {
delay(5000);
System.sleep(D0,RISING);
}
}
This is great news, ive been trying to find a simple code to demonstrate it! on of my photons stops waking all the time… and its the one outside on my water meter in a sealed box. i have another photon in exactly the same setup, same battery shield and everything (only difference is external antenna- but rssi is similar) running the exact same code that doesnt have the issue.
i have been using deep sleep and it sleeps and doesnt always wake up… from time or wkp pin
System.sleep(SLEEP_MODE_DEEP, 600);
it works for for a few hours, waking evey 10mins or when wkp pin is toggled… and then just gives up, neither the wkp pin or the 10min wake it back up and i have to reset the photon.
next step is to swap the photons and try and see why one stops all the time and the other doesn’t.
The not waking up from deep sleep issue have been around for a few months atleast, but it seems deep sleep is not getting much mileage to cause it to be a priority, hopefully in 4.8
We had a similar “issue” on this forum already and the reason was the power supply and not the Photon.
Try a different power supply (e.g. computer USB port without power safe).
Update: I’ve just tried a 10 minute deep-sleep (of my Photon ;-)) and it just works as expected.
as you suggested the problem was the power supply! I used a TP-Link powerbank, probably it has some kind of powersaving that interrupts the power supply…
Thank you, for your help!