Anyone can help me with an example to do a simple html code to incorporate Spark API. I want a web page to two buttoms/ links to turn ON / OFF led(s) on NET using this code on Spark:
// -----------------------------------
// 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';
//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 was written by jflasher actually. Tonight I’ve been hacking on it to make something even more simple for a controller. Just something you can hard code in your coreID and accessToken and upload to your own web host. When I get a few more things done I’ll upload to my github page.
Let me know if you have any questions. You can download the zip file and unzip to anywhere on your hard drive and run it locally first to test it out. Just edit the index.html and look for the user data near the top. The readme.md file also explains things for you. There is an example sketch that you can copy/paste into your Spark WebIDE as well.
My instructions for the first one I did are missing a key piece of information! You need to Flash the contents of this file to your Core:
It contains the “test” funcKey and testFunction that will toggle the D7 LED.
I created two other more sophisticated examples with better documentation since then, so I’ll go back and fix it up a bit to match. (EDIT: Documentation Updated!) Here’s the others in case you want to play with these as well. Remote Spark Remote RTTTL
Sorry about that! I had changed the name of the file from RemoteSpark.ino to RemoteSpark.cpp and forgot to run the git add * command before pushing the doc changes. The result was it deleted the file. I just added it back!