modified from Tom's Processing-to-Perl example code by Keunyoung Oh

This Processing code reads data sent by a microcontroller attached to a SHT11 Temperature and humidity sensor and sends the result to a perl script. Basic Stamp code can be found here.

//Tom's code, little change to keep update to perl.


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
int count;
int oldVar = 0;
int threshold= 20;  // 2% difference

void setup()
{
  size(200, 200);
  // Print a list of the serial ports, for debugging purposes:
  //println(Serial.list());

  port = new Serial(this, Serial.list()[2], 9600);
  port.write(65);    // Send a capital A to start the microcontroller sending
}

void draw()
{
  background(0);
  framerate(10);
  // 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) {
   // println("sensorValue: "+sensorValue);
    // 3 sec interval.
    //if((count > 20) && (abs(sensorValue - oldVar) >  threshold)){
    if(abs(sensorValue - oldVar) >  threshold){
      sendToNet(sensorValue);
      println("sendToNet");
       oldVar = sensorValue;
       //println("oldVar:"+oldVar);
      count = 0;
    } else{


      count++;
    }
  }

  // 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(1000);
        // 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];
    // println("serialInArray[1]:"+serialInArray[1]+"      serialInArray[0]"+serialInArray[0]);
    newSensorData = true;
    //println("sensorValue:"+sensorValue);
    // 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:
  String sensorString = Integer.toString(sensorValue);
  client.write("GET /~[yourID}/cgi-bin/pic_to_sql.pl?sensorVar=" + sensorValue + " HTTP/1.1\n");
  // client.write("GET /~[yourID]/picTosql.php?action=insert&sensorValue=" + sensorValue + " HTTP/1.1\n");
  client.write("HOST: itp.nyu.edu\n\n");
  // println("sensorValue:"+sensorValue);
  netSendInProgress = true;
  //delay(3000);
}