Spark API returning an array or string

Is it possible to return anything other than an int to the Spark API cloud? I was thinking that it would be interesting to have a function on my Spark Core that would provide an API endpoint that would tell my webapp how to set itself up. Also this might pave the way to use some of the HATEOS principles with the spark api.

Maybe this might help?

Return some strings when a client connects to the Spark core and process accordingly :slight_smile:

@kennethlimcp a TCP server is a too low level interface to achieve the goal, especially considering we are talking about micro controllers, unable to run a full blown HTTP server on their own without sacrificing a big share of their computational power.

The :spark: already has a cloud and exposes a REST API, we need to leverage that and allow more power to be unleashed. I’m with you @greatwitenorth!

From what I understand c++ doesn’t allow returning of arrays from a function. But this StackOverflow thread suggest a pointer could be returned. What if we returned a pointer and the spark cloud somehow retrieved the array’s contents and returned it as JSON (I’m not even sure this is possible)? This would allow for some really interesting possibilities.

C strings work fine as :spark: variables. There are some size limits, which got bigger in the last round of updates from the :spark: team. Here’s an example that worked around a prior problems returning doubles (which is also fixed now) showing how a string variable is setup:

#include <math.h>

double result = 0;
char resultstr[64];

void setup()
  Spark.variable("result", &resultstr, STRING);

void loop()
  result = log10(10000);
  sprintf(resultstr, "%f",result);

Hi @greatwitenorth,

Although right now Spark.function only works with functions that return an INT, you could definitely set a string variable exposed by Spark.Variable from your function. Then it’d just be a matter of calling your function, and then checking that variable. Down the road I believe we will be adding support for other function return types.


1 Like

You could quite easily return a JSON array for parsing, depending on your application.

1 Like