Sleep and WiFi Connecting

I notice that sometimes my device doesn’t properly go to sleep or doesn’t want to wake up properly.
Sometimes everything switches of but the connecting light keeps flashing green (rapidly in connection state).

It seems that this happens when I switch of when WiFi is connecting. Does sleep (stop mode) stop everything directly or do I have to stop some things manually? I’m running in manual and system thread mode.

The problem was that WiFi has some calls (like credentials, connect) that block the loop kind of long. This made my application seem unresponsive, because it didn’t directly respond.

I’ve ended up using a Timer that calls a readInputs function. In this function I do all the digitalRead/analogReads and act based on that. This function is called each ms, also when WiFi is busy.

I’ve found out that interrupts don’t work good with debouncing a switch, so this will let me implement the ClickButton library as well.

It also makes it possible to read analog sensors.

If the WiFi calls are done the Photon is set the sleep in the loop (based on a flag set in the readInputs function.

I haven’t yet come across any switch I couldn’t get to work with interrupts.
It might require some extra coding, but that should still be doable.

I use a switch like this:

I use a change interrupt, so it triggers multiple times on a press (the button remembers it’s state). Of course I need to detect the last change, because that is the stable one. Then I need to take an action. Problem is that I could do that in the loop, but the WiFi blocking code makes that to unreliable. I was thinking to start a timer from the interupt, however that didn’t seem to work directly (I now the ISR call) and I was not patient enough to explore that further. Also because I needed to monitor an analog input, for which I can’t use an interrupt either.