import simpleML.*;
// A Request object, from the library
HTMLRequest htmlRequest;
int startTime; // for the timer to make request ever N seconds
String html = ""; // String to hold data from request
int counter = 0; // Counter to animate rectangle across window
int back = 255; // Background brightness
void setup() {
size(200,200);
// Create and make an asynchronous request
htmlRequest = new HTMLRequest(this,"http://cuartielles.com/david/h/1.php");
// <div id='$count'><div id='key-$count'>f</div><div id='val-$count'>45</div></div>
htmlRequest.makeRequest();
startTime = millis();
background(0);
}
void draw() {
// Fill background
background(back);
// Every 50 seconds, make a new request
int now = millis();
if (now - startTime > 50000) {
htmlRequest.makeRequest();
println("Making request!");
startTime = now;
}
// Draw some lines with colors based on characters from data retrieved
for (int i = 0; i < width; i++) {
if (i < html.length()) {
int c = html.charAt(i);
stroke(c,150);
line(i,0,i,height);
}
}
// Animate rectangle and dim rectangle
fill(255);
noStroke();
rect(counter,0,10,height);
counter = (counter + 1) % width;
back = constrain(back - 1,0,255);
}
// When a request is finished
void netEvent(HTMLRequest ml) {
html = ml.readRawSource(); // Read the raw data
back = 255; // Reset background
println(html);
println("Request completed!"); // Print message
}