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.
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.
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.
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.1nHOST:yoursite.comnn");
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
<html>
<head>
<style type="text/css">
</style>
<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()
{
makeHttpRequest('getSlog.php',ajax_return);
}
// A function that gets called when ajax returns
function ajax_return(response)
{
document.getElementById('m1').style.backgroundColor='RGB('+response+')';
document.getElementById("m1").innerHTML = response;
call_ajax();
}
// Setup AJAX function, creates a timeout so that we run something periodically
function setup_ajax()
{
call_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);
}
}
addOnLoad();
</script>
</head>
<body>
<small><div id="m1" style="height:35px; width:100%;">
checking sensorbase.org...
</div></small>
</body>
</html>
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.
<?php
//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';
$csv=1;
//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));
$sbid=(int)$sCount;
fclose($cfr);
while ($csv!=null)
{
$csv = $client->getData($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;
?>
- Published:
- 10.02.08 / 2am
- Category:
- ITP, Live Web, Web, Work in Progress
- Related: [HND: Live GPS][Laser Tether : Sketch][Tilt SCREAM Pong]











No comments
Jump to comment form | comments rss [?] | trackback uri [?]