How do I properly detect if my Particle Photon is offline?

How do I properly detect if my Particle Photon is offline?

I currently setup a function that gets called every 15 minutes that pings the Particle Photon.
It works fine when the Photon is online but fails when it is offline.

private getStatus() {
    def params = [
        uri: "https://api.particle.io/v1/devices/${deviceId}/status?access_token=${token}"
    ]
    
    asynchttp_v1.get(processData, params)
    log.info("getStatus")
}

def processData(response, data) {
    log.info("processData")
    
    if (response.data.hasError()) {
    	log.info("Test.")
    }

    // More code.

}

The Particle Photon outputs:

{"error":"Timed out."}

When offline and SmartThings outputs:

error java.lang.IllegalArgumentException: Failed to complete. Message was: Read timeout to api.particle.io/34.226.167.93:443 of 20000 ms @line 93 (processData)

I have tried reading Particle Photon’s response.

private getStatus() {
    def params = [
        uri: "https://api.particle.io/v1/devices/${deviceId}/status?access_token=${token}"
    ]
    
    asynchttp_v1.get(processData, params)
    log.info("getStatus")
}

def processData(response, data) {
    log.info("processData")
    
    def results = new groovy.json.JsonSlurper().parseText(response.data)
    
    if (results.error) {
    	log.info("Test.")
    }

    // More code.

}

Which results in a similar error in the SmartThings Logs.

There seems to be a longer timeout on the Particle side than the 20 seconds that asynchttp_v1 is willing to wait, so that it hangs up before the api.particle.io has given up on receiving a reply from your Photon. Can’t help you with a fix though, haven’t found a timeout parameter for the http call. But fairly fresh off the boat when it comes to SmartThings, so perhaps someone else knows of a way to do an external http call with longer timeout. Presumably there are no config options in the Particle api to make it give up sooner either?

OK

Try to implement your code in a try/catch code block. If you get a timeout, then update the status of your particle photon accordingly. At least the code will not error out.

I figured it out.
I was using the wrong api call for that purpose.

It should have been:
https://api.particle.io/v1/devices?access_token=${token}

I would have deleted this earlier.
How do I remove topics?

No need to delete the topic. It may be useful to other users in the future.

Glad to hear you got it working.