Frequent Disconnects

What is the best way to troubleshoot frequent disconnects from Losant via a Particle Photon? It seems when I lose connection with Losant the process of attempting to reconnect hangs too long and the Particle drops is WiFi connection. It is only after the Particle reconnects that Losant can connect and continue on.

What technique are you using to connect the Photon to Losant?

Hi Brandon,

This is the function I use to connect:

void connect() {
    Serial.print("Connecting to Losant...");
 
    while(!client.isConnected()) {
       client.connect(
           LOSANT_DEVICE_ID,
           LOSANT_ACCESS_KEY,
           LOSANT_ACCESS_SECRET);
 
       if(client.isConnected()) {
           Serial.println("connected!");
           client.subscribe(MQTT_TOPIC_COMMAND);
       }
       else {
           Serial.print(".");
           delay(500);
       }
    }
 }

This is where I call the function from the loop:

// Publish state to Losant every XX.
    if(now - lastUpdate > 5000) {
        lastUpdate = now;
 
        StaticJsonBuffer<200> jsonBuffer;
        JsonObject& root = jsonBuffer.createObject();
        JsonObject& state = jsonBuffer.createObject();
 
        state["Flow"] = flowRate;
        state["Pressure"] = pressure;
        root["data"] = state;
 
        // Get JSON string.
        char buffer[200];
        root.printTo(buffer, sizeof(buffer));
 
        client.publish(MQTT_TOPIC_STATE, buffer);
    }

This is a sample from my Device Connection Log:

Date	Sent	Rec	Details
09/06/2016 9:56:43 PM			
09/06/2016 9:55:52 PM	5	0	Connection Lost
09/06/2016 9:55:06 PM			
09/06/2016 9:54:17 PM	9	0	Connection Lost
09/06/2016 9:53:02 PM			
09/06/2016 9:53:00 PM	6	0	Connection Lost
09/06/2016 9:52:06 PM			
09/06/2016 9:51:13 PM	14	0	Connection Lost
09/06/2016 9:49:31 PM

I assume you followed our blog article on connecting Particle to Losant. I apologize for the run-around, but since writing that we now recommend connecting the Photon to the Particle Cloud and using a webhook to send data to a virtual device in Losant. The primary reason is that the Photon cannot do TLS encryption, which means data between the device and Losant cannot be encrypted. The Photon does encrypt the data when being sent to Particle Cloud, which is why we recommend using it.

The Particle MQTT client and Json libraries are both not actively being maintained. There are known memory leaks in the Json library that have been fixed but not yet ported to Particle.

You can follow our detailed Particle workshop to see how to properly set up the webhook integration between the two clouds. Once the data is in a virtual device, it can be used within Losant in the exact same way you would a standalone device.

I apologize again and it’s very high priority for us to deprecate those instructions and replace them with our current recommendation.

Ok, thanks Brandon I will take a look!

I also think I found the core of my issue. It seems since I am also publishing some data to the Particle Cloud, I am hitting up against their rate limiting. This is causing the Photon to lose its Cloud connection and subsequently resetting its WifI connection. Reducing the rate at which I am sending data seems to have resolved the reliability issue.

Bob

Brandon,
Is the Particle Cloud/Webhook method still the most efficient to get data from the Particle chip to Losant? We are now running up against the webhook execution limits within Particle Cloud. I can certainly work with them to pay for the next tier level to potentially relieve that limit but I wanted to make sure there was not a better way I should be considering and bypassing the Particle Cloud traversal.
Bob

There still does not appear to be any TLS capability on the Particle boards, which makes it hard to directly connect them securely to external services. The underlying encryption mechanisms that Particle does use are not exposed at the user-level (your firmware), so we can’t easily piggyback on that functionality either.

If you’re using the Electron over cellular, there’s no good options yet. If you’re using the Photon over WiFi, you could setup a gateway device to bypass the Particle cloud and send encrypted data directly to Losant.