While I haven’t found an explicit mention of it in the docs, I presume that any blocking code, such as HTTP requests, should not be made inside a Particle function callback (to prevent timeout). However, I’ve been struggling to find an alternative.
If I put my HTTP request code in another function, and execute it from my Particle function callback, it will wait for my function to return before returning callback – effectively doing the same as leaving blocking code in the callback function.
What’s the best way to execute a function asynchronously?
I’m aware of async, however would rather not include it as the future
dependancy would add several Kb.
@BenedictLewis, you will need to decouple the callback from the HTTP request. Perhaps set a flag in the callback (which can then return immediately) which gets sampled in loop(). The actual HTTP request is then performed in loop() and when completed, sets another flag to indicate completion (if required). Given the asynchronous nature of the Particle.function() call, the caller may need to monitor a Particle.variable() to establish the status of the function call for example (or subscribe to an event is another method).
With SYSTEM_THREAD(ENABLED), I believe TCPClient will run in the system thread and the only blocking will be from waiting for the server response which doesn’t need to block per se.