How do I call a function from a webpage etc

I’ve looked at the Spark documentation and as usual it’s written for people who already know how to do things.

I’m trying to call a function on my spark that has three integer arguments. I’ve written the function and have a Spark.function(); in the void loop() but what I need to do is sent it the arguments somehow and I’m stumped on that.

It’s something to do with curl no doubt but I can’t seem to get the formatting right. I’ve searched the forums but found nothing of use to me.

It there a tutorial anywhere that explains things for complete beginners to all this stuff?

1 Like

Hi @Owen,

Sorry the docs wasn’t able to help!

Can you show me your code?

We can use the online tools to help us test this out or by a http link

@Owen The spark API only allows you to pass one argument into your function. (A String). Inside your function you’ll need to cut up the string unto multiple parts. From the docs, here is a sample function that does such a thing. The parameter string would look like “l1,HIGH”

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

Ok that helps a bit but I have two further questions;

How would I go about converting three numbers from a string into integers for the function?
Say I sent the numbers “12,600,6” in the command String how would I get those into a format I can put into my function?

And secondly how do I actually send those to the spark via a webpage?

Thanks

You can use this online tool to get started:

http://jflasher.github.io/spark-helper/

Also, your spark.function should be in the setup() loop :slight_smile:

I tried that webpage and it just tells me that the function is working. I doesn’t say how to call it myself from my own webpage without going through that website.

You want to get a web app running?

Look at:

Hi @Owen,

If you want to write some code that hits the API, the magic words you want are “http POST request”. Whichever programming language your site is using, you should be able to find an example, what language do you want to use? (Ruby, Python, C#, PHP, Javascript, Java, etc, etc)

Thanks,
David

The code for reading a Spark.variable is, for example

https://api.spark.io/v1/devices/0123456789abcdef01234567/temperature?access_token=1234123412341234123412341234123412341234

And the code for sending arguments to a functions is given as…

https://api.spark.io/v1/devices/0123456789abcdef01234567/brew \
     -d access_token=1234123412341234123412341234123412341234 \
     -d "args=202,230"

But how do you actually format the above to put into the address box on a web browser? (other than change the device ID, access token and function name).

My page is using Javascript and Jquery.

@Owen, doing directly using http in a web browser is not feasible.

You need some code to do that you. Like i use python to do so sometimes :smile:

Javascript should be able to as well. Let me find the code example

See this thread:

Still having no joy, here’s my code.

On the Spark Core:

int brewCoffee(String command);

void setup()
{
  //register the Spark function
  Spark.function("brew", brewCoffee);
}

void loop()
{
  //this loops forever
}

//this function automagically gets called upon a matching POST request
int brewCoffee(String command) 
{
  //look for the matching argument "coffee" <-- max of 64 characters long
  if(command == "coffee")
  {
    //do something here
    return 1;
  }
  else return -1;
}

And in the jQuery script:

$.ajax({
  type: "POST",
  url: "https://api.spark.io/v1/devices/CORE_ID/brew,
  data: {
	access_token: "ACCESS_TOKEN",
	args: "coffee"
  },
  success: function() { $("#result").text("Success"); } ,
  dataType: "json"
});

What’s the json output from jQuery?

1 Like

missing " after brew?

1 Like

Hi kenneth, again thanks for helping on my similar issue as you saved me a lot of time and frustration. As long as Owen is using the SPARK API Cloud then it is not possible to directly communicate with the spark core using a simple browser http request. However, it is possible to do this from a webpage on a local net using a TCP server and client browser and without the token. The code you supplied me did allow me to change the state of one Spark Core Digital pin from a wepage running on my Tiny Webserver. I only needed the IP of the Spark Core and the IP of my Tiny Webserver which I obtained from my router DHCP table… I put the Client IP (Webserver) in the Spark Core IDE and locally compiled and flashed the core.

I will do further testing this weekend and yes this would be unsecure over the Internet but I think Old School will be okay and that can be addressed in the future, as you suggested, setting up a local cloud on my local network and use the API.

Not everyone needs the cloud for over the internet access as all I need right now is local control using a webserver. I will post my code next week when I can do more than just turn D4 High / Low

I am hoping that by assigning the Spark Core a static IP and opening a port on my router to the Core Static IP that I can control my Core from a hosted Apache password protected Webpage. If I am successful I will call it the poor mans cloud :smile:

2 Likes

@skydrop, that’s partially true. You can do stuff with the variables via the :spark: cloud via direct http requests :smiley:

Im thinking you can still build in security with some simple form of encoding/decoding.

So fun hearing projects come to live and im glad im of help!

Got it working now. There was a typo as spotted by @Dave.

I can now turn an LED on and off via a webpage, which is nice.

3 Likes

Hello everyone, first post here :smile:

I’m using a Photon and controlling it with Particle.function() and Particle.variable() working great from an HTML+Javascript webpage. But I haven’t been able to modify something without the page taking me to a result page that looks like this:

{
“id”: “3b***********”,
“last_app”: “”,
“connected”: true,
“return_value”: 0
}

I tried iframe and some jquery tricks, but I cannot make it to act without loading this page.

What are my options?

I hope someone can help me. Thanks!

HH

Hey there, welcome to the community!

Your request is certainly possible, and you're not the first to ask. As such, have a look at these topics, they might contain some useful info :wink:

2 Likes

Hello Moors7,

Thank you very much for the quick and accurate response! Now my Photon is working exactly the way it is supposed to be.

I used the Tutorial: Spark Variable and Function on One Web Page I found on your first link, and I just want to make a clarification. In this tutorial, at the end of the requestURL, after the setFunc it adds a “/” sign, but what worked for me was a "?" sign instead. I hope this helps someone else.

Thanks!

HH

1 Like

Thanks @Moors7 for the assist!

1 Like