How do I query one Photon's status from another Photon

I’m looking for a way to query one Photon’s connection status to the Cloud from another Photon. I have an application where the two Photons are interdependent and will indicate with an LED that the opposite Photon is connected to the Cloud.

I found the Particle.connected() cloud function that returns the local device’s status but nothing for a remote device.

Is there a simple way to do this?

Thanks, Glenn

Do a ‘ping’ with publish/subscribe?

1 Like

So, have each Photon periodically publish an event with the other subscribed and if there is a time-out raise an exception?

I was hoping for something like:

Particle.connected(Device ID)

but the ping will work.

Thanks.

Have a Photon send a “ping” publish, and have the receiving end (the one you want to check the status of) respond with a “pong”. If it doesn’t reply, you know there’s something off.

I implemented a ping - response for the two Photons. The Studio Photon sends a Ping via Particle.publish() and the Driver Photon is subscribed, waiting on the ping. Once the Driver Photon sees the ping it responds with “pong” via another Particle.publish() that the Studio unit is subscribed to. Upon successful receipt of the “pong” the Studio unit turns on a green LED indicating connectivity. This is probably the simplest way to test connectivity between the two units as the ping - response won’t happen if there is anything wrong in-between.

The code for the two devices is below:

*** Studio ***

// Studio

int greenLed = D4; // Indicates connectivity to the Driver unit
int pingInterval = 14; // Chnage this value to set the time between pings in seconds + 1

bool pingReply = FALSE;  // Indicates a successful ping response

unsigned long lastPing; // Placeholder for the lat ping time

void setup() {
    
    Particle.subscribe("Driver", PingHandler, "2c######################");  // Subscribe to the Driver Photon

    // Setup the Digital pins
    pinMode(greenLed, OUTPUT);
    
}

void loop() {
    
    if ((Time.now() - lastPing) > pingInterval) {  // Ping Driver to test connectivity
        if (!pingReply) {              // Turn off LED if there is no ping reply from Driver
            digitalWrite(greenLed, 0);
        }
        pingReply = FALSE;  // Set ping reply to FALSE before pinging Driver
        Particle.publish("Studio", "ping");  // Publish ping to get Driver to respond
        
    lastPing = Time.now();
    }
}

// This function is called when the Particle.subscribe("Driver"... gets pong
void PingHandler(const char *event, const char *data) {
    if(String(data) == "pong") {
        digitalWrite(greenLed, 1);  // Set the green LED on if Driver responds
        pingReply = TRUE;  // Set to TRUE on ping response to keep LED on between pings
    }
}
// Driver

void setup() {

Particle.subscribe("Studio", CloudHandler, "37######################");  // Subscribe to the Studio Photon

}

void loop() {

    // Don't need to do anything here.

}

void CloudHandler(const char *event, const char *data) {
    if (String(data) == "ping") {  // Respond to the ping from Studio
        Particle.publish("Driver", "pong");
    }
        
}

I hope someone finds this helpful. If you have any suggestions or if this needs an edit just let me know.

Thanks to Moors7 and Dave for the great support.

Glenn

4 Likes

General suggestion for Particle.publish(): Keep the event PRIVATE and subscribe to MY_DEVICES.
I think the subscriby by DeviceID was never implemented.

And use if (strcmp(data, "ping") == 0) instead of a String() conversion.

1 Like