How to change a variable in Spark Core?

I know how to get variable from spark core using the Spark.variable, PHP, cURL

But how to change the variable from a webpage like the image below?

@5h177y, Spark.variable() is used to expose a variable to the cloud but NOT change it. You will need to use Spark.function() to make the changes. :smile:

Hi @5h177y

Have a look at this tutorial that explains how to use Spark.function() and Spark.variable() to set and get values.

2 Likes

Hi @peekay123, Thanks for reply!
Do you have any example to do this?

I got this error:

In file included from ../inc/spark_wiring.h:29:0,
from ../inc/application.h:29,
from testing.cpp:2:
../../core-common-lib/SPARK_Firmware_Driver/inc/config.h:12:2: warning: #warning "Defaulting to Release Build" [-Wcpp]
#warning "Defaulting to Release Build"
^
testing.cpp:1:17: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

^
testing.cpp: In function 'int receiveMess(String)':
testing.cpp:17:13: error: cannot convert 'String' to 'char*' in assignment

^
make: *** [testing.o] Error 1

My code:

char *message = "abcd";
int receiveMess(String newMess);

void setup()
{
  Spark.variable("mess", message, STRING);
  Spark.function("receivemess", receiveMess);
}

void loop()
{
    
}

int receiveMess(String newMess)
{
    message = newMess;
    return 0;
}

Try this

char message[64] = "abcd";
int receiveMess(String newMess);

void setup()
{
  Spark.variable("mess", message, STRING);
  Spark.function("receivemess", receiveMess);
}

void loop()
{

}

int receiveMess(String newMess)
{
    newMess.toCharArray(message,64);
    return 0;
}
1 Like

Thanks @bko, I verified the code and no error, but it doesn’t work.
I want to change the variable “message” from a webpage.

my PHP code:

<?php
$my_device = "xxx";
$my_access_token = "xxx";
$url = "https://api.spark.io/v1/devices/".$my_device."/mess/?access_token=".$my_access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
echo "Variable: " . $json['result'];

if(isset($_POST['submit']))
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=" . $my_access_token . "&args=" . $_POST['value']);
	curl_exec ($ch);
	curl_close ($ch);
}
?>

<form action="sparkcoretesting.php" method="post">
Variable: <input type="text" name="value">
<input type="submit">
</form>
1 Like

Hi @5h177y

Sorry I am not a PHP person. The only thing that jumped out at me was that the value for the POST should have a field name as well as in this example:

curl https://api.spark.io/v1/devices/0123456789abcdef01234567/receivemess\ -d access_token=1234123412341234123412341234123412341234 \ -d "args=my_string_here"

You could try curl on the command line or try my Javascript from the tutorial I mentioned above.

Thanks for helping me so much @bko ! :smiley:
It’s working now :smile:

PHP CODE:

<?php
$my_device = "<device id here>";
$my_access_token = "<access token here>";
$url = "https://api.spark.io/v1/devices/".$my_device."/mess/?access_token=".$my_access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$json = json_decode($result, true);
echo "Variable: " . $json['result'];

if(isset($_POST['submit']))
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://api.spark.io/v1/devices/".$my_device."/receivemess");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=" . $my_access_token . "&args=" . $_POST['messagebox']);
	curl_exec($ch); 
	curl_close($ch);
	header("Location:sparkcoretesting.php");
}
?>
<br>
<br>
<form method="post">
Variable: <input type="text" name="messagebox">
<input type="submit" name="submit">
</form>

And I tried Javascript too:

<!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>
<span id="showMessage"></span>
<br>
<input type="text" id="inputmessage">
<input type="button" onClick="setValue(inputmessage)" value="Submit">

<script type="text/javascript">

var deviceID    = "<device id here>";
var accessToken = "<access token here>";
var receiveMess = "receivemess";
var getMess = "mess";

window.setInterval(function() {
        requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getMess + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("showMessage").innerHTML = json.result;
                 });
      }, 1);

      function setValue(obj) {
        var newValue = document.getElementById('inputmessage').value;
        sparkreceiveMess(newValue);
      }


      function sparkreceiveMess(newValue) {
	var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + receiveMess + "/";
        $.post( requestURL, { args: newValue, access_token: accessToken });
      }
</script>
</body>
</html>
1 Like

A better version of Javascript, only send request when loading page and click the submit button:

<!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 onLoad="showMessage()">
<span id="showMessage"></span>
<br>
<input type="text" id="inputmessage">
<input type="button" onClick="setValue(inputmessage);showMessage()" value="Submit">

<script type="text/javascript">

var deviceID    = "<device id here>";
var accessToken = "<access token here>";
var receiveMess = "receivemess";
var getMess = "mess";

function showMessage() {
        requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getMess + "/?access_token=" + accessToken;
        $.getJSON(requestURL, function(json) {
                 document.getElementById("showMessage").innerHTML = json.result;
                 });
      }

      function setValue(obj) {
        var newValue = document.getElementById('inputmessage').value;
        sparkreceiveMess(newValue);
      }

      function sparkreceiveMess(newValue) {
	var requestURL = "https://api.spark.io/v1/devices/" +deviceID + "/" + receiveMess + "/";
        $.post( requestURL, { args: newValue, access_token: accessToken });
      }
</script>
</body>
</html>
1 Like

@peekay123 @bko
Hi, Can I do the same thing using TCP and how?

Hi @5h177y

I am not sure what you mean by using TCP–the Spark cloud connection does use TCP.

If you meant, can you do something similar without using the Spark cloud, then of course you can, but you will have to write a lot more code. The cloud provides a quick and easy path to control your Spark. But you could make your core provide a web server or web client to do similar things, it is just a lot more work.

1 Like