Post to Twitter?

You'd need to change the PHP side of things to output the contents of the file. Then you'd need to change the Processing side of things to read in the output from the PHP.

Following on from the earlier example, here's a revised PHP script:

<?php file_put_contents("tick.txt", file_get_contents("tick.txt") + 1); file_get_contents("tick.txt") ; ?>

This increments the contents of the file then it outputs the contents of the file.

And here's a revised Processing script:

import java.net.;
import processing.serial.
;

Serial theSerial;

void setup() {
theSerial = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
if (theSerial.available() > 0) {
print("S: "); println(theSerial.readString());
try {
URL theURL = new URL("tick.php");
InputStream theInputStream = theURL.openStream();
InputStreamReader theInputStreamReader = new InputStreamReader(theInputStream);
BufferedReader theBufferedReader = new BufferedReader(theInputStreamReader);
String theString = theBufferedReader.readLine();
print("I: "); println(theString);
}
catch (Exception theException) {
theException.printStackTrace();
}
}
}

Two things have changed here.

  1. I've added in a little debugging - the sketch will output any data it receives (the two println statements) plus it'll tell you whether they arrive over the serial line (print("S: ")) or over the internet (print("I: ")).

  2. I've changed the original script such that it not only opens the tick.php script but it also reads the output. The combination of Java objects that you need to use may seem a little OTT but that's Java IO for you.

If you wanted to change this so that your Processing sketch does something with the PHP output rather than just printing it to the screen you've got it stored in the local variable called theString and can play with it at will.