Spark Cloud POST not working?

Hello,

I have flashed the following code onto my Spark Core and tried to POST via the command prompt to control an LED, but for some reason it is not working as intended…

int blue = D7;
int led1 = D6;
int led2 = D5;
int led3 = D4;
int led4 = D3;
int led5 = D2;
int led6 = D1;
int button = D0;

void setup()
{
Spark.function("bigblue", bigblue);
pinMode(blue, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(button, INPUT);
}

void loop()
{
digitalWrite(blue, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
digitalWrite(blue, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
digitalWrite(blue, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
digitalWrite(blue, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
digitalWrite(blue, HIGH);
digitalWrite(led5, LOW);
digitalWrite(led4, HIGH);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
digitalWrite(blue, LOW);
digitalWrite(led4, LOW);
digitalWrite(led3, HIGH);
attachInterrupt(button, check, CHANGE);
delay(300);
attachInterrupt(button, check, CHANGE);
}

void check()
{
if (digitalRead(button) == HIGH)
{
digitalWrite(led1, HIGH);
}

else if (digitalRead(button) == LOW)
{
    digitalWrite(led1, LOW);
}
}

int bigblue(String onoff)
{
if (onoff.substring (0,1) == "ON")
{
digitalWrite(led6, HIGH);
}

else if (onoff.substring (0,2) == "OFF")
{
    digitalWrite(led6, LOW);
}

else
{
    return -1;
}

return 1;
}

It should go over to digitalWrite(led6, HIGH) and turn the LED on, but instead just goes straight to the “else” option and returns -1. If I return 1 under else, it returns 1.

Thanks in advance!

substring(X,Y) selects from the Xth position in the target String to the Yth.

you can try to change your function to something like this:

int bigblue(String onoff)
{
  if (onoff.substring (0,3) == "ON")
  {
    digitalWrite(led6, HIGH);
    return 1;
  }

  else if (onoff.substring (0,3) == "OFF")
  {
    digitalWrite(led6, LOW);
    return 0;
  }

  else
  {
    return -1;
  }
}

and call you function like this:

curl  -k https://api.spark.io/v1/devices/<DeviceID>/bigblue -d access_token=<AccessToken> -d params=ON

try that...

1 Like

Oh… I forgot to ask…

What are you trying to do here?

void loop()
{
  digitalWrite(blue, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
  digitalWrite(blue, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
  digitalWrite(blue, HIGH);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
  digitalWrite(blue, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
  digitalWrite(blue, HIGH);
  digitalWrite(led5, LOW);
  digitalWrite(led4, HIGH);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
  digitalWrite(blue, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led3, HIGH);
  attachInterrupt(button, check, CHANGE);
  delay(300);
  attachInterrupt(button, check, CHANGE);
}

void check()
{
if (digitalRead(button) == HIGH)
{
digitalWrite(led1, HIGH);
}
1 Like

Hi, Sorry for the late reply! I was away for the weekend.

I have 5 LED’s on the breadboard and 4 are to blink in a sequence, while the first one only reacts to a switch, regardless of where the sequence currently is.

This shouldnt have anything to do with the curl command, as that LED is yet another LED, a big blue one (hence bigblue) :stuck_out_tongue:

I got it to work by the way, I just used if onoff == “ON” and if onoff = “OFF” and it worked just fine :smile:

Although I still dont know why it didnt work with the other method :confused: