Multiple function calls and timing out

Hello Spark Community

I’ve put together my first spark project recently, a christmas light controller. For the most part the results work pretty well. Here’s a video of it in action:

I put this together by using an HTML5 Simon Says project by Mr. Uxmonk and simply modifying the code to call POST functions to my spark core depending on the note played. Here’s a small snippet of the web side code:

if(note==0){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light1ON" } );
}
if(note==1){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light2ON" } );
}
if(note==2){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light3ON" } );
}
if(note==3){
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "light4ON" } );
}
function myFunction() {
myVar = setTimeout(funcx, 800);
}
function funcx() {
$.post( "https://api.spark.io/v1/devices/xxx/lightcmd?access_token=yyy", { params: "alllightsOFF" } );
}

And then my spark side code is a pretty simple copy of the led example:

int lightcmd(String command) {
...
  if(command == "light1ON"){
  digitalWrite(light1, HIGH);
  }
  if(command == "light1OFF"){
  digitalWrite(light1, LOW);
  }
  if(command == "light2ON"){
  digitalWrite(light2, HIGH);
  }
  if(command == "light2OFF"){
  digitalWrite(light2, LOW);
  }
...
if(command == "alllightsOFF"){
digitalWrite(light1, LOW);
digitalWrite(light2, LOW);
digitalWrite(light3, LOW);
digitalWrite(light4, LOW);
}
...

It all works pretty well. I was actually surprised to find if someone pressed two notes at the same time, both lights would (seemingly) light up at the same time.

My main problem is that if someone hits the notes too fast, the spark appears to get confused and eventually seems to timeout. Is there some limitation on the number of POST function calls I can expect the spark to handle in a certain period of time? Or is there some sort of watchdog type function that I could potentially add to protect against the spark timing out? Any help would be appreciated.

2 Likes

You could write in a delay in your javascript so that you can only press one switch every second. You’re dealing with a pretty long total round trip, even on a cable modem, to go from input device -> internet -> web server (HTML5 interface) -> internet -> spark cloud -> internet -> core.

For me to use a few cores here and there to replace other things I’ve thrown together I’ve determined I needed a local cloud. (Plus the internet isn’t up 100% of the time, or in my control if/when it goes down)

The delay time between calls using the internet and using a local cloud are completely different. Using the local cloud, even running on an RPi, things are much snappier.

You give up a lot of ease of use by going local, but you gain a good bit in speed. I did a little stress test in php and can get almost 10 function calls/sec for a few seconds without a hiccup on a local cloud. I didn’t try it with the spark cloud (would be rude), but I doubt it could approach those speeds without penalty.

2 Likes

Thanks Jack,

The delay is actually surprisingly small and practically imperceivable, which is great. This also makes it a lot easier for some random passer by to interact with the lights from their own device.

I think i figured it out though, i put a flag in for the light on and off calls and prevent one from firing if there’s another function call ‘in progress’, the spark doesn’t seem to hang anymore. Something like this:

if(inprog<1){
if(command == "light1ON"){
inprog = 1;
digitalWrite(light1, HIGH);
}
if(command == "light1OFF"){
inprog = 1;
digitalWrite(light1, LOW);
}
if(command == "light2ON"){
inprog = 1;
digitalWrite(light2, HIGH);
}
if(command == "light2OFF"){
inprog = 1;
digitalWrite(light2, LOW);
}
...
if(command == "alllightsOFF"){
inprog = 1;
digitalWrite(light1, LOW);
digitalWrite(light2, LOW);
digitalWrite(light3, LOW);
digitalWrite(light4, LOW);
}
...
inprog = 0;
}

Probably not the most eloquent way of preventing multiple / conflicting function calls but it seems to have done the trick.

1 Like