Simple html POST to work with Spark API

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;
}  

I’m working on something even simpler than this, but this is a good start for your need. http://jflasher.github.io/spark-helper/

Just enter your funcKey four times “led”

and data for each one “l0,HIGH”, “l0,LOW”, “l1,HIGH”, “l1,LOW”

Not exactly what you want, but everything custom is a bit of work. It’s definitely a great starting point for you though.

Thats a great tool you have made there BDub. I have used it and it works perfectly. Now its time for me to learn how I can embed that on my own site.

I want to include authentication in code… (if possible) and just have ON OFF for 1 led to start of.

If someone can do a nice simple tutorial it will benefit all learner like me.

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.

That would be great BDub. Can you let me know when you finish with it. It will br a great resource for all of us new into this.

@Sanjay all finished! https://github.com/technobly/Simple-Spark-Core-Controller

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.

@BDub,

Can you help, i tried the code you posted, but can’t get ‘Test 1’ button to trigger core code. Is there something missing in HTML script ?

I’m no expert, but it looks like there is no data specific to an IO point on the spark being triggered from the script.

Do i need to edit this in some way ?

var funcKey1 = “test”;
var args1 = “”;
var label1 = “Test 1”; // button label 1

thanks

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

1 Like

Thank you for your reply.

I did try to use your example of Remote Spark, but couldn’t find the file RemoteSpark.cpp

Maybe this is what you are fixing up ?

Your help is very much appreciated

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!