# Spark core web relay

**URL:** <https://community.particle.io/t/spark-core-web-relay/15942>\
**Category:** Troubleshooting\
**Created:** [September 25, 2015, 12:40am UTC](https://community.particle.io/t/spark-core-web-relay/15942 "2015-09-25T00:40:48Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 12:40am UTC](https://community.particle.io/t/spark-core-web-relay/15942/1 "2015-09-25T00:40:48Z")

</div>

yes i have downloaded a project from instructables called [a spark core web relay.](http://www.instructables.com/id/Spark-Core-controlled-relay-w-web-panel/) ( iam using this on my photon).  
i can not get the web page to work properly. I am using linux apache2.

---

<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 25, 2015, 12:45am UTC](https://community.particle.io/t/spark-core-web-relay/15942/2 "2015-09-25T00:45:21Z")

</div>

Is there a question?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [September 25, 2015, 12:53am UTC](https://community.particle.io/t/spark-core-web-relay/15942/3 "2015-09-25T00:53:33Z")

</div>

Yes! What’s this all about???

---

<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 25, 2015, 1:19am UTC](https://community.particle.io/t/spark-core-web-relay/15942/4 "2015-09-25T01:19:54Z")

</div>

A link to the project, and the code you're using (for both the device as well as the web page) would be nice.

> [@fcw158](#):
>
> i can not get the web page to work properly.

What exactly doesn't work?

* * *

_We shouldn't have to go around looking where you got your project from if you require help. We also aren't magicians who can guess what a topic is about without any explanation. Next time, please ask a question to go along with a piece of random code. Furthermore, removing formatting (which makes your code readable) after we had to add it in the first place, is generally not appreciated._

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:23am UTC](https://community.particle.io/t/spark-core-web-relay/15942/5 "2015-09-25T01:23:09Z")

</div>

understood the format did not look correct that is why i pulled it off. i did have a link in the last reply but it did not show. i will try the code again. thank you

this is the code

```cpp
/*
 * This is a minimal example for using a relay for controlling appliances.
 * The idea is to control a set of lights and maybe some music to set some mood
 * but this project can be used for anything, really.
 */

// name the pins
int dev1 = D0;
int dev2 = D1;
int dev3 = D2;
int dev4 = D3;

// we set all the devices states to 0 (off)
int dev1_state = 0;
int dev2_state = 0;
int dev3_state = 0;
int dev4_state = 0;

void setup() 
{
    Spark.function("switcher", switcher); // this makes your function public
    
    // set the pins to output mode
    pinMode(dev1, OUTPUT); 
    pinMode(dev2, OUTPUT);
    pinMode(dev3, OUTPUT);
    pinMode(dev4, OUTPUT);
}

void loop()
{
  // this loops forever
}

// this is the function that we're going to call from our web panel
int switcher(String command)
{
  if(command == "dev1")
  {
    digitalWrite(dev1, (dev1_state) ? HIGH : LOW);
    dev1_state = !dev1_state;
    return 1;
  } else if(command == "dev2")
  {
    digitalWrite(dev2, (dev2_state) ? HIGH : LOW);
    dev2_state = !dev2_state;
    return 1;
  } else if(command == "dev3")
  {
    digitalWrite(dev3, (dev3_state) ? HIGH : LOW);
    dev3_state = !dev3_state;
    return 1;
  } else if(command == "dev4")
  {
    digitalWrite(dev4, (dev4_state) ? HIGH : LOW);
    dev4_state = !dev4_state;
    return 1;
  } else if(command == "turnoff")
  {
    digitalWrite(dev1, LOW); 
    digitalWrite(dev2, LOW); 
    digitalWrite(dev3, LOW); 
    digitalWrite(dev4, LOW); 
    return 1;
  } else if(command == "turnon")
  {
    digitalWrite(dev1, HIGH); 
    digitalWrite(dev2, HIGH); 
    digitalWrite(dev3, HIGH); 
    digitalWrite(dev4, HIGH); 
    return 1;
  }
  else return -1;
}

```

this the index.php

```auto
  <!DOCTYPE HTML>
<html>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<body>

    <br><br>
    <button id="verde" style="background:green" onclick="switcher('dev1')">DEVICE 1</button>
    <button id="rojo" style="background:red" onclick="switcher('dev2')">DEVICE 2</button>
    <button id="azul" style="background:blue" onclick="switcher('dev3')">DEVICE 3</button>
    <button id="rainbow" style="background:white" onclick="switcher('dev4')">DEVICE 4</button>
    <button id="apagar" style="background:grey" onclick="switcher('turnon')">TURN ON</button>
    <button id="apagar" style="background:black" onclick="switcher('turnoff')">TURN OFF</button>
    <br><br>
    <script type="text/javascript">
	var deviceID = "xxxxxxxxxxxxxxxxxxxx"; // set with your Spark Core device ID :)

	function switcher(dev) {
		var funcion = 'switcher';
		var paramStr = dev.toString();

		var requestURL = deviceID + "/" + funcion ;
		$.post( 'proxy.php?' + requestURL , { params: dev });
	}

	
    </script>
</body>
</html>

```

this is the proxy.php

```auto
    <?php
// Set your access token here
define('xxxxxxxxxxxxxxxxxxxxxxxxxxx ');

// All responses should be JSON
header('Content-type: application/json');

// Security check!
if(!isset($_SERVER['HTTP_REFERER']) || substr_count($_SERVER['HTTP_REFERER'], $_SERVER['SERVER_NAME'])==0)
        die(json_encode(array(
                'error' => 'Invalid request'
        )));

// Build the URL. Since it's possible to accidentally have an
// extra / or two in $_SERVER['QUERY_STRING], replace "//" with "/"
// using str_replace(). This also appends the access token to the URL.
$url = 'https://'.str_replace('//', '/', 'api.spark.io/v1/devices/'.$_SERVER['QUERY_STRING'].'?access_token='.ACCESS_TOKEN);

// HTTP GET requests are easy!
if(strtoupper($_SERVER['REQUEST_METHOD'])=='GET')
        echo file_get_contents($url);

// HTTP POST requires the use of cURL
elseif (strtoupper($_SERVER['REQUEST_METHOD'])=='POST') {
        $c = curl_init();

        curl_setopt_array($c, array(
                // Set the URL to access
                CURLOPT_URL => $url,
                // Tell cURL it's an HTTP POST request
                CURLOPT_POST => TRUE,
                // Include the POST data
                // $HTTP_RAW_POST_DATA may work on some servers, but it's deprecated in favor of php://input
                CURLOPT_POSTFIELDS => file_get_contents('php://input'),
                // Return the output to a variable instead of automagically echoing it (probably a little redundant)
                CURLOPT_RETURNTRANSFER => TRUE
        ));

        // Make the cURL call and echo the response
        echo curl_exec($c);

        // Close the cURL resource
        curl_close($c);
}
?>

```

* * *

Moors7: Please check [this topic](https://community.particle.io/t/forum-tips-and-tricks/3999/2?u=moors7) to see how to _properly_ format code on this forum.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:34am UTC](https://community.particle.io/t/spark-core-web-relay/15942/9 "2015-09-25T01:34:26Z")

</div>

i have inserted my access token and device id in the correct location. i just can not control of my LEDs connected to pins D0-D4.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:35am UTC](https://community.particle.io/t/spark-core-web-relay/15942/10 "2015-09-25T01:35:53Z")

</div>

thank you for the tip i will learn formatting.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:41am UTC](https://community.particle.io/t/spark-core-web-relay/15942/11 "2015-09-25T01:41:30Z")

</div>

[spark core web relay instructable link](http://www.instructables.com/id/Spark-Core-controlled-relay-w-web-panel/)

---

<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 25, 2015, 1:45am UTC](https://community.particle.io/t/spark-core-web-relay/15942/12 "2015-09-25T01:45:50Z")

</div>

Alright. Next time, consider posting things together, no need to make a separate post for each piece of code. That’ll just clutter up the topic. Posting things after each other might be considered ‘bumping’, which is something we’d like to avoid.

That said, is there any special reason why you’d want to use a proxy? It’d be easier if you weren’t to use that. I’ve got an example HTML page to call four functions without a proxy over [here.](http://jordymoors.nl/particle/demos/function_button.html)

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:48am UTC](https://community.particle.io/t/spark-core-web-relay/15942/13 "2015-09-25T01:48:27Z")

</div>

your link did not work. web page not available.

---

<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 25, 2015, 1:50am UTC](https://community.particle.io/t/spark-core-web-relay/15942/14 "2015-09-25T01:50:15Z")

</div>

Whoops, my bad, link is fixed 🙊

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 1:54am UTC](https://community.particle.io/t/spark-core-web-relay/15942/15 "2015-09-25T01:54:55Z")

</div>

reading the spark core web relay it stated that the proxy.php would hide the access token.

---

<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 25, 2015, 1:58am UTC](https://community.particle.io/t/spark-core-web-relay/15942/16 "2015-09-25T01:58:26Z")

</div>

Yes, that’s one reason to use it. An alternative would be to log in using your credentials. That way, you wouldn’t have to hard-code the accesstoken anywhere. you also don’t need the proxy then, and you can thus make direct calls to the cloud. That’s what I’ve done [here.](http://www.jordymoors.nl/interface) You can log in with your account, and it’ll show you all the functions/variables you’ve got exposed.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 25, 2015, 2:01am UTC](https://community.particle.io/t/spark-core-web-relay/15942/17 "2015-09-25T02:01:05Z")

</div>

ok i will give it a try and report back tomorrow. thank you for your help and for your patient.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 26, 2015, 11:56am UTC](https://community.particle.io/t/spark-core-web-relay/15942/18 "2015-09-26T11:56:10Z")

</div>

i tried you link it worked great , but i am looking to have buttons on a web page so i can control lighting in my house either from my laptop, or iphone via a web page.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 26, 2015, 12:13pm UTC](https://community.particle.io/t/spark-core-web-relay/15942/19 "2015-09-26T12:13:42Z")

</div>

this is what i am after

![](https://sea2.discourse-cdn.com/flex026/uploads/particle/original/4/7/47a86f1ffab37a6e0d04bbfc00d540e1689da82f.png)

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 26, 2015, 12:14pm UTC](https://community.particle.io/t/spark-core-web-relay/15942/20 "2015-09-26T12:14:29Z")

</div>

well off to work i will be back tonight.  
thanks in advance

---

<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 28, 2015, 1:03pm UTC](https://community.particle.io/t/spark-core-web-relay/15942/21 "2015-09-28T13:03:52Z")

</div>

> [@fcw158](#):
>
> i am looking to have buttons on a web page

But they _are_ buttons on a webpage(?) They allow you to toggle _any_ function you've got. What you program those functions to do is entirely up to you 😉

That said, I couldn't help myself, and built you [an example page...](http://jordymoors.nl/particle/demos/function_toggle.html) Give it a try, and let me know it that works for you.

---

<div class="post-metadata">

**Author:** ![fcw158](https://avatars.discourse-cdn.com/v4/letter/f/d6d6ee/32.png) [@fcw158](https://community.particle.io/u/fcw158)\
**Post date:** [September 29, 2015, 1:27am UTC](https://community.particle.io/t/spark-core-web-relay/15942/22 "2015-09-29T01:27:16Z")

</div>

the example you built is awesome.this will work.
