/* datalogging client

 by Tom Igoe

revised 06Dec05 by mjh348 - doesn't work

 */

import processing.net.*; 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:
  println(Serial.list());

  // 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() {

  background(0);

// if there's any serial data available, get it:

  if (port.available() > 0) {
    serialEvent();
    // 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) {
    delay(300);
    port.write(65);
  }


  // 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) {
     println ("sending");
      sendToNet(sensorValue);
    }
  }


  // print the results of the net send:
  if (netSendInProgress) {
    if (client.available() > 0) {
      int inByte = client.read();
      print((char)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:
      delay(5000);
      // Send a capital A to request new sensor readings:
      port.write(65);
      }
    }
  }

}

void serialEvent() {

  // Add the latest byte from the serial port to array:
  serialInArray[serialCount] = port.read();
  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, "itp.nyu.edu", 80);

  //print the IP address of the host:
  println(client.ip());
  // send the HTTP GET request:
  client.write("GET /~mjh348/logger.php?tempValue=" + sensorValue + "

HTTP/1.1\n");

  client.write("HOST: itp.nyu.edu\n\n");
  netSendInProgress = true;

}

  • /

void sendToNet(int sensorValue) {

   // open a TCP socket to the host:
   client = new Client(this, "itp.nyu.edu", 80);

   //print the IP address of the host:
   println(client.ip());
   // send the HTTP GET request:
   String sensorString = Integer.toString(sensorValue);
   client.write("GET /~mjh348/sensor/sql_datalog_2.php?sensorValue=" +

sensorValue + " HTTP/1.1\n");

   client.write("HOST: itp.nyu.edu\n\n");
   netSendInProgress = true;

}

Back