Heart beat status of the devices?

Use Node JS to check if a device is connected to the cloud of each device. Or if the device push a notification to the server.
What is the best way or is there a third option?

Probably the SSE from the server that indicates the status. Since the server is the one you’re interested in, that should be taken as the ‘official’ state. If the server doesn’t know it’s state, then neither will you. Luckily, a SSE gets publishes with state changes, so you should be able to monitor that. Since it’s being pushed, you don’t even have to poll the cloud.

Something like this?

unsigned long previousMillis = 0;
const long interval = 60000;

void setup() {
}

void loop() {
    iAmAlive();
}
void iAmAlive() {
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis >= interval){
        previousMillis = currentMillis;
        Spark.publish("heart", "alive", 60, PRIVATE);
    }
}

Actually, the cloud publishes a SSE already when a device (dis)connects. Open the dashboard and reset a device, you should see an ‘online’ even pop up :slight_smile:

Alright :smile: Where is the documentation for it?

Hi @fotoKrille,

@Moors7 pinged me, looks like this hasn’t been well documented, sorry about that!

When the device comes online or offline, the cloud will publish a private event from your device in this format:

// online
"spark/device/status", "online"

//offline
"spark/device/status", "offline"

//safe-mode
"spark/status/safe-mode"

and there are a bunch more. Thanks for asking, hopefully these will pop up on the docs soon.

Thanks!
David

3 Likes

Thanks, quite new to SSE :smile:
UPDATE: Got it to work :slight_smile:
So i guess that the url i am gona use is something like this? https://api.particle.io/v1/events/spark?access_token=MyToken

How quick will it send if a device is offline?
And the use this lib to read the stream?

1 Like

That’s indeed the URL you should be using.

As to how quickly it will send it, as soon as it notices it’s offline. The connection is broken after a certain timeout, and can vary a bit. Should be somewhere within a minute if I’m not too far off. The online one is pretty much instantly.

As for a library, you could use the Particle Javascript SDK, which also has something for SSEs.

1 Like

Thanks that work like a sharm!

1 Like

Anyone able to tell me if any of the data transmitted with the

event: spark/status/safe-mode

is any use to me?

event: spark/status/safe-mode
data: {"data":"{\"f\":[],\"v\":{},\"p\":6,\"m\":[{\"s\":16384,\"l\":\"m\",\"vc\":30,\"vv\":30,\"f\":\"b\",\"n\":\"0\",\"v\":7,\"d\":[]},{\"s\":262144,\"l\":\"m\",\"vc\":30,\"vv\":30,\"f\":\"s\",\"n\":\"1\",\"v\":11,\"d\":[]},{\"s\":262144,\"l\":\"m\",\"vc\":30,\"vv\":30,\"f\":\"s\",\"n\":\"2\",\"v\":11,\"d\":[{\"f\":\"s\",\"n\":\"1\",\"v\":11,\"_\":\"\"}]},{\"s\":131072,\"l\":\"m\",\"vc\":30,\"vv\":30,\"u\":\"72236D9522CC5300FFCFF5B3B17C3F52E78B965438DB28CF22546BA5F1BBD8EE\",\"f\":\"u\",\"n\":\"1\",\"v\":3,\"d\":[{\"f\":\"s\",\"n\":\"2\",\"v\":11,\"_\":\"\"}]},{\"s\":131072,\"l\":\"f\",\"vc\":30,\"vv\":30,\"u\":\"064602F0AEFC02AA314605F0DAFF02F0A8FC02A905F0F5FF054602F0A2FC03A9\",\"f\":\"u\",\"n\":\"1\",\"v\":2,\"d\":[{\"f\":\"s\",\"n\":\"2\",\"v\":1,\"_\":\"\"}]}]}"

Some info can be found here
Determining the version of system firmware on a Photon/P1

If this will be of any use to you can only be answered by … you

1 Like

Thanks @ScruffR :kissing:

1 Like

Can anyone confirm that this heartbeat functionality is still working? If so, what is the min/max delay before the device offline event is sent by the particle cloud?

I have the particle web console open to the events view for my device, and then I cut power to my device. 25 minutes later, I see no events. If I power the device back on, I do see “spark/status online” among other events, but detecting that a device is online is easy :slight_smile:, I need to know when it’s offline.

Thanks!
Theo

After some patience, it looks like the offline event comes 45 minutes after the device is powered down. Any chance this can be shortened on a per device or per product basis?

The issue is that the cloud cannot detect whether the device is offline.
Even when the device is online there is no connection to be checked and hence no way to know whether the device is available without trying to communicate.
That’s the nature of UDP connections in general and over cellular in particular.
The cloud only assumes a device offline when it hasn’t heard of it for two keepAlive cycles (23 minutes each).

The cloud will never ping the device without anybody requesting a datatransfer as it would incure data traffic most people don’t want.
If you want it, you need to incorporate a regular ping.

For my education, can you explain where the 23 minutes comes from? Is that defined by the mobile network or by Particle? (or is it in our DNA… :))

This is the negotiated maximum keep alive between Particle and its MVNOs for the Particle SIMs.