I'm having issues completely eradicating blocking in my specific application. There are some instances where my board loses connection to the cloud and then tries to reconnect. When attempt to reconnect to the cloud, I am witnessing my loop() code block for up to 20 seconds. For my application, I can not have my loop code blocked at all.
I have done some research and have found that I should avoid Particle.publish() if I am not connected to the cloud. I have avoided that but the blocking persists (although, before I removed attempting to publish when unconnected to the cloud, I saw my blocking time at 100+ seconds)
I am using
SYSTEM_THREAD(ENABLED); SYSTEM_MODE(SEMI_AUTOMATIC);
I do not want to include all my code because it's a large file but will include some snippets.
I am calling this function in my setup():
void initParticle(){
// Synchronize time with Particle cloud
Particle.syncTime();
//Particle function declarations that can be access in Particle Console
Particle.function("startSleep", startSleep);
Particle.function("reboot", rebootBoard);
Particle.function("hourPressed", hourPressed);
Particle.function("minutePressed", minutePressed);
Particle.function("secondPressed", secondPressed);
//Connect to allow webhook integration response
Particle.subscribe("hook-response/button_pushed", myHandler, MY_DEVICES);
Particle.subscribe("hook-response/online_check", myHandler, MY_DEVICES);
// Connect to the Particle cloud
Particle.connect();
delay(1000);
}
My code where I am checking if I am connected and publishing if so, and trying to connect if not:
if (Particle.connected()){
bool success;
success = Particle.publish("online_check", data);
if (!success){
//If we fail here, we know signalflag should be set high
Serial.println("Failure to send");
signalFlag = true;
}
else {
Serial.println("Message sent successfully!");
//Set an alarm to timeout response
pingResponseAlarm.setAlarm(timeoutTime);
}
}
else {
Serial.println("We are not connected to the Particle cloud! The Particle board should reconnect automatically..");
signalFlag = true;
Particle.connect();
}
If this message times out (10 seconds), this function gets called which disconnects me from the cloud:
void pingResponseTimeout(){
Serial.println("Here in pingResponseTimeout");
signalFlag = true;
Particle.disconnect();
loadingDisplayIterationAlarm.unSetAlarm();
}
I am witnessing this blocking take place only when attempting to connect to the cloud. Is there any way I can avoid blocking or some type of workaround?