# How do I call a function from a webpage etc

**URL:** <https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255>\
**Category:** Getting Started\
**Created:** [March 5, 2014, 7:16pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255 "2014-03-05T19:16:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 5, 2014, 7:16pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/1 "2014-03-05T19:16:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 5, 2014, 7:53pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/2 "2014-03-05T19:53:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Hypnopompia](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hypnopompia/32/9039_2.png) [@Hypnopompia](https://community.particle.io/u/Hypnopompia)\
**Post date:** [March 5, 2014, 8:02pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/3 "2014-03-05T20:02:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 5, 2014, 8:17pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/4 "2014-03-05T20:17:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 5, 2014, 8:26pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/5 "2014-03-05T20:26:51Z")

</div>

You can use this online tool to get started:

[http://jflasher.github.io/spark-helper/](http://jflasher.github.io/spark-helper/)

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

---

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 5, 2014, 9:49pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/6 "2014-03-05T21:49:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 5, 2014, 9:54pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/7 "2014-03-05T21:54:55Z")

</div>

You want to get a web app running?

Look at:

> **[jflasher/spark-helper](https://github.com/jflasher/spark-helper)**
>
> A web-based helper for the Spark Core. Contribute to jflasher/spark-helper development by creating an account on GitHub.

> **[technobly/SparkCore-WebApp](https://github.com/technobly/SparkCore-WebApp)**
>
> An amazing web based interface for your Spark Core. It uses the power of Node.js, Express, Jade, Stylus for the backend, jQuery and AngularJS for the frontend, and the Spark API to let you remote ...

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [March 5, 2014, 10:23pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/8 "2014-03-05T22:23:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 5, 2014, 11:46pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/9 "2014-03-05T23:46:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 5, 2014, 11:50pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/10 "2014-03-05T23:50:14Z")

</div>

@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 😄

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

See this thread:

> **[Yes, great, but how do I get it on a web page?](https://community.particle.io/t/yes-great-but-how-do-i-get-it-on-a-web-page/1050/4)**
>
> Hi @PaulRB, This is a great question with a lot of correct answers. It sounds like you’re asking about server-side examples that use the cloud api and display something on a website you’re running. It really depends on what environments you’re...

---

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 6, 2014, 1:19am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/11 "2014-03-06T01:19:26Z")

</div>

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"
});
```

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 6, 2014, 1:20am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/12 "2014-03-06T01:20:49Z")

</div>

What’s the json output from jQuery?

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [March 6, 2014, 2:13am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/13 "2014-03-06T02:13:52Z")

</div>

missing `"` after brew?

---

<div class="post-metadata">

**Author:** ![spydrop](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@spydrop](https://community.particle.io/u/spydrop)\
**Post date:** [March 6, 2014, 8:12am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/14 "2014-03-06T08:12:34Z")

</div>

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 😄

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [March 6, 2014, 8:37am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/15 "2014-03-06T08:37:05Z")

</div>

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

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!

---

<div class="post-metadata">

**Author:** ![Owen](https://avatars.discourse-cdn.com/v4/letter/o/b2d939/32.png) [@Owen](https://community.particle.io/u/Owen)\
**Post date:** [March 6, 2014, 11:38am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/16 "2014-03-06T11:38:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HugoHam](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@HugoHam](https://community.particle.io/u/HugoHam)\
**Post date:** [September 19, 2016, 8:46pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/17 "2016-09-19T20:46:49Z")

</div>

Hello everyone, first post here 😄

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

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [September 19, 2016, 9:02pm UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/18 "2016-09-19T21:02:35Z")

</div>

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 😉

> [@Web Led turn on/off from webpage without particle redirect info](http://community.particle.io/t/web-led-turn-on-off-from-webpage-without-particle-redirect-info/22838):
>
> Hi! I’m new to particle. I’ve been working through the Getting Started examples. I followed the ‘Control LED over the net’ example. I wanted to execute a variation of it: Clicking checkbox would turn the led on, Clicking off would turn the LED off(without a submit type). I was able to accomplish this. However as with the code example, the page refreshes every time a submit is sent to a new page. [image] I want to be able to turn the led on and off without a page redirect or refresh. I figure…

> [@Prevent Spark.function() from redirecting when button is pressed](http://community.particle.io/t/prevent-spark-function-from-redirecting-when-button-is-pressed/18725):
>
> I’ve been looking into this example: [https://docs.particle.io/guide/getting-started/examples/core/#control-leds-over-the-39-net](https://docs.particle.io/guide/getting-started/examples/core/#control-leds-over-the-39-net) and was wondering if it is possible to prevent the redirecting to the “response” in the .html file. I just want to stay on the same site website I was on. Anybody know if this is possible?

> [@Web connected led question](http://community.particle.io/t/web-connected-led-question/25575):
>
> Hello! slight_smile This is my first time using the particle photon and I decided to make a remote garage door opener. I used the web connected led example code but every time I click “do it” it gives me that page with the information is there any way I can skip that info so when I click “do it” it submits but never takes me to another page? -Thanks

---

<div class="post-metadata">

**Author:** ![HugoHam](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@HugoHam](https://community.particle.io/u/HugoHam)\
**Post date:** [September 20, 2016, 1:12am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/19 "2016-09-20T01:12:27Z")

</div>

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](https://community.particle.io/t/tutorial-spark-variable-and-function-on-one-web-page/4181) 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

---

<div class="post-metadata">

**Author:** ![KyleG](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/kyleg/32/11077_2.png) [@KyleG](https://community.particle.io/u/KyleG)\
**Post date:** [September 21, 2016, 12:35am UTC](https://community.particle.io/t/how-do-i-call-a-function-from-a-webpage-etc/3255/20 "2016-09-21T00:35:17Z")

</div>

Thanks @Moors7 for the assist!
