How To Tell Photon Is On And When It Is Off?

Hello,

So I have photons that are uploading data using webhooks to Firebase. I would like to be able to tell when the photon is on to be able to send something to Firebase and then I want to know when the photon is off. All of this is to be showed on a Android Application. So the idea is that on the app it will show when the photon is active and when it is turned off?

Anyone know how to do this?

There are several ways depending on the tools you have at your disposition and are willing to use.

The easies would just be to hit the API endpoint https://api.particle.io/v1/devices/<deviceID>?access_token=<yourAccessToken> and check the connected and/or last_heard field.
But that is relying on the device checking in regularly or when it fails to do so the cloud guessing the device being offline.
A similar approach would be the the enpoint to list devices https://docs.particle.io/reference/api/#list-devices

Another way would be to just send a request and see whether it can be serviced or will timeout.

Or your device publishes an event whenever it comes online or is about to go offline.

All of these hold some degree of uncertainty tho’

The more reliability you need, the more laborious you’d need to lay out the logic.

How would I be able to publish an event when the device is going to go offline?

If you have planned offline periodes (e.g. System.sleep() or Particle.disconnect()) you can publish the event just before you enter these states.
That won’t work for unexpected connection loss. For such cases you’d need to use some kind of “ping” request which won’t get serviced, indirectly telling you: Not online.

Just as you’d do with a land line phone. Just ring, if noone picks up they’re likely not home - but you won’t know for sure.

2 Likes