Wednesday, March 01, 2006

Datalogging, Database, Server...

<--Using Pot

Link to Network Datalogging Suite in Tom's page

To link Mac and Serial: Use USB to Serial Converter (required installing driver)

My pic send values to the computer and I checked from Hyper terminal. The range was from 1 to 1021. But the processing didn't give me the value, instead it said,

Devel Library=========================================
Native lib Version = RXTX-2.1-7pre17
Java lib Version = RXTX-2.1-7pre17
HTTP/1.1 200
Date: Tue, 07 Mar 2006 04:16:53
GMTServer: Apache/2.0.52 (Red Hat)
Content-Length: 42
Connection: close
Content-Type: text/html;
The file datalog.txt is not write-enabled.

Sending values from Pic to Computer, Computer to Server is the goal of my assignment. Saving space for data is needed. Ex) Range of 1024 per Sec for an hour is 60 times 1024... We are going to use this to analysis the data later.

Processing Code (Red text parts need to be changed for each server address)

/* datalogging client by Tom Igoe Communicates between a microcontroller reading a 10-bit analog sensor and a CGI script that timestamps the reading and writes it to a file. The program starts by sending to the sensor for an initial value. When a good sensor value is obtained, the program checks to see if there's a net send in progress. If there's not, it sends the sensor reading to the net by starting a network client that connects to a server on port 80, sends an HTTP 1.1 GET request, and prints the results. Once the sensor request is done, the client waits 3 seconds before the next send so as not to overwhelm the server. created 18 March 2005 updated 24 Oct. 2005 */

import processing.serial.*;

Serial port; // The serial port
Client client; // the net client
int[] serialInArray = new int[2]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int sensorValue = 0; // value of the sensor
boolean firstContact = false; // whether we've heard from the microcontroller
boolean netSendInProgress = false; // whether or not the last net request is finished
boolean newSensorData = false; // whether or not we have new sensor data

void setup()
size(200, 200);
// Print a list of the serial ports, for debugging purposes:
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
port = new Serial(this, Serial.list()[0], 9600);
port.write(65); // Send a capital A to start the microcontroller sending

void draw()
// if there's any serial data available, get it:
if (port.available() > 0) {
// Note that we heard from the microntroller at least once:
firstContact = true;
// If there's no serial data, send again until we get some incoming data.
// (in case you tend to start Processing before you start your
// external device):
if (firstContact == false) {
// if we have new sensor data, check to see that there's no open
// net connections. If there aren't, send the data.
if (newSensorData) {
if (!netSendInProgress) {

// print the results of the net send:
if (netSendInProgress) {
if (client.available() > 0) {
int inByte =;
// when we get a byte of value 0, it's the end of the response
// from the server. Stop listening and get some more data:
if (inByte == 0) {
netSendInProgress = false;
// don't overwhelm the server:
// Send a capital A to request new sensor readings:

void serialEvent()
// Add the latest byte from the serial port to array:
serialInArray[serialCount] =;
// If we have 2 bytes, combine them into one value:
if (serialCount > 1 ) {
sensorValue = serialInArray[1] * 256 + serialInArray[0];
newSensorData = true;

// Reset serialCount:
serialCount = 0;
else {
// if we have only one byte, don't let the main loop
// send out yet:
newSensorData = false;

void sendToNet(int sensorValue) {
// open a TCP socket to the host:
client = new Client(this, "", 80);

//print the IP address of the host:
// send the HTTP GET request:
client.write("GET /~sh1319/logger.php?tempValue=" + sensorValue + " HTTP/1.1\n"); client.write("HOST:\n\n");

netSendInProgress = true;}

Pic Code
I changed the code a little bit for my clock, blinky LED on portd.1 and Lowbyte, Highbyte parts.

define osc 4

' serial RX is on pin RC7
' serial TX is on pin RC6

' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
' constant to set the baud rate:
inv9600 con 16468

' define variables:
adcVar var word
inByte var byte

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result


high portd.1
pause 500
low portd.1
pause 500

' read sensors, convert to bytes:
adcin 0, adcVar

' read serial data in:
'serin2 portc.7, inv9600, [inByte]

' if you got the message from the remote device, send out data:
serout2 portc.6, inv9600, [ dec adcVar, 13, 10]

goto main

CGI program, written in PHP, tested using an Apache web server running on a Redhat Linux machine:

$filename = 'datalog.txt';
// compose the data string from the date and the incoming value:
$dataString = date("Y-m-d h:i:s\t");
$dataString = $dataString.$_REQUEST['tempValue'];
// add a linefeed and carriage return
$dataString = $dataString."\r\n";

// make sure the file exists and is writable first:
if (is_writable($filename)) {

// Open $filename in append mode. anything you add
// will be appended to the end of the file:
if (!$handle = fopen($filename, 'a')) {
echo "Can't open file $filename";

// Write $dataString to the opened file.
if (fwrite($handle, $dataString) == FALSE) {
echo "Can't write to file $filename";
// data successfully written:
echo "Wrote $dataString to file $filename";
// send a 0 to tell the remote side we're done:
echo "\0";
// close the file

} else {
echo "The file $filename is not write-enabled.";

text_datalog.php : Save this file with this name in my server public folder with 'datalog.txt'. 'datalog.txt' file can be started blank.

How to Use Telnet

sol-huhs-powerbook-g4-12:~ sol$ pwd
sol-huhs-powerbook-g4-12:~ sol$ ls
Desktop Music Sites
Documents Pictures Untitled.txt
Library Public careers.html
Movies Send Registration fish2.html

sol-huhs-powerbook-g4-12:~ sol$ ls /dev/tty.*
/dev/tty.Bluetooth-Modem /dev/tty.modem
sol-huhs-powerbook-g4-12:~ sol$ screen /dev/tty.KeySerial1.9600

Than, I'm supposed to type in,

telnet 80
get /~sh1319/index.html HTTP/1.1

I get the blank screen that will show the data from the Pic.

If I want to quit the telnet,
Ctrl + a, Ctrl + /, than Y.


Post a Comment

<< Home