Anyway.
The script in Processing at the moment does this:
import hypermedia.net.*;
UDP udp; // define the UDP object
String feed = "http://news.google.com/....";
int interval = 5; // this variable will be used to retrieve content;
int lastTime; // var to be used as the last time we fetched the content
// the 4 main categories of words to be found
int categ4 = 0; //word4
int categ3 = 0; //word3
int categ2 = 0; //word2
int categ1 = 0; //word1
// variables that represent the four above mentioned categories
//Serial port;
int c1; // preparing category 1 to be sent
int c2; // preparing category 2 to be sent
int c3; // preparing category 3 to be sent
int c4; // preparing category 3 to be sent
String buffer = ""; // This accumulates characters coming from Arduino
PFont font; // This visualisation needs a font to display the informations collected
void setup() {
size(1200, 480);
frameRate(10); // regulates the speed at which the code is played
font = loadFont("Akkurat-12.vlw");
fill(60);
textFont(font, 12);
noStroke();
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
//udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message
lastTime = 1000;
fetchData();
}
void draw() {
background(255);
int n = (interval - ((millis()-lastTime)/500)); // timer
c1 = categ1; // Prepares the values for each category to be sent to Arduino
c2 = categ2;
c3 = categ3;
c4 = categ4;
// println(c1);
text("Data reading from CSS", 55, 40); // diagram of the categories
text("Reading feed:", 55, 60);
text(feed, 55, 80);
text("Word 1", 55, 400);
text(categ1, 55, 420);
rect(55, 380, 150, categ1*(-2.5));
text("Word 2", 245, 400);
text(categ2, 245, 420);
rect(245, 380, 150, categ2*(-2.5));
text("Word 3", 435, 400);
text(categ3, 435, 420);
rect(435, 380, 150, categ3* (-2.5));
text("Word 4", 625, 400);
text(categ4, 625, 420);
rect(625, 380, 150, categ4* (-2.5));
if (n <= 0) { // It sends data to Arduino every 1 second
fetchData();
lastTime = millis();
String ip = "192.168.1.167"; // the remote IP address
int port = 8888; // the destination port
String cone="P"+categ1;
println(cone);
udp.send(cone, ip, port ); // the message to send
myDelay(250);
String ctwo="Q"+c2;
println(ctwo);
udp.send(ctwo, ip, port ); // the message to send
myDelay(250);
String cthree="R"+c3;
println(cthree);
udp.send(cthree, ip, port ); // the message to send
myDelay(250);
String cfour="S"+categ4;
println(cfour);
udp.send(cfour, ip, port ); // the message to send
myDelay(250);
if (port.available() > 0) { // this checks if there is data waiting
int inByte = port.read(); // then it reads one byte
if (inByte != 10) { // if byte is not newline
buffer = buffer + char(inByte); // it adds it to the buffer
}
else {
// when a newline reached, it processes the data
if (buffer.length() > 1) { // it makes sure there is enough data
// this chops off the last character
buffer = buffer.substring(0, buffer.length() -1);
//clean the buffer for the next read cycle
buffer = "";
// it refreshes the port
port.clear();
}
}
}
}
void fetchData() {
// the code uses these strings to parse the feed
String data;
String chunk;
// it zeroes the counters
categ3 = 0;
categ3 = 0;
categ2 = 0;
categ1 = 0;
try {
URL url = new URL(feed); // An object that represents the URL
URL url2 = new URL(feed2); // Another one for anothert website t connect to
// it prepares a connection
URLConnection conn = url.openConnection();
conn.connect(); // it connects to google website
// it connects the data coming from the connection to a buffered
// reader that reads the data one line at a time.
URLConnection conn2 = url2.openConnection();
conn2.connect();
BufferedReader in = new
BufferedReader(new InputStreamReader(conn.getInputStream()));
BufferedReader in2 = new
BufferedReader(new InputStreamReader(conn2.getInputStream()));
// it reads each line from the feed
while ( (data = in.readLine ()) != null) {
StringTokenizer st =
new StringTokenizer(data, "\"<>,.()[] ");// it breaks it down
while (st.hasMoreTokens ()) {
// each chunk of data is made lowercase
chunk= st.nextToken().toLowerCase() ;
// and then the words every time they are found they add 1 to the category
if (chunk.indexOf("word4") >= 0)
categ4++; // increment categ4 by 1
if (chunk.indexOf("word3) >= 0)
categ3++; // increment categ3 by 1
if (chunk.indexOf("word2) >= 0)
categ2++; // increment categ2 by 1
if (chunk.indexOf("word1) >= 0)
categ1++; // increment categ1 by 1
// It sets 100 to be the maximum number of references considered
if (categ4 > 99) categ4 = 99;
if (categ3 > 99) categ3 = 99;
if (categ2 > 99) categ2 = 99;
if (categ1 > 99) categ1 = 99;
}
catch (Exception ex) { // If there was an error, it stops the sketch
ex.printStackTrace();
System.out.println("ERROR: "+ex.getMessage());
}
}
void myDelay(int ms) //This a delay which will used after each category (c1, c2, c3, c4, c5)
// is sent to Arduino
{
try
{
Thread.sleep(ms);
}
catch(Exception e){}
}
void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}