Unexpected behavior for Particle.connect() timeout and System.sleep()

I’m having some weird issues with Particle.connect() timeouts, sleep, and subsequent publish. On rev. 0.7.0

The photon starts out without connecting because in the actual application there would be some code running that would decide whether or not to connect to the cloud.

The following code is designed to:

  1. timeout the Particle.connect()
  2. sleep for “sleepIntervalTimeOut”
  3. resume from where execution left off
  4. take a measurement of WiFi RSSI, and publish it
  5. wait 8 seconds to let publish do its thing
  6. disconnect from cloud, then shut off wifi
  7. sleep normal interval, sleepIntervalNormal

What happens is that upon powerup,

  1. timeout, white led flash and sleep
  2. on wakeup, code continues and automatically connects to cloud and publishes, however it publishes “0” instead of the expected -127 to -1 reading
  3. goes back to sleep
  4. wakes up and repeats cycle-- however now all subsequent cloud connections do not publish anything!

Thanks in advance.

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

String sleep_test;

const int MAX_TIME_TO_CONNECT_MS = 50; //wait time for wiFi/cloud connect (millis)
const int sleepIntervalTimeOut = 15; //sleep time if cloud connect timed out (s)
const int sleepIntervalNormal = 10; //normal time between measurements (s)

int wifiStrength;

void setup() {
    pinMode(D3, INPUT);
}

void loop() {
    publishData();
    System.sleep(D3,RISING,sleepIntervalNormal);
}

void publishData() {
    Particle.connect(); 

    if (!waitFor(Particle.connected, MAX_TIME_TO_CONNECT_MS)) {
        System.sleep(D3,RISING,sleepIntervalTimeOut);
    }

    wifiStrength = WiFi.RSSI();
    sleep_test = String(wifiStrength);
    Particle.publish("sleep_test", sleep_test, PRIVATE);
    delay(8000);
    
    Particle.disconnect();
    WiFi.off();
}

One thing, I’d expect to happen is that when you wake from sleepIntervalTimeOut you are experiencing a race condition where the WiFi module isn’t actually fully operational when you try to read RSSI and publish the event.
You could try adding the SLEEP_NETWORK_STANDBY flag to the sleep call, and if this doesn’t help add another waitFor(Particle.connected, x) after the sleep call.

Your 50ms allowance for the connect to succeed is also rather short IMO - I’d rather expect it to take a few seconds.
You are allowing 8 seconds for the event to be published but only 50 milliseconds for the WiFi modem to be switched on, the WiFi connection to be established and the Cloud handshake to take place, that’s very unlikely to be enough.

the race condition makes sense, do you know if calling WiFi.RSSI and/or Particle.publish without cloud connection just times out, throws an internal error and continues, or what? Also why do you think the first publish after timeout makes it through but none of the subsequent tries do?

I haven’t got a conclusive explanation for that, but the obvious difference is that you only run through the initialisation and setup() once which may cause a slighlty different timing and also the starting condition of the WiFi module might be slightly different.

But with the needed delays things should work correctly.

Thanks ScruffR, that makes sense. Adding the delays does fix it.