Javascript turn on led

Hello,

I adapted a function that should trigger a led, she placed inside a while PHP, but it does work. is there any command that I run into the php loop to execute the javascript? The While works fine.

<?php
while($row = mysql_fetch_array($retval, MYSQL_BOTH)){
?>
    <script type="text/javascript">
    var deviceID    = "xxxxxxxxxxxxxxxxxx";
   var accessToken = "kkkkkkkkkkkkkkkkkkkkkkkkkkkk";
   var setFunc = "led";
   function exemplo() {
   paramStr = "l1,LOW";

var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + setFunc + "/";
    $.post( requestURL, { params: paramStr, access_token: accessToken });
     document.write(paramStr);
   }
 <?php
  ...
 ?>

This could be a silly question but is the tibker firmware on your spark core?

Additionally since you are already using php I’d look into using curl instead of Java script so you don’t expose your access credentials

I put the access code and token only to test, I already insert it on Mysql. The original code downloaded from spark core website works fine, but spark example have a button and here I want run javascript with while without button

I understand but by using javascript you arw exposing your real access credentials when you render the page. If you weren’t using php i would understand but since you are (which is more secure anyway) take a look at this:http://php.net/manual/en/curl.examples-basic.phpand try adapting it for spark. I can throw Towle an example later

ok thank you I'll learn about curl

this line is correct to torn HIGH L1?

https://api.spark.io/v1/devices/0123456789abcdef/led/access_token=123412341234/params=l1,HIGH

You are close but not quite there. I just wrote this: https://gist.github.com/harrisonhjones/1990668b32ae68e4519e

I haven’t tested it (i’m sadly not on my dev computer) but it should at least point you in the correct direction

yes because works in javascript. I want change to CURL

Thanks, but that code is wrong. If you can test it, I’ll thank

Just updated it a bit. Made some small mistakes. Try again and let me know

[Edit] If it still doesn’t work let me know what response you get from it (it should output the response from the Spark Cloud)

I found that code, but when I try run on my browser I receive that error:

Undefined variable: fields_string on line 24`

<?php

// From http://davidwalsh.name/curl-post
//set POST variables
$accessToken = "aaaa";
$deviceID = "skjaska";
$deviceFunction = "led";
$argument = "l1,HIGH";

$url = 'https://api.spark.io/v1/devices/' . $deviceID . '/' + $deviceFunction ;
$fields = array(
	'access_token' => urlencode($accessToken),
	'args' => urlencode($argument)
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

I updated the gist. I added a $fields_string = ""; in there to be safe

Now I don’t receive error, the code run but don’t change the D0. I have that code working using javascript,I want change it to CURL. I want open php file and CURL run automatically

    <button id="led1onbutton"  onclick="switchLED(1,0)">LED 1 On</button>

    var deviceID    = "ksdj";
    var accessToken = "kjdf";
    var setFunc = "led";


  function switchLED(ledn,newValue) {
    var paramStr = "l" + ledn.toString();
    if (newValue==1) { 
       paramStr = paramStr + ",HIGH";
       document.write(paramStr);
    }
var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + setFunc + "/";
    $.post( requestURL, { params: paramStr, access_token: accessToken });
  }
</script>

I’ve edited your post to properly format the code. Please check out this post, so you know how to do this yourself in the future. Thanks in advance! ~Jordy

What response do you get when you run the PHP file (not the Javascript version)? What does the cloud report?

where I can see this cloud report?

when I run the php file I don’t receive error,

When you run the php script I posted (or your modified version) it should output something (if not, add a echo $result; after curl_close($ch);)

I made an echo, but don’t receive nothing

Hmm. You should have. I updated the script. (https://gist.github.com/harrisonhjones/1990668b32ae68e4519e) What do you see now (obviously need to make your edits for access token)

I made an echo on $fields_string and receive it access_token=xxxxxxxxxxxxxxx&args=l1%2CLOW

maybe the problem is it: %2C ?