[SOLVED] Disconnecting during setup

I have a calibration sequence in the setup section of my code. If I have the calibration loop run for longer than 10 or 20 seconds, it disconnects from the cloud temporarily, then reconnects. It’s not really a problem, but not exactly desireable. I’m wondering what exactly causes this and what is the cleanest way to correct it is?

int calibrationTime = 60000;

void setup() {
    pinMode(proximityPin1, INPUT);
    pinMode(proximityPin2, INPUT);
    pinMode(ledPin, OUTPUT);
    Threashold1 = calibrate(proximityPin1);
    Threashold2 = calibrate(proximityPin2);
int calibrate(int pinToCalibrate) {
    calibrationValue = analogRead(pinToCalibrate);
    Particle.publish("Starting Calibration", String(calibrationValue));
    timerStart = millis();
    while (millis() < (timerStart + calibrationTime)) {
        if ((millis() % 1000) > 500) {
            digitalWrite(ledPin, HIGH);
        else digitalWrite(ledPin, LOW);
        calibrationCheck = analogRead(pinToCalibrate);
        if (calibrationCheck > calibrationValue) {
            calibrationValue = calibrationCheck;
    digitalWrite(ledPin, LOW);
    Particle.publish("Calibration Complete", String(calibrationValue));
    return (calibrationValue);

Any code that keeps the flow from dropping out of loop() for longer than 10sec prevents the system from doing the cloud housekeeping.

In that case you have to trigger that cloud keeping, either by frequently calling Particle.process() or by use SYSTEM_THREAD(ENABLED) which donates a seperate thread just for the cloudkeeping and other system stuff.

You might also notice if you do something as delay(60000) that the cloud doesn’t get dropped, but that is because delay() implicitly calls Particle.process() once every second.

1 Like

Thank you!

1 Like