Processing Forum
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
Serial myPort;
String waarde = "0";
int test = int(waarde);
// this is the webpage containing the relevant data
String url = "http://iphone.bjair.info/m/beijing/mobile";
String path = "#number h1";
int refresh = 20; // sec
int walkingTime = 6; // min
void setup() {
size(200, 200);
background(0);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
noStroke();
smooth();
frameRate(1/(float)refresh);
draw();
}
void draw() {
// the data extraction is done in the function getTimeUntilNextBus()
Integer timeUntilBus = getTimeUntilNextBus();
if(timeUntilBus != null) {
//int diff = timeUntilBus - walkingTime;
println("Time until leave: "+timeUntilBus);
}
/*
if(diff > 10 || diff < 0)
background(255,0,0);
else if(diff > 5)
background(255,255,0);
else if(diff >= 0)
background(0,255,0);
} else {
background(0);
}*/
}
Integer getTimeUntilNextBus() {
// doc contains the result from the website request
Document doc;
// this is the actual request, including error handling, simply copy this
try {
doc = Jsoup.connect(url).get();
} catch(IOException e) { // Connection error
e.printStackTrace();
doc = null;
} catch(IllegalArgumentException e) {
println("Bad URL!");
doc = null;
}
if(doc != null) {
// this is the command that selects the data from the result expression
Element data = doc.select(path).first();
waarde = data.text();
println(waarde);
return 4;/*
// the rest here is only to get the data in a nice format, times is a list
if(times != null && times.size() > 0) {
// timeString contains then the first string of the list times
String timeString = times.first().text();
// this is to separate the number from min, eg if timeString is "10 min"
// splitResult will contain "10" then...
String[] splitResult = split(timeString, ' ');
if(splitResult.length > 0) {
return parseInt(splitResult[0]);
}
}*/
}
if(test > 100){
myPort.write(1);
}
else{
myPort.write(2);
}
return null;
}