Simple Web API failing

Hi,
I have never worked with a web api before (I generally work with client side programming). However, I have followed the instructions from the examples page and it keeps saying the code is wrong. Help please, here is the code.

// -----------------------------------
// 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,
// 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’;
//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;
}

POST /v1/devices/{DEVICE_ID}/led

EXAMPLE REQUEST IN TERMINAL

Core ID is 0123456789abcdef01234567

Your access token is 1234123412341234123412341234123412341234

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

Hi @1rudster,

No worries, make sure your code stops before this line:

That line isn’t part of the code you’re writing, and is meant to show you what the web request should look like.

Thanks!
David

1 Like

Thanks so much! Happy New Year to you and your companion cube!

1 Like