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.
// -----------------------------------
// 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;
}
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?
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.
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.
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.
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.