I got rid of most calls to delay(). There's only one left at the bottom of loop(), but that's just 50 ms.
// Allow Oak to handle WiFi stuff in the loop()
// while we're not busy reading data.
if (! Voltcraft.readmore() ) {
delay(PUBLISH_DELAY_MILLIS);
}
yield() did not work here. I did not need to sprinkle any other calls to yield().
Your suggestion to remove the delay(5000) from the reconnect() function solved my OTA problem (*). I replaced it with a state, which is also more in line with the entire sketch:
/* Reconnect to MQTT */
case 100:
if ( abs(millis() - previousReconnectAttempt) > DELAY_BETWEEN_CONNECTS_MILLIS) { // non-blocking delay, we can still do stuff in loop()
if (client.connect(connection_id, client_name, client_password)) {
previousReconnectAttempt = millis();
client.publish(statusTopic, "Oak has (re)connected");
nextStatus = prevStatus; // go back and attempt to publish our payload again
}
else { // connect failed
nextStatus = 0; // give up, get new data, try again later
}
}
break;
Works nicely, and is no longer blocking to loop(). Thanks for the suggestion!
(*) I am powering the Oak of the Voltcraft device and I think that it cannot provide enough power for the reflash: it fails all the time. OTA reflashing from USB power supply is fine.