Hardware Reference
In-Depth Information
8
The loop() contains all the logic
laid out in Figure 4-11. If the client
is connected to the server, it makes
an HTTP GET request. If it's made a
request, it uses TextFinder to search
the response for the air-quality string
and then sets the meter. If the client
isn't connected to the server, it waits
another two minutes until the request
interval's passed, and tries to connect
again.
void loop()
{
// if you're connected, save any incoming bytes
// to the input string:
if (client.connected()) {
if (!requested) {
requested = makeRequest();
}
else {
// make an instance of TextFinder to search the response:
TextFinder response(client);
// see if the response from the server contains the AQI value:
if(response.find("Air Quality:")) {
// convert the remaining part into an integer:
airQuality = response.getValue();
// set the meter:
meterIsSet = setMeter(airQuality);
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
client.stop();
connectToServer();
}
// set the status LEDs:
setLeds();
}
8
Both setup() and loop() attempt
to connect to the server using the
connectToServer() method. If it gets
a connection, it resets the requested
variable so the main loop knows it can
make a request.
void connectToServer() {
// clear the state of the meter:
meterIsSet = false;
// attempt to connect, and wait a millisecond:
Serial.println("connecting...");
if (client.connect(server, 80)) {
requested = false;
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
 
Search WWH ::




Custom Search