How can I tell if particle is connected

I am turning of the Cellular modem on my electron to save power and also the unit will be in marginal reception areas so signal will cut out. I am storing readings in the eeeprom and then uploading them when I get a signal.

Problem is that Particle.connected() is returning true even when it is not connected. So my unit is publishing but the readings are not getting to the server.

How can I ensure that I have a connection to the cloud before I publish.

Any help is appreciated.

void sendReadings(){

    if(Particle.connected()){
        Particle.process();

        mem_get();

        
        while(reading.temp < 119 && Particle.connected()){
 
            char lff[26];

            sprintf(lff, "%d,%d,%d,%d", reading.temp,  reading.humidity, pointer.readTime, (int)fuel.getSoC());

            if(Particle.publish("Telemetry", lff)){
                move_read_pointer();
                print_to_serial("Published OK ");
                print_to_serial((String)lff);
                delay(1000);

                mem_get();
        
            } else {
                print_to_serial("Could not publish");
                break;
            }
        }
        
        if(!Particle.connected()){
            print_to_serial("Connection Lost during upload.");
        }
 
    } else {
        print_to_serial("Could not connect");
    }
}
1 Like

One thing you need to consider is, that in default single threaded automatic mode your code will stop running when the connection is lost and in your location you won’t only lose connection to the Particle cloud but frequently also to the cell tower.

So I’d do something like this

SYSTEM_THREAD(ENABLED)

void setup() {
  pinMode(D7, OUTPUT);
}

void loop() {
  if (Cellular.ready() && Particle.connected())
    digitalWrite(D7, HIGH);
  else
    digitalWrite(D7, LOW);
}
1 Like