Sending parameters to functions using Postman (not curl)

Just got the Spark Core running and am trying to send a parameter to a function. I am not sing curl but rather Postman extension on Chrome. Can’t figure out how to format the Postman get to get the function parameter down.
I load the following into Postman and POST, it calls the function (I can blink a light) but does not get the “arg=l1,HIGH” argument. I assume I am close but not quite there.

https://api.spark.io/v1/devices/DEVID/led/?access_token=ACCESS_TOKEN&args=l1,HIGH

Gotta be simple…

There is no l1 pin, so I hope that's a typo.

I'm pretty sure there are other topics about this, but to keep it simple, try this:

Let me know if that worked for you.

1 Like

I tried it but still no joy. I am trying the remote led blink example code found at
http://docs.spark.io/examples/

// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------

// name the pins
int led1 = D0;
int led2 = D1;

// This routine runs only once upon reset
void setup()
{
   //Register our Spark function here
   Spark.function("led", ledControl);

   // Configure the pins to be outputs
   pinMode(led1, OUTPUT);
   pinMode(led2, OUTPUT);

   // Initialize both the LEDs to be OFF
   digitalWrite(led1, LOW);
   digitalWrite(led2, LOW);
}


// This routine loops forever
void loop()
{
   // Nothing to do here
}


// This function gets called whenever there is a matching API request
// the command string format is l<led number>,<state>
// for example: l1,HIGH or l1,LOW
//              l2,HIGH or l2,LOW

int ledControl(String command)
{
   int state = 0;
   //find out the pin number and convert the ascii to integer
   int pinNumber = (command.charAt(1) - '0') - 1;
   //Sanity check to see if the pin numbers are within limits
   if (pinNumber < 0 || pinNumber > 1) return -1;

   // find out the state of the led
   if(command.substring(3,7) == "HIGH") state = 1;
   else if(command.substring(3,6) == "LOW") state = 0;
   else return -1;

   // write to the appropriate pin
   digitalWrite(pinNumber, state);
   return 1;
}

It says use curl like this

curl https://api.spark.io/v1/devices/0123456789abcdef/led
-d access_token=123412341234
-d params=l1,HIGH

and I am trying to use Postman instead with the idea I can write some javascript after that.

1 Like

If your goal is Javascript, you should read this over:

It is very easy with a little AJAX.

1 Like

Also, you could take a look at SparkJS, the JavaScript library. There are still a few kinks to be worked out, but functions, variables, and events are working.
You could take a look at this page I made with the library. Also, on that page, could you try to execute the commands from the CURL thingy, and see if they’re working?

1 Like

Which led are you trying to control? D0 or D1?

The javascript references did the trick. I used Google Docs and wrote a script that worked. Code is below.
Thanks!!

function ledOn(){
  var payload =
      {
        "params" : "i1=HIGH",
        "access_token" : "ACCESS_TOKEN"
      };
  
  var options =
      {
        "method" : "post",
        "payload" : payload
      };
  
  result = UrlFetchApp.fetch("https://api.spark.io/v1/devices/DEVID/led", options);
  Logger.log(result);
}

The Spark example looks for “i0” or “i1” to set D0 or D1 HIGH or LOW respectively. I changed the example so it turned on D7 which is the on-board blue LED. so did not use D0 or D1.

Actually no.
I admit the original sample is a bit confusing, but it doesn't really care what your first character is and it's not using the digit as it comes in to reference the pin, but it decreases it by one as well, so you need to send ?1 for D0 and ?2 for D1 where ? stands for any char allowed in a String.

1 Like

Yes, it’s a confusing example. It’s trying to map ‘switch 1’ to D0 and ‘switch 2’ to D1, but that introduces the concept of indexes starting from one versus from zero, which isn’t always obvious to beginners.

I think it would be clearer to use D6 and D7, as that make it obvious that the ‘switch’ number isn’t the same as the led number; and has the side benefit that D7 is already wired up and therefore takes away the possibility that the leds have been incorrectly connected.

1 Like

I just ran into the same issue and figured it out for my Photon. I’m using the “new” standalone version of Postman, but it should be the same whether you’re using it or the Chrome App. I’m running with flash = 0.6.3.

I’m using POST with the following URL:

https://api.particle.io/v1/devices/<DEVICE>/<FUNC NAME>/?access_token=<ACCESS TOKEN>

In the “Body” section, I added the following under the “x-www-form-urlencoded” option, I added the following:

Key: args
Value: disable

I ended up creating two different versions of the command, one with “enable” as the argument, and the other with “disable” as the argument. Without the “x-www-form-urlencoded” option enabled, the args value would show up as an empty string.

Hope this helps.

1 Like

Thanks! this worked for me too.