root / arduino-1.0 / libraries / Ethernet / examples / TwitterClient / TwitterClient.ino @ 58d82c77
History | View | Annotate | Download (3.52 KB)
1 |
/* |
---|---|
2 |
Twitter Client with Strings |
3 |
|
4 |
This sketch connects to Twitter using an Ethernet shield. It parses the XML |
5 |
returned, and looks for <text>this is a tweet</text> |
6 |
|
7 |
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield, |
8 |
either one will work, as long as it's got a Wiznet Ethernet module on board. |
9 |
|
10 |
This example uses the DHCP routines in the Ethernet library which is part of the |
11 |
Arduino core from version 1.0 beta 1 |
12 |
|
13 |
This example uses the String library, which is part of the Arduino core from |
14 |
version 0019. |
15 |
|
16 |
Circuit: |
17 |
* Ethernet shield attached to pins 10, 11, 12, 13 |
18 |
|
19 |
created 21 May 2011 |
20 |
by Tom Igoe |
21 |
|
22 |
This code is in the public domain. |
23 |
|
24 |
*/ |
25 |
#include <SPI.h> |
26 |
#include <Ethernet.h> |
27 |
|
28 |
|
29 |
// Enter a MAC address and IP address for your controller below. |
30 |
// The IP address will be dependent on your local network: |
31 |
byte mac[] = { |
32 |
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; |
33 |
IPAddress ip(192,168,1,20); |
34 |
|
35 |
// initialize the library instance: |
36 |
EthernetClient client; |
37 |
|
38 |
const int requestInterval = 60000; // delay between requests |
39 |
|
40 |
char serverName[] = "api.twitter.com"; // twitter URL |
41 |
|
42 |
boolean requested; // whether you've made a request since connecting |
43 |
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds |
44 |
|
45 |
String currentLine = ""; // string to hold the text from server |
46 |
String tweet = ""; // string to hold the tweet |
47 |
boolean readingTweet = false; // if you're currently reading the tweet |
48 |
|
49 |
void setup() { |
50 |
// reserve space for the strings: |
51 |
currentLine.reserve(256); |
52 |
tweet.reserve(150); |
53 |
|
54 |
// initialize serial: |
55 |
Serial.begin(9600); |
56 |
// attempt a DHCP connection: |
57 |
if (!Ethernet.begin(mac)) { |
58 |
// if DHCP fails, start with a hard-coded address: |
59 |
Ethernet.begin(mac, ip); |
60 |
} |
61 |
// connect to Twitter: |
62 |
connectToServer(); |
63 |
} |
64 |
|
65 |
|
66 |
|
67 |
void loop() |
68 |
{ |
69 |
if (client.connected()) { |
70 |
if (client.available()) { |
71 |
// read incoming bytes: |
72 |
char inChar = client.read(); |
73 |
|
74 |
// add incoming byte to end of line: |
75 |
currentLine += inChar; |
76 |
|
77 |
// if you get a newline, clear the line: |
78 |
if (inChar == '\n') { |
79 |
currentLine = ""; |
80 |
} |
81 |
// if the current line ends with <text>, it will |
82 |
// be followed by the tweet: |
83 |
if ( currentLine.endsWith("<text>")) { |
84 |
// tweet is beginning. Clear the tweet string: |
85 |
readingTweet = true; |
86 |
tweet = ""; |
87 |
} |
88 |
// if you're currently reading the bytes of a tweet, |
89 |
// add them to the tweet String: |
90 |
if (readingTweet) { |
91 |
if (inChar != '<') { |
92 |
tweet += inChar; |
93 |
} |
94 |
else { |
95 |
// if you got a "<" character, |
96 |
// you've reached the end of the tweet: |
97 |
readingTweet = false; |
98 |
Serial.println(tweet); |
99 |
// close the connection to the server: |
100 |
client.stop(); |
101 |
} |
102 |
} |
103 |
} |
104 |
} |
105 |
else if (millis() - lastAttemptTime > requestInterval) { |
106 |
// if you're not connected, and two minutes have passed since |
107 |
// your last connection, then attempt to connect again: |
108 |
connectToServer(); |
109 |
} |
110 |
} |
111 |
|
112 |
void connectToServer() { |
113 |
// attempt to connect, and wait a millisecond: |
114 |
Serial.println("connecting to server..."); |
115 |
if (client.connect(serverName, 80)) { |
116 |
Serial.println("making HTTP request..."); |
117 |
// make HTTP GET request to twitter: |
118 |
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1"); |
119 |
client.println("HOST: api.twitter.com"); |
120 |
client.println(); |
121 |
} |
122 |
// note the time of this connect attempt: |
123 |
lastAttemptTime = millis(); |
124 |
} |