Particle.connected() returns True after calling Particle.disconnect()

I am trying to diagnose some general permanent disconnection states of remote Electrons. I’ve been trying to reproduce my issues, but in doing so I stumbled upon this issue. If (in SYSTEM_MODE(AUTOMATIC)) I call Particle.disconnect(), the device permanently disconnects from the Particle cloud despite an attempt to call Particle.connect again.

Firmware: 0.6.4
3rd Party SIM Card

#include <MQTT.h>

STARTUP(cellular_credentials_set("hologram", "", "", NULL));

//MQTT Config

byte server[] = {REDACTED};
MQTT client(server, REDACTED, callback);

String truncated_deviceID = Particle.deviceID().substring(4);   // get a 20char version of the device ID to match the length constraint for the MQTT ID for compatibility
const char* MQTT_ID = truncated_deviceID;   // c: chicago

#define MQTT_TOPIC_BASE      REDACTED
const char* MQTT_TOPIC_error_data =  MQTT_TOPIC_BASE "_error_data";

// Dummy handler to receive callback message from MQTT
void callback(char* topic, byte* payload, unsigned int length){};

bool keepAlive_set = false;
const uint32_t MQTT_msg_interval = 5000;  //ms
uint32_t MQTT_time_last_sent;
uint32_t MQTT_msg_counter;

void setup() {
    MQTT_time_last_sent = 0;
    MQTT_msg_counter = 0;
}


void loop() {
    if (!keepAlive_set && Particle.connected())
    {
        Particle.keepAlive(30);
        keepAlive_set = true;

    }

    if (!Particle.connected())
    {
        Particle.connect();
    }
    else
    {
        Particle.process();
    }

    if (!client.isConnected())
    {
        client.connect(MQTT_ID);
    }
    else
    {
        client.loop();
        if ((millis() - MQTT_time_last_sent) > MQTT_msg_interval)
        {
            client.publish(MQTT_TOPIC_error_data, "Test Heartbeat #" + String(MQTT_msg_counter) + ";  Particle.connected()=" + String(Particle.connected()));
            MQTT_msg_counter++;
            MQTT_time_last_sent = millis();
            if (MQTT_msg_counter == 10)
            {
                Particle.disconnect();
            }
        }
    }
}

As indicated by the logs below, Particle.connected() continues to return true. I understand that it takes a few calls to Particle.process() to update, but after 30 minutes (just in case the keepAlive wasn’t set properly) there was no change in state.

REDACTED : Test Heartbeat #0;  Particle.connected()=1
REDACTED : Test Heartbeat #1;  Particle.connected()=1
REDACTED : Test Heartbeat #2;  Particle.connected()=1
REDACTED : Test Heartbeat #3;  Particle.connected()=1
REDACTED : Test Heartbeat #4;  Particle.connected()=1
REDACTED : Test Heartbeat #5;  Particle.connected()=1
REDACTED : Test Heartbeat #6;  Particle.connected()=1
REDACTED : Test Heartbeat #7;  Particle.connected()=1
REDACTED : Test Heartbeat #8;  Particle.connected()=1
REDACTED : Test Heartbeat #9;  Particle.connected()=1
REDACTED : Test Heartbeat #10;  Particle.connected()=1
REDACTED : Test Heartbeat #11;  Particle.connected()=1
REDACTED : Test Heartbeat #12;  Particle.connected()=1
REDACTED : Test Heartbeat #13;  Particle.connected()=1
REDACTED : Test Heartbeat #14;  Particle.connected()=1
REDACTED : Test Heartbeat #15;  Particle.connected()=1
REDACTED : Test Heartbeat #16;  Particle.connected()=1
REDACTED : Test Heartbeat #17;  Particle.connected()=1
REDACTED : Test Heartbeat #18;  Particle.connected()=1
REDACTED : Test Heartbeat #19;  Particle.connected()=1
REDACTED : Test Heartbeat #20;  Particle.connected()=1

In general, I’ve been seeing some issues with my devices where my remote devices in spottier service permanently disconnect from the Particle cloud, and then eventually (could be minutes or days later) stop sending data altogether. I’m trying to simulate various states of disconnection to try and reproduce my problems and this seems to be step one in figuring out what my actual root cause is.

The docs state:

In automatic mode, the user can still call Particle.disconnect() to disconnect from the Cloud, but is then responsible for re-connecting to the Cloud by calling Particle.connect().

The docs also say that Particle.connected() should:

Returns true when connected to the Cloud, and false when disconnected from the Cloud.

This does not seem consistent with the behavior I’m seeing. Am I missing something fundamental here, or is Particle.connected() not returning it’s expected value?

As a case study, when Particle had the outage a few weeks ago, 3 of my Electrons were permanently disconnected from the Particle cloud and required power resets. They all streamed data via MQTT for a while during that event before eventually completely disconnecting from the cellular network completely. I have since had the same thing happen several times for unknown reasons, but it was always the cloud disconnecting first, then completely losing contact.

If I could trust the Particle cloud connection to never get closed from an external factor none of this would matter, but I’ve seen some real world situations where that wasn’t true and the device stays in the state from my original post.

Is my only solution to use MANUAL mode to avoid my devices going into this state? Why doesn’t the System Firmware detect when the cloud connection is lost and reconnect?

Thankfully the solution to this one was easy - the device was correctly reconnecting as normal. I was just checking after the reconnection so I never caught it. My original problem still exists, but I’ll continue diagnosing and try and make a separate post once I can be more specific. Whoops!