Thank you for the answers!
After the WiFly initialization on the
setup() function like on the library examples, the
loop() contains this quite simple snippet to make a new request every 30 seconds:
void loop() {
time = millis();
....
....
if ((time - prevTime) > 30000) { // update every 30 seconds
updateURL();
prevTime = time;
}
....
....
}
And here is the
updateURL() function that makes the request:
boolean updateURL() {
if (wifly.open("www.theurl.com", 80)) {
Serial.println("Connected, sending request...");
/* Send the request */
wifly.println(F("GET /file.php HTTP/1.1"));
wifly.println(F("Host: www.theurl.com"));
// wifly.println(F("Connection: close"));
wifly.println();
Serial.println("Waiting results...");
return true;
}
else {
Serial.println("Connection failed");
return false;
}
}
In this way I am sending the HTTP request (is there a faster/lighter method?) to read a page on the server.
All other instructions on the loop rely on the
time variable, so everything is non-blocking.
On the
loop() I am also reading back from the buffer of the WiFly serial connection one char per loop (so this is non-blocking too), using some
"if" statements to skip the HTTP headers and find the real value: this is not very elegant, but works just fine without complex string parsing etc: I am finding the sequence
{"pattern": " to skip all other content. :-D
if (wifly.available()) {
reading = wifly.read();
if ((reading == '{') and (count == 0)) {
count ++;
} else if ((reading == '"') and (count == 1))
count ++;
} else if ((reading == 'p') and (count == 2))
count ++;
} else if ((reading == 'a') and (count == 3))
count ++;
} else if ((reading == 't') and (count == 4))
count ++;
} else if ((reading == 't') and (count == 5))
count ++;
} else if ((reading == 'e') and (count == 6))
count ++;
} else if ((reading == 'r') and (count == 7))
count ++;
} else if ((reading == 'n') and (count == 8))
count ++;
} else if ((reading == '"') and (count == 9)) {
count ++;
} else if ((reading == ':') and (count == 10)) {
count ++;
} else if ((reading == ' ') and (count == 11)) {
count ++;
} else if ((reading == '"') and (count == 12)) {
count ++;
} else {
count = 0;
}
if (count == 13) {
pattern = wifly.read();
Serial.print("pattern: ");
Serial.println(pattern);
}
}
I agree, probably there is no way to avoid that delay, but maybe someone more expert than me can suggest a better approach...