Tilt Sensor to Live Web

Tilting my laptop changes the background color of a div element on a webpage. [Live Tilt]

An applet reads tilt sensor values from my laptop, posts them to sensorbase.org, then a webpage running ajax reads the sensor values and changes a background color. LIVE.

It’s super slow right now because I can’t figure out how to get sensorbase to only send me the latest value in the dataset, so i have to poll through a ton of values until i reach the end of the set.
Last polled id number is written and read from a txt file to keep count. Live tilt values to web color updates every 500 milliseconds now.

Procesing Code:

//slog tilt sensor to sensorbase.org example

import processing.net.*;
import sms.*;
int x,y,z;

//Client c;
String data;
//boolean httpRequestDone=true;

void setup()
  //c = new Client(this, "yoursite.com", 80); // Connect to server on port 80

void draw()
  int[] vals = Unimotion.getSMSArray();

  int multip=5;
  println(x + " " + y + " " + z);


void slog() {

  //i shouldn't be making a client each time, but i don't know what to do when it craps out
  Client c = new Client(this, "yoursite.com", 80); // Connect to server on port 80
  //slog to tom igoes slogger php script that lives on your server http://www.tigoe.net/pcomp/code/category/PHP/253
  c.write("GET http://yoursite.com/slogger.php?x="+x+"&y="+y+"&z="+z);
  // Identify yourself. must keep the space before HTTP
  c.write(" HTTP/1.1\nHOST:yoursite.com\n\n");

  if (c.available() > 0) { // If there's incoming data from the client...
    data = c.readString(); // ...then grab it and print it
    //ok to send next String

The processing sketch sends GET requests to Tom Igoes slogger php script that lives on my server

Here’s the webpage that reads values from sensorbase.org and displays the color based on the tilt values

<style type="text/css">

		<title>Live Background</title>
		<script type="text/javascript" src="ajax.js"></script>

		<script type="text/javascript">

			// A variable to hold the interval id
			var interval = null;	
			// A function to call our AJAX PHP script
			function call_ajax()

			// A function that gets called when ajax returns
			function ajax_return(response)
				document.getElementById("m1").innerHTML = response;

			// Setup AJAX function, creates a timeout so that we run something periodically
			function setup_ajax()
				// Keeps going...
				//interval = setInterval("call_ajax()",5000);
				// Only happens once..
				//interval = setTimeout("call_ajax()",50);
			// Register setup_ajax with the onload event of the window (when it is done loading)..	
			function addOnLoad()
				if (window.addEventListener)
					window.addEventListener('load', setup_ajax, false);
				else if (window.attachEvent)
					window.attachEvent('onload', setup_ajax);
		<small><div id="m1" style="height:35px; width:100%;">
		checking sensorbase.org...

To make the httpRequests you need the ajax.js file. You also need the getSlog.php file below to request the sensorbase.org table values (uses the same sensorbase_config.php file from Tom Igoe’s example. It also uses the sensorbase.wsdl file.


//get sensorbase user password etc
include "sensorbase_config.php";           

$client = new SoapClient("sensorbase.wsdl", array("trace"=>1,"exceptions"=>0));

$fields = "x,y,z";
$tables = "p_{$projectId}_{$tableName}";
$condition = "x>=0 || x<0"; 
$delta = 1;
$type = 'csv';

//total hack to get the last $from table value
//sensorbase doesn't return in descending order?

//get sbid number from counter file
$counterFile = "counter.txt";
$cfr=fopen($counterFile, 'r+') or die("can't open file");
$sCount = fread($cfr, filesize($counterFile));

while ($csv!=null)
$csv = $client->getData($email, $password, $fields, $tables, $condition, $sbid, $delta, $type);
$csv = $client->getData($email, $password, $fields, $tables, $condition, $sbid, $delta, $type);

//write to counter file
$cfw = fopen($counterFile, 'w');
fwrite($cfw, $sbid);

echo $csv;
//return $csv;