Are there restrictions as to what should be called within a Cloud Function?

I am experiencing a problem that events published from within a Cloud Function do not work reliably.
I am wondering if this is another multi-threading issue.

The documentation for Cloud Functions talks about calling some other functions…

// this function automagically gets called upon a matching POST request
int brewCoffee(String command)
{
  // look for the matching argument "coffee" <-- max of 64 characters long
  if(command == "coffee")
  {
    // some example functions you might have
    //activateWaterHeater();
    //activateWaterPump();
    return 1;
  }
  else return -1;
}

I have at the start of my code

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

Are there specific restrictions on what can be called? Can I assume that calls to shared resources should be avoided so that there are no clashes possible with what is going on in the Application thread? So in the documentation example given ActivateWaterPump(); should not be called in the function if it is also called from the main application loop.

Lastly, should Particle.publish() be avoided from within a cloud function and any other calls such as System.reset();?

AFAIK Particle.function()s are called from the application thread (either between iterations of loop() or when calling Particle.process()), so they shouldn’t interfere with other calls on the same thread.
But you should keep the functions short otherwise the cloud call will see a timeout and report an error.

1 Like