Bummer! failed to call

I get an error when I try and call a function from the console.particle.io that says Bummer! failed to call aerator. The function is called aerator and has some code with delays and switching pins on and off etc for automating a pool valve. The relays click on and off as they are supposed to but it sends the failed message. If I remove the bulk of the code inside the function and just have the return part it succesfully calls and returns a one or a zero. Is there a way to keep this from I’m assuming timeing out? Perhaps by sending the response and then continuing the code? Cause in its current state its screwing up the status of the switch in my smartthings home automation since it thinks that it failed.

Spark.function("aerator", aeratorfunc);

}

//
// runs based upon cloud input
//

int aeratorfunc(String command)
{
// clockwise valve aerator on relay  
  if (command == "1") 
    {   
    digitalWrite(aeratoroffrelay, HIGH); // first make sure the reverse direction re;ay is set to off
    delay(2000);                         // wait for motor to spool down before in case its reversing direction
    digitalWrite(aeratoronrelay, LOW);   // activate the relay
    delay(20000);                        // wait amount of time then turn the relay off
    digitalWrite(aeratoronrelay, HIGH);
    return 1;
    }
// counterclosckwise valve aerator off relay 
  else 
    {               
    digitalWrite(aeratoronrelay, HIGH);  // first make sure the reverse direction re;ay is set to off
    delay(2000);                         // wait for motor to spool down before in case its reversing direction
    digitalWrite(aeratoroffrelay, LOW);  // activate the relay
    delay(20000);                        // wait amount of time then turn the relay off
    digitalWrite(aeratoroffrelay, HIGH);
    return 0;
    }
}

if I do this I get a successful return


Spark.function("aerator", aeratorfunc);

}

//
// runs based upon cloud input
//

int aeratorfunc(String command)
{
// clockwise valve aerator on relay  
  if (command == "1") 
    {   
  
    return 1;
    }
// counterclosckwise valve aerator off relay 
  else 
    {               
       return 0;
    }

This would block the :cloud: connection and not return a int status code to the API call. That's why you are see a failed to call.

You would want to set a flag that is polled in loop() and run those stuff in loop() instead.

6 Likes

Oh okay I think i understand I’ll give that a whirl. So you are saying set a variable or something and monitor that variable in my loop code to run my desired outcome?

Correct! A (very) basic example (and untested – but should illustrate the idea) would be:

bool run_aeratorfunc = false
bool aeratorfunc_command = 0;

void setup() {
  // do stuff
}

void loop() {
  // Check the flag to see if we need to run aeratorfunc();
  if(run_aeratorfunc) {
    aeratorfunc();
  }
}

void aeratorfunc() {
  if(aeratorfunc_command==1) {
    // do stuff
  } else {
    // do the other stuff
  }

  run_aeratorfunc = false; // Set the flag back to false
}

int cloud_aeratorfunc(String command) {
  aeratorfunc_command = command.toInt(); // Save this for later
  run_aeratorfunc = true; // The flag to tell loop() to run aeratorfunc()

  return aeratorfunc_command; // Re-use this variable to return an integer
}
6 Likes

Thanks for the help!

1 Like

Okay doke that makes sense for the most part. I’m not a programmer but a hobbiest so I had trouble following all of what you did. I ended up setting an interger and changing that interger around to trigger code in the loop. It all works flawlessly now so thanks to all that helped. I had no clue that the delays blocked the cloud connection when not in the loop. I now have automated pool valves. So I’m halfway to my goal for this pool project. :smile:

2 Likes

That’s great to hear! Keep hacking and don’t hesitate to ask in the forums if you get stuck again. There’s plenty of people floating around to help!

3 Likes