Is waitFor(Particle.connected()) needed?

On the Electron, I’m in SEMI_AUTOMATIC mode with a sketch that looks something like this:

Particle.connect();
waitFor(Particle.connected, 30000);
char data[50];
snprintf(data, sizeof(data), "{\"temp\": %.1f, \"humidity\": %.1f}", temp, hum);
Particle.publish("weather", data, PRIVATE);
Particle.disconnect();

Can I safely omit the waitFor?
Thanks in advance

You can, but for Electrons 30000ms would be too short anyway since a cold-connect to the cell network is allowed to take up to 10 times that long.

However, you should check for Particle.connected() before attempting to Particle.publish()

If I omit waitFor(Particle.connected, 30000) will the device just keep attempting to connect—i.e. is there any point to using waitFor(Particle.connected())?

If you use it in the intended way - like


if (waitFor(Particle.connected, 300000)) {
  // do whatever intended when we got a connection
}
else {
  // do something when the connection couldn't be made
  // e.g. storing data for a later time or re-arm for a new connection attempt
}

then, sure there is a point in using it :wink:

1 Like

If I didn’t have the waitFor(Particle.connected()) how long will it try for a connection?

If you don’t cancel the connection attempt after a while probably forever.

But due to this issue I’m not entirely sure under what circumstances Particle.connect() would “hard block” vs. just yield - however I’d definetly go with SYSTEM_THREAD(ENABLED) and with that SYSTEM_MODE(MANUAL) should be the most consistent mode.

Appreciate your help

1 Like

I’m going to revive this as today I found an instance where waitFor was not working as expected.

This was in setup block after connect etc etc, in the same spot it’s been for over ?2-3? years:

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
....

void setup() {

    delay (3000);
    // Start serial at 9600 baud
    Serial.begin(115200);
    Serial1.begin(115200); // BT module


    BLEsetupBlock();
    Serial.println("completed BLE setup");

    delay(5000);

    // Update version as property functions can't be in global scope
    sV.concat("_");
    sV.concat(System.version());
    sV.toCharArray(strVersion, 32);

    // Spark read/write variables
      Particle.variable("version", strVersion, STRING);

      Particle.variable("temps", publishString, STRING);
....
      Particle.function("reset", callReset);

      Serial.println("start of setup 0 functions");

    WiFi.on();

    delay(300);

    WiFiAccessPoint ap[5];
    int found = WiFi.getCredentials(ap, 5);
    for (int i = 0; i < found; i++) {
        Serial.print("ssid: ");
        Serial.println(ap[i].ssid);
        // security is one of WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA, WLAN_SEC_WPA2, WLAN_SEC_WPA_ENTERPRISE, WLAN_SEC_WPA2_ENTERPRISE
        Serial.print("security: ");
        Serial.println(ap[i].security);
        // cipher is one of WLAN_CIPHER_AES, WLAN_CIPHER_TKIP or WLAN_CIPHER_AES_TKIP
        Serial.print("cipher: ");
        Serial.println(ap[i].cipher);
    }

    Serial.println("start of setup 1 connect");

    WiFi.connect();

    delay(300);

    //delay(2000);

    //Particle.connect();
    Serial.println("start of setup 1 particle connect");

    Particle.connect();
    //WiFi.macAddress(mac); // Save the particle mac address

    Serial.println("start of setup 1 wifi");

if (waitFor(Particle.connected, 15000)) {
                  Serial.println("start of setup5.2 waitfor ");
                  bool Subscribed = Particle.subscribe(String("hook-response/gettargetsxxx_") + System.deviceID(), cbGetTargets, MY_DEVICES);
                  Serial.println("start of setup5.3 waitfor 2");
                  bool Subscribed2 = Particle.subscribe(String("hook-response/getregoxxxv1_" + System.deviceID()), cbGetRego, MY_DEVICES);
                  // Retrieve the actual targets
                  Particle.publish("gettargetsv4", "inside waitfor", 30, PRIVATE);
}
}

Did not work.

Placed it in the loop and let it execute when a particle_connected flag had been set for the first time and it started working again.

I’ve been asking for a while for a .ino example of semi-automatic, system thread test case that shows wifi connect, particle connect, cloud variables and functions and now it seems we also need to keep on our toes about particle.subscribe.

Can someone provide what Particle are running as their test case? It must exist somewhere…