Spark core web relay


#1

yes i have downloaded a project from instructables called a spark core web relay. ( iam using this on my photon).
i can not get the web page to work properly. I am using linux apache2.


#2

Is there a question?


#3

Yes! What’s this all about???


#4

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

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.


#5

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

/*
 * 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

  <!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

    <?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 to see how to properly format code on this forum.


#9

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.


#10

thank you for the tip i will learn formatting.


#11

spark core web relay instructable link


#12

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.


#13

your link did not work. web page not available.


#14

Whoops, my bad, link is fixed :speak_no_evil:


#15

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


#16

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. You can log in with your account, and it’ll show you all the functions/variables you’ve got exposed.


#17

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


#18

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.


#19

this is what i am after


#20

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


#21

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 :wink:

That said, I couldn’t help myself, and built you an example page… Give it a try, and let me know it that works for you.


#22

the example you built is awesome.this will work.