Hello!
I need your help with a project I am working on. I have written a program for Processing which gets data from website and sends it via the serial port to Arduino. But when I run the program nothing happens, apparently Processing cannot send the data. Could you please help me with that problem? I want the data from the string path "#number h1", which is a number between 100 and 500. If the number is bigger than 100 (see if loop), processing should send the number "1" via serial port to Arduino. If the number is smaller than 100, it should send the number "2".
It would be great if somebody could help me with that problem :( I think that something is wrong with the last if loop.
With kind regards,
Laura
I need your help with a project I am working on. I have written a program for Processing which gets data from website and sends it via the serial port to Arduino. But when I run the program nothing happens, apparently Processing cannot send the data. Could you please help me with that problem? I want the data from the string path "#number h1", which is a number between 100 and 500. If the number is bigger than 100 (see if loop), processing should send the number "1" via serial port to Arduino. If the number is smaller than 100, it should send the number "2".
It would be great if somebody could help me with that problem :( I think that something is wrong with the last if loop.
With kind regards,
Laura
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;
}
1