Wondering, beyond some very helpful tutorials below, what all can block the Particle cloud? For example I’ve seen an app work completely fine for days on identical hardware on same OS (even with less signal) in the lab, and the SAME app be completely unresponsive in the wild (even with better signal) right from the start.
There is no exhaustive list of all calls that can block, but you should assume that any call that requires access to the cellular modem (or Wi-Fi) will block.
In your code above, Particle.connected() does not block, because it just returns a flag and does not access the modem.
As written, Particle.publish() will generally only block for up to 20 seconds because of the connected check before it. However if you don’t check for connected first, it can block for up to 10 minutes. There might be some other edge cases where really long blocking can occur.
Also, even with the connected check, the device may not realize that it has lost its connection until it tries to send, and blocking can still occur.
The two most common cases for blocking are Particle.publish() and Cellular.RSSI(). It’s because both need to access the cellular modem, which might itself be blocked trying to reconnect to cellular. The best way to avoid this unpredictable blocking is to execute these calls from a separate thread. Sometimes two threads, or sometimes you can do both on one thread. If you use other calls that access the modem like Cellular.command()those would also potentially block, as well as things that will eventually access the modem like TCP and UDP.
Creation of the TCPClient object is not blocking, but connect() is definitely blocking. If you specify the host by name its doubly blocking because of both DNS and the TCP connection. The calls that result in a read or write can also block.
The modem is blocked issue in kind of annoying for TCP. The reason is that if you are trying to publish, even from another thread, and the cloud connection is lost and is trying to reconnect, the modem could get blocked. This will prevent TCP from working, even though it’s theoretically independent from the cloud connection. This is because the modem mutex gets blocked.
They’re only blocking if the system is attempting to use the PMIC at the same time. There’s a PMIC mutex, but unlike the modem mutex, it should only be blocked for very short periods of time.