Hardware Reference
In-Depth Information
Continued from previous page.
else {
Serial.print(F("failed to connect"));
return false;
}
}
The readResponse() method reads
incoming bytes from the server
after the HTTP request, and looks
for the beginning and end of a tweet.
The tweet is conveniently sandwiched
between two tags: <tweet> and </
tweet> . So, all you have to do is look for
those and return what's between them.
You can ignore the rest of the stream.
If you want to see the whole XML feed,
open the URL the sketch is using in a
browser, and give it a Twitter handle at
the end, like this:
8
int readResponse() {
char tweetBuffer[141]; // 140 chars + 1 extra
int result = 0;
// if there are bytes available from the server:
while (client.connected()) {
if (client.available()) {
// make an instance of TextFinder to search the response:
TextFinder response(client);
// see if the response from the server contains <text>:
response.getString("<text>", "</text>", tweetBuffer, 141);
// print the tweet string:
Serial.println(tweetBuffer);
// make a String with padding on both ends:
tweet = " " + String(tweetBuffer)
+ " ";
result = tweet.length();
// you only care about the tweet:
client.stop();
}
}
return result;
}
http://api.twitter.com/1/statuses/
user_timeline.xml?screen_
name=halfpintingalls
Troubleshooting
There are a lot of things that can go wrong in this sketch,
so make sure you're clear on each of the parts, and have a
plan for troubleshooting. Below are a few things to look for:
Can it reach the server? If not, try the HTTP test client in
Chapter 4 with the URL that this sketch calls.
Can it read the user's tweets? Are you sure you didn't
pick a Twitter user who protects her tweets? Or who hasn't
tweeted yet? Put the URL in a browser and search for the
<tweet> tags yourself. If they're not there, the sketch can't
see them.
Is it reaching each of the four major states? If not,
determine at which one it's stopping. Have the sketch print
a message when it reaches each new state, as well as the
results of the previous state.
Is the LCD working? Separate this out using the Serial
Monitor. This sketch contains a number of print state-
ments to let you know what's happening. Use them when
troubleshooting.
Is it reading a tag? Can it read the tag's memory
blocks? If not, try one of the example sketches that
comes with the SonMicroReader library. In particular, the
ReadBlock example was written to do a basic read of tags.
 
Search WWH ::




Custom Search