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.