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

		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.


$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;