Function not found [SOLVED]

I get a function not found response from the API - my call to the API is this:

curl --verbose https://api.spark.io/v1/devices/xxx/lights -d access_token=xxx -d “args=0”

So the plan here is to call function ‘lights’ and pass a String parameter of “0” to it.

Code is below, who has an idea? I think the function is declared and also implemented… it all compiles and flashes, too… thx!

–>above: NeoPixel Spark library code…

int lights(String command);

void setup() {
  Spark.function("lights", lights);
  
  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

int lights(String command) 
{
  uint32_t c_on = strip.Color(255, 255, 255);
  uint32_t c_off = strip.Color(0, 0, 0);
  
  int l = command.toInt();
  
  for (int i = 0; i < 16; i++)
  {
      mylights[i] = (((l) >> (i)) & 0x01);
  }
  
  for (int i = 0; i < 16; i++)
  {
    int base = i * 4;
    strip.setPixelColor(base, (mylights[i] == 1) ? c_on : c_off);
    strip.setPixelColor(base+1, (mylights[i] == 1) ? c_on : c_off);
    strip.setPixelColor(base+2, (mylights[i] == 1) ? c_on : c_off);
    strip.setPixelColor(base+3, (mylights[i] == 1) ? c_on : c_off);
  }
  
  return 1;
  
}

void loop() {

}

Hi @hansamann,

Can you try hitting https://api.spark.io/v1/devices/your_device_id?access_token=your_access_token and see if your function is listed? Maybe try flashing again?

Thanks,
David

Yes, it leads to this:

{
id: “53ff6f065067544840351087”,
name: “bacon_ranger”,
variables: { },
functions: [ ]
}

Thsi is really strange.

After flashign, I was able to call the function via CURL once. I know it works as I got an array of LEDs connected and they walked up once. Then when I tried this again, it says function not found.

Could it be that it somehow hits an issue and reverts to an old version that does not have the function?

Do you have a check on this endpoint that does list the function before you call it? As far as I know it shouldn’t auto-rollback like that once your firmware has started running.

OK, I just really confirmed:

  • function is available (check via call you gave me),
  • function called successfully
  • second check - function no longer exists ???
  • second call, “Function not available”.

How can I figure out why the function goes away? One thing that is strange is this: after the first function seems to run successfully, the spark starts flashing green and then breathes cyan again. So it does something, that I find strange - what is that? Is is reinitializing, but maybe not setting up functions again?

Hi @hansamann,

Hmm, that would be really strange, your firmware would need to be messing with system flags and messing with memory to create a bug like that I would think. Can you post the full firmware and maybe we can try to reproduce it here?

Thanks,
David

Yes, the full code is here:

http://snipt.org/Eita1

The lights function is called with a String like “3” which is converted to an integer and then the individual bits are read to determine which of the 16 channels is on and off. I just had this code repetively running when the NeoPixel Lib was not included. Once I added the .h/.cpp and include + code for controlling the NeoPixel LEDs, it stopped working again. The function is no longer there…

Thx for looking into this, really appreciate it!

I believe the code is hanging due to the library calls to NeoPixel stuff because there is no strip.begin(); in the setup. Also missing strip.show()'s. Posted in the NeoPixel Library thread :wink:

User Code Hanging = No User Data Available to the cloud.

1 Like

Thanks @BDub , that sounds correct. @hansamann , a bit more context in case it’s useful:

You can run into problems if you call hardware initialization routines like pinMode outside of setup (before the hardware is ready). If you look at the implementation of neopixel , it’s begin() method does this–and should therefore be called in setup.

Hi @BDub and @jgoggins - I’ve updated my program and added the begin() and show() calls in setup, also after each time I am updating the NeoPixel strip. Unfortunately it still does not work and the behaviour is again very weird.

Latest code is here: http://snipt.org/Fari3

  • updated the program, started flashing -> spark begins flashing magenta, about one minute later it is back brathing cyan
  • I then call the device info to see if the function is registered: https://api.spark.io/v1/devices/53ff6f065067544840351087?access_token=xxx
  • it SHOWS the lights function
  • I now do a POST to that device’s lights function. with the Integer 3 for example, which will based on the code turn on the light channels 0 and 1 (3 binary). The call takes pretty long, In between the cyan quickly flashed after it started breathing again. The response was:
    {
    “error”: “Timed out.”
    }
  • I now call the device info again, like above to see of the function is there.
    function is gone :frowning: There was also nothing happening on the connectd RGB leds.

Pretty clueless. It works if I take out the strip/NeoPixel calls. But of course that is the main idea of the program, drive the neopixels with input via that function call.

So my latest trials are here now: http://snipt.org/FaBj1

I am trying to seperate the code that is called via the spark function from the code that operates the light channels. The idea is that the spark function can also return very quickly and does not block a long time.

So the loop now goes through the mychannels[] array and then updates the strip accordingly.

I’ve also taken out the Serial calls - just thought in case something goes wrong here.

Unfortunately, I can see via the GET /device call that the fucntion is there. But all the function calls for the lights function time out. I always get a timed out response.

It might be a tiny issue with the code, but as it all compiles and I cannot have a debugging view to the device, it’s hard to tell. Does anyone see an issue?

@BDub has solved the issue. It seems my code calculates a value that causes the NeoPixel lib to fail. He changed and fixed the issue, now I am trying to come back to my version and figure out what causes teh behavior in the NeoPixel lib. It will be around my usage of int (due to lack of knowledge of c) compared to uint16_t for example.

Thanks @hansamann for vocalizing your issues and @BDub for fixing, that’s sweet–this Neopixel library is awesome.