Spark.variable() event

Hey guys,

I need to:

  1. change a LED state
  2. by clicking the bookmark in my home screen (iOS)

Well, clicking some bookmark means that I’ll be doing a HTTP GET to Spark

https://api.spark.io/v1/devices/DEVICEIDHERE/brew?access_token=ACCESSTOKENHERE

I’ve done some research, and I understood that GET’s are meant for Spark.function()
and that POST’s are for Spark.variable(). But I just need to make sure someone did the GET, nothing else.

So. Is it possible to attach another function to a Spark.variable() call? Where should I be looking at? Any other suggested solution?
If possible, I wouldn’t like to directly use TCPServer… it doens’t seem like a reliable solution.

Regards,

Gabriel

Actually, POST requests are for Spark.function() calls, and GET requests are to get Spark.variable(). Having cleared that, you could try using this code:

<html>
	<head>
	</head>
	
	<body>
		Function just executed.
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
		<script>
			var deviceID    = "Enter device ID here";	
			var accessToken = "Enter accestoken here";	
			var baseURL = "https://api.spark.io/v1/devices/";
					
			function doMethod(func, data) {			
			  var url = baseURL + deviceID + "/" + func;
			  $.ajax({
				type: "POST",
				url: url,
				data: {
				  access_token: accessToken,
				  args: data
				},
				dataType: "json"
			  })
			  console.log("Sent POST request.");
			};
			
			doMethod("Enter function name", "optional: Enter function argument);
		</script>
	</body>
</html>

Place it on a host you can reach, and add it as a bookmark. As soon as you open it, the function will be called.

Let me know if you need any help.

Sorry for the mixup, you’re absolutely right. Thanks for the solution, but I was thinking on something more Spark-Cloud-local.

No servers, no document.ready stuff… Just a plain GET to Spark API that calls LED changing state.

Is that even possible?

Thanks again,

Gabriel

Not currently. You need to be able to make a POST request to call any spark functions.

Is this not possible with a Bookmark?

In this topic I’ve tried to explain why I think it’s not currently possible:

My solution above is a fairly simple one, as long as you can host the page somewhere. I believe github can do that for free :slight_smile:
If you’re using the local cloud, then you should be able to edit the routing and make it do whatever you’d like it to do.

Have you tried to store @Moors7 code as a .html file locally on your device an bookmark this local file?
With my MS device this works like a charm.

1 Like

I also store a local HTML file, but on Dropbox. That way I can get to it from home Mac, my work PC, and my iOS device.

Thanks guys! All those ideas are working fine!

1 Like