
136 MAKING THINGS TALK
void lookForData() {
// wait for bytes from server:
if (Serial.available()) {
inByte = Serial.read();
// If you get a "<", what follows is the air quality index.
// You need to read what follows the <.
if (inByte == '<') {
stringPos = 0;
status = reading;
}
}
}
The stuff you see at the top is the
HTTP header. When you call the PHP
script from a browser, you don’t see
all of the header, because the browser
strips it out for you. The lookForData()
method reads strip the header out by
ignoring all the bytes before the < sign:
8
void readData() {
if (Serial.available()) {
inByte = Serial.read();
// Keep reading until you get a ">":
if (inByte != '>') {
// save only ASCII numeric characters (ASCII 0 - 9):
if ((inByte >= '0') && (inByte <= '9')){
inString[stringPos] = inByte;
stringPos++;
}
}
// if you get a ">", you've reached the end of the AQI reading:
else {
interpretResults();
}
}
}
After you get the < sign, the
readData() method takes only the
numeric characters from the remaining
string and saves them to the inString[]
array. When it gets the > symbol indi-
cating the end of the string, it calls the
interpretResults() method:
8
void interpretResults() {
// convert the string to a numeric value:
int airQuality = atoi(inString);
// set the meter appropriately:
setMeter(ai