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
//cwwang.com

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

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

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

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

  int multip=5;
  x=abs(vals[0])*multip; 
  y=abs(vals[1])*multip; 
  z=abs(vals[2]-255)*multip;
  background(x,y,z);
  println(x + " " + y + " " + z);
  slog();

  delay(500);
}

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");
  httpRequestDone=false;

  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
    //httpRequestDone=true;
    println(data);
  }
}

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





    	
    
		Live Background
		
		

		
		
		
	
	
		
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.


1,"exceptions"=>0));

$fields = "x,y,z";
$tables = "p_{$projectId}_{$tableName}";
$condition = "x>=0 || xgetData($email, $password, $fields, $tables, $condition, $sbid, $delta, $type);
$sbid=$sbid+1;
}
$sbid=$sbid-2;
$csv = $client->getData($email, $password, $fields, $tables, $condition, $sbid, $delta, $type);


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

echo $csv;
//return $csv;

?>