Closed Hello1024 closed 5 years ago
Looks like this might be the answer:
asyncHTTPrequest request;
void setup() {
Serial.begin(9600);
}
void loop() {
// start request
request.open("GET", "http://worldtimeapi.org/api/timezone/Europe/London.txt");
request.send();
// Print dots while request in progress
while (request.readyState() != 4) {
delay(100);
Serial.print('.');
}
// Check there was no error and print result
if (request.responseHTTPcode() == 200)
Serial.println(request.responseText());
delay(5000);
}
Note that the above is rather complex - it would be nice to have a 'simple' static API which looks like this:
void setup() {
Serial.begin(9600);
}
void loop() {
auto req = asyncHTTPrequest.start("http://worldtimeapi.org/api/timezone/Europe/London.txt");
// do stuff while waiting
while (!req.done) delay(100);
if (req.success) Serial.println(req.responseText());
}
I've added a simple example that makes requests to the worldtimeapi above. However the example doesn't sit in a wait loop waiting for completion. That would defeat the purpose. The most recent comment above suggests changing the actual methods of the class. That is doable, and I recommend that you fork the repo and make a new class for that. I made this class to use in one of my other repositories "IoTaWatt" because there was no good way to have an async client and I desperately needed one. It fulfills my needs. Also, take a look at the subsidiary xbuf, which probably should be in it's own repo as it has come in handy for all manner of buffering and manipulation. I would entertain any PRs to improve this class, but the risk would have to be low and it would need to be well tested as IoTaWatt is in use in thousands of sites worldwide.
I would imagine that most users of this library want to make a non-blocking HTTP GET request and get back the response as a string. I'm sure thats possible... But with the current documentation it would require quite some digging.