Potentiometer —> Arduino —> Processing —> PHP
Arduino Code
#define BAUD_RATE 9600
#define SENSOR 0
int dataInBytes = 0;
int incomingData = 0;
void setup()
{
Serial.begin( BAUD_RATE );
}
void loop()
{
if( Serial.available() > 0 )
{
// ASCII "A" is the trigger
if( (incomingData = Serial.read()) == 65 )
{
// Send the LSB, followed by the MSB
Serial.print( (dataInBytes = analogRead( SENSOR )), BYTE );
Serial.print( (dataInBytes >> 8), BYTE );
}
}
}
Processing Code
import processing.net.*;
import processing.serial.*;
Serial port;
Client client;
int[] serialInArray = new int[ 2 ];
int serialCount = 0;
int sensorValue = 0;
boolean firstContact = false;
boolean netSendInProgress = false;
boolean newSensorData = false;
/* ====={ Setup }==============================================
================================================================ */
void setup()
{
size( 200, 200 );
println(Serial.list());
port = new Serial( this, Serial.list()[ 0 ], 9600 );
port.write( 65 );
}
/* ====={ Draw }==============================================
================================================================ */
void draw()
{
background(0);
if( port.available() > 0 )
{
serialEvent();
firstContact = true;
}
if( firstContact == false )
{
delay( 300 );
port.write( 65 );
}
if( newSensorData )
{
if( !netSendInProgress )
sendToNet(sensorValue);
}
if( netSendInProgress )
{
if( client.available() > 0 )
{
int inByte = client.read();
print((char)inByte);
if( inByte == 0 )
{
netSendInProgress = false;
delay( 3000 );
port.write(65);
}
}
}
}
/* ====={ Serial Event }==============================================
================================================================ */
void serialEvent()
{
serialInArray[serialCount] = port.read();
serialCount++;
if( serialCount > 1 )
{
sensorValue = serialInArray[1] * 256 + serialInArray[0];
newSensorData = true;
serialCount = 0;
}
else
newSensorData = false;
}
/* ====={ Send to Net }===============================================
================================================================ */
void sendToNet( int sensorValue )
{
client = new Client(this, "itp.nyu.edu", 80);
println();
println();
println(client.ip());
client.write( "GET /~rav256/sensor_workshop/logger.php?tempValue="
+ sensorValue
+ " HTTP/1.1\n" );
client.write( "HOST: itp.nyu.edu\n\n" );
netSendInProgress = true;
}
PHP
<?php
$filename = 'datalog.txt';
$dataString = date("Y-m-d h:i:s\t");
$dataString = $dataString.$_REQUEST['tempValue'];
$dataString = $dataString."\r\n";
if( is_writable( $filename ) )
{
if( !$handle = fopen( $filename, 'a' ) )
{
echo "Can't open file $filename";
exit;
}
if( fwrite($handle, $dataString) == FALSE )
{
echo "Can't write to file $filename";
exit;
}
echo "Wrote $dataString to file $filename";
echo "\0";
fclose($handle);
}
else
{
echo "The file $filename is not write-enabled.";
}
end;
?>