Controlling LED with iPad and smart BASIC and Spark Core

Hi,

I found a remarkable useful programming language “smart BASIC” for my iPad to build user interfaces. Now I can control my Spark Core. It will be useful too for the PHOTON. The next smart BASIC code switches a LED on or off.

COREID$ = "*******************"
ACCTKN$ = "********************"

REM 1 = ON, 0 = OFF
led1 = 1 
IF led1 = 1 THEN
	send_cmd$ = "params=l1,HIGH"
ELSE
	send_cmd$ = "params=l1,LOW"
ENDIF
URL$ = "https://api.*****/devices/" & COREID$ & "/led?access_token=" & ACCTKN$
h$(1) = "content-type:application/x-www-form-urlencoded; charset=UTF-8"
HTTP URL$ HEADER h$ POST send_cmd$
PRINT HTTP_RESPONSE$()
PRINT "HTTP HEADER: " & HTTP_HEADER$()
PRINT "HTTP STATUS: " & HTTP_STATUS()
PRINT "HTTP ERRORS: " & HTTP_ERROR$()
END

The Spark Core is flashed with the next c 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<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;
}

My smart BASIC post can be found on this website.

Next thing to do is to build a nice interface.