Networked Cat - image upload and mailer not being triggered by serial events

edited October 2015 in Arduino

I have a php server running on the root of this structure:








this Arduino-based app should:

1) receive an input from a pressure sensor; 2) verify whether threshold is surpassed 3) take picture from build-in camera 4) upload pic to website 5) mail me about the upload

testing on browser, save2web.php and cat-script.php are working, that is, scripts are uploading and emailing.

pressure sensor is also reading and printing, and threshold have been calibrated.

but NetworkedCat.pde is NOT triggering the events.

please note:

Processing opens localhost at another port (80), because php server works on 8080.

are they compatible?

why is the Processing code below not woking?

if I shorten the Processing code, and test the image capture and upload only, it works. so, the bug must be related to the serial events.

please help!

/*Serial String reader
Context: Arduino

Reads in a string of characters until it gets a linefeed (ASCII 10).
then converts the string into a number

import processing.serial.*;

Serial myPort;              //the serial port
float sensorValue = 0;      //the value form the sensor
float prevSensorValue = 0;  //previous value from the sensor
int threshold = 200;        //above this number, the cat is on the mat

int currentTime = 0;       //the current time as a single number
int lastMailTime = 0;      //last minute you sent a mail
int mailInterval = 60;     //minimum seconds betweeen mails
String mailUrl = "cat-script.php";
int lastPicture = 0;       //last minute you sent a picture
int lastPictureTime = 0;   //last minute you sent a picture
int pictureInterval = 10;  //minimum seconds between pictures

Capture myCam;            //camera capture library instance
String fileName = "img.jpg"; //file name for the picture

//location on your server for the picture script:
String pictureScriptUrl = "save2web.php";
String boundary = "----H4rkNrF"; //string boundary for the post request

Client thisClient;        //instance for the net library

//float xPos = 0;             //horizontal position of the graph
//float lastXPos = 0;         //previous horizontal position  

void setup(){
  size(400, 300);
  //list all the available serial ports

  myPort = new Serial(this, Serial.list()[7], 9600);

  //reads bytes into a buffer until you get a newline (ASCII 10);

  //set initial background and smooth drawing:
  //for a list of cameras on your computer, use this line:

  //use the default camera for capture at 30 fps
  myCam = new Capture(this, width, height, 30);

void draw(){
  //make a single number fmor the current hour, minute, and second
  currentTime = hour() * 3600 + minute() * 60 + second();

  if (myCam.available() == true) {
    //draw the camera image to the screen;;
    set(0, 0, myCam);

    //get the time as a string
    String timeStamp = nf(hour(), 2) + ":" + nf(minute(), 2)
    + ":" + nf(second(), 2) + "   " + nf(day(), 2) + "-"
    + nf(month(), 2) + "-" + nf(year(), 4);

    //draw a dropshadow for the time text:
    text(timeStamp, 11, height - 19);
    //draw the main time next
    text(timeStamp, 10, height - 20);

void serialEvent (Serial myPort){
  //get the ASCII string
  String inString = myPort.readStringUntil('\n');

  if (inString != null){
    //trim off any whitespace:
    inString = trim(inString);
    //convert to an int and map to the screen height
    sensorValue = float(inString);
    sensorValue = map(sensorValue, 0, 1023, 0, height);

    if (sensorValue > threshold){
      if(currentTime - lastPictureTime > pictureInterval){
        PImage thisFrame = get();;
        lastPictureTime = currentTime;
      //if the last reading was less than the threshold
      //then the cat just got on the mat
      if(prevSensorValue <= threshold){
        println("Peti na casinha");
      //if the sensor value is less than the threshold,
      //and the previous value was greater, then the cat
      //just left the mat
      if (prevSensorValue > threshold){
        println("Peti não está na casinha");
    //save the current value for the next time
    prevSensorValue = sensorValue;

void sendMail(){
  //how long has passed since the last mail
  int timeDifference = currentTime - lastMailTime;

  if( timeDifference > mailInterval){
    String[] mailScript = loadStrings(mailUrl);
    println("results from mail script:");

    //save the current minute for next time
    lastMailTime = currentTime;

void postPicture(){
  //load the saved image into an array of bytes
  byte[] thisFile=loadBytes(fileName);

  //open a new connection to the server
  thisClient = new Client(this, "localhost", 80);
  //make an HTTP POST request:
  thisClient.write("POST" + pictureScriptUrl + " HTTP/1.1\n");
  thisClient.write("Host: localhost\n");
  //tell the server you're sending the POST in multiple parts
  //and send a unique string that will delineate the parts
  thisClient.write("Content-Type: multipart/form-data; boundary=");
  thisClient.write(boundary + "\n");

  //form the beginning of the request
  String requestHead ="\n--" + boundary + "\n";
  requestHead +="Content-Disposition: form-data; name=\"file\"; ";
  requestHead += "filename=\"" + fileName + "\"\n";
  requestHead +="Content-Type: image/jpeg\n\n";

  //form the end of the request
  String tail="\n\n--" + boundary + "--\n\n";

  //calculate and send the length of the total request
  //including the head of the request, the file, and the tail
  int contentLength = requestHead.length() + thisFile.length + tail.length();
  thisClient.write("Content-Length: " + contentLength + "\n\n");

  //send the header of the request, the file and the tail
Sign In or Register to comment.