I ran a few tests. The Spark Core restarts with red blinking lights when calling Spark.publish()
before the first successful connection to the cloud. It is OK to call Spark.publish()
after the connection drops as long as it was connected once before.
I think the firmware should protect the user against this corner case by checking if the initial connection was done before trying to publish.
Here are the details:
Code variant 1: Call publish before cloud connection is complete
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Program starting");
Spark.connect();
Serial.println("Cloud connect done");
}
void loop() {
Serial.println("connected=" + String(Spark.connected()));
Serial.println("Publishing");
Spark.publish("event");
Serial.println("Publish done");
delay(1000);
}
Output variant 1
2015-06-03 15:29:55 -0400 Program starting
2015-06-03 15:29:57 -0400 Cloud connect done
2015-06-03 15:29:57 -0400 connected=0
2015-06-03 15:29:57 -0400 Publishing
2015-06-03 15:30:10 -0400 Program starting
2015-06-03 15:30:12 -0400 Cloud connect done
2015-06-03 15:30:12 -0400 connected=0
2015-06-03 15:30:12 -0400 Publishing
2015-06-03 15:30:25 -0400 Program starting
Results variant 1
The program restarts when calling Spark.publish()
since the connection was initiated but the cloud handshake wasn’t finished. You get the same results if you don’t call Spark.connect()
at all.
Code variant 2: Don’t call publish when there is no cloud connection
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Program starting");
Spark.connect();
Serial.println("Cloud connect done");
}
void loop() {
Serial.println("connected=" + String(Spark.connected()));
if(Spark.connected()) {
Serial.println("Publishing");
Spark.publish("event");
Serial.println("Publish done");
} else {
Serial.println("Not publishing");
}
delay(1000);
}
Output variant 2
2015-06-03 15:41:02 -0400 Program starting
2015-06-03 15:41:04 -0400 Cloud connect done
2015-06-03 15:41:04 -0400 connected=0
2015-06-03 15:41:04 -0400 Not publishing
2015-06-03 15:41:06 -0400 connected=1
2015-06-03 15:41:06 -0400 Publishing
2015-06-03 15:41:06 -0400 Publish done
2015-06-03 15:41:07 -0400 connected=1
2015-06-03 15:41:07 -0400 Publishing
2015-06-03 15:41:07 -0400 Publish done
2015-06-03 15:41:08 -0400 connected=1
2015-06-03 15:41:08 -0400 Publishing
2015-06-03 15:41:08 -0400 Publish done
2015-06-03 15:41:09 -0400 connected=1
2015-06-03 15:41:09 -0400 Publishing
2015-06-03 15:41:09 -0400 Publish done
2015-06-03 15:41:10 -0400 connected=1
2015-06-03 15:41:10 -0400 Publishing
2015-06-03 15:41:10 -0400 Publish done
Results variant 2
The defensive programming approach suggested by @ScruffR to check for Spark.connected()
avoids issues here.
Code variant 3: Wait for initial connection then always publish
SYSTEM_MODE(SEMI_AUTOMATIC);
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Program starting");
Spark.connect();
Serial.println("Cloud connect done");
while(!Spark.connected()) {
delay(1000);
}
}
void loop() {
Serial.println("connected=" + String(Spark.connected()));
Serial.println("Publishing");
Spark.publish("event");
Serial.println("Publish done");
delay(1000);
}
Output variant 3
2015-06-03 15:53:44 -0400 Program starting
2015-06-03 15:53:46 -0400 Cloud connect done
2015-06-03 15:53:48 -0400 connected=1
2015-06-03 15:53:48 -0400 Publishing
2015-06-03 15:53:48 -0400 Publish done
2015-06-03 15:53:49 -0400 connected=1
2015-06-03 15:53:49 -0400 Publishing
2015-06-03 15:53:49 -0400 Publish done
...
2015-06-03 15:53:57 -0400 connected=1
2015-06-03 15:53:57 -0400 Publishing
2015-06-03 15:53:57 -0400 Publish done
***************************************WiFi turned off here
2015-06-03 15:53:58 -0400 connected=0
2015-06-03 15:53:58 -0400 Publishing
2015-06-03 15:53:58 -0400 Publish done
2015-06-03 15:53:59 -0400 connected=0
2015-06-03 15:53:59 -0400 Publishing
2015-06-03 15:53:59 -0400 Publish done
...
2015-06-03 15:54:52 -0400 connected=0
2015-06-03 15:54:52 -0400 Publishing
2015-06-03 15:54:52 -0400 Publish done
2015-06-03 15:54:54 -0400 connected=0
2015-06-03 15:54:54 -0400 Publishing
2015-06-03 15:54:54 -0400 Publish done
***************************************WiFi turned on here
2015-06-03 15:54:56 -0400 connected=1
2015-06-03 15:54:56 -0400 Publishing
2015-06-03 15:54:56 -0400 Publish done
2015-06-03 15:54:57 -0400 connected=1
2015-06-03 15:54:57 -0400 Publishing
2015-06-03 15:54:57 -0400 Publish done
Results variant 3
Even though the WiFi connection drops, the program doesn’t restart.