Javascript turn on led


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.

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 = "" +deviceID + "/" + setFunc + "/";
    $.post( requestURL, { params: paramStr, access_token: accessToken });

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: 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?,HIGH

You are close but not quite there. I just wrote this:

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`


// From
//set POST variables
$accessToken = "aaaa";
$deviceID = "skjaska";
$deviceFunction = "led";
$argument = "l1,HIGH";

$url = '' . $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


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";
var requestURL = "" +deviceID + "/" + setFunc + "/";
    $.post( requestURL, { params: paramStr, access_token: accessToken });

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. ( 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 ?