Repository navigation
Expand file tree
/
Copy pathTwitterBox.ino
More file actions
241 lines (207 loc) · 7.03 KB
/
Copy pathTwitterBox.ino
File metadata and controls
241 lines (207 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <Ethernet.h>
#include <SPI.h>
#include <TextFinder.h>
#include <LiquidCrystal.h>
#include <avr/wdt.h>
boolean DEBUG = false;
int LIGHT_PIN = 2;
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x16 };
EthernetClient client;
TextFinder finder( client );
int loopCount;
int tweetCount;
String last_tweet;
String last_author;
long last_timestamp;
void setup() {
Serial.begin(9600);
delay(100);
// Watchdog timer
wdt_enable(WDTO_8S);
// Hook up the light hardware
pinMode(LIGHT_PIN, OUTPUT);
// Get the LCD fired up
lcd.begin(16, 2);
printMessage("TwitterBox v2.6");
// Turn on the network
printMessage("Starting up", "Ethernet...");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
delay(1000);
client.setTimeout(3000);
if(DEBUG) {
// print your local IP address:
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}
loopCount = 0;
tweetCount = 0;
}
void loop() {
loopCount++;
if (DEBUG) {
Serial.println("==================== (Loop " + String(loopCount) + ")");
}
// Look for a new tweet
if (getTweet()) {
// Print the new tweet
printMessage("New tweet from:", last_author, 0);
if (tweetCount > 1) {
// Turn the light on
if(DEBUG) Serial.println("Turning the light on");
digitalWrite(LIGHT_PIN, HIGH);
if(DEBUG) Serial.print("Pausing..");
for (int i = 0; i < 6; i++) {
if(DEBUG) Serial.print(".");
delay(1000);
}
if(DEBUG) Serial.println();
// Turn the light off
if(DEBUG) Serial.println("Turning the light off");
digitalWrite(LIGHT_PIN, LOW);
}
} else {
printMessage("No new tweet");
printMessage("Last tweet from:", last_author);
}
// A few closing messages
printMessage("Tweets Counted:", String(tweetCount));
}
boolean getTweet() {
boolean return_value = true;
printMessage("Searching for", "@officenomads");
if (client.connect("search.twitter.com", 80)) {
if(DEBUG) Serial.println("Connection established...");
client.println("GET http://search.twitter.com/search.atom?q=%40officenomads HTTP/1.0");
client.println("Host: search.twitter.com");
client.println("Connection: close");
client.println("Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7");
client.println("Cache-Control: no-cache");
client.println("Accept-Language: de,en;q=0.7,en-us;q=0.3");
client.println("Referer: http://officenomads.com/");
client.println();
if(DEBUG) Serial.println("Search request sent...");
} else {
printMessage("Connection to", "twitter failed!");
}
/*
<entry>
<id>tag:search.twitter.com,2005:112221813009944576</id>
<published>2011-09-09T17:52:19Z</published>
<link type="text/html" href="http://twitter.com/she_bikes/statuses/112221813009944576" rel="alternate"/>
<title>@officenomads we meet again</title>
<content type="html"><em>@officenomads</em> we meet again</content>
<updated>2011-09-09T17:52:19Z</updated>
<link type="image/png" href="http://a2.twimg.com/profile_images/1056271498/mms_picture__33__normal.jpg" rel="image"/>
<twitter:geo/>
<twitter:metadata>
<twitter:result_type>recent</twitter:result_type>
</twitter:metadata>
<twitter:source><a href="http://twitter.com/">web</a></twitter:source>
<twitter:lang>en</twitter:lang>
<author>
<name>she_bikes (Alexandra)</name>
<uri>http://twitter.com/she_bikes</uri>
</author>
</entry>
*/
if (client.connected()) {
if(DEBUG) Serial.println("Pulling twitter data...");
if(finder.find("entry") ) {
// Pull the tweet from the <title> elemet
if(DEBUG) Serial.print(" - tweet: ");
char tweet_buffer[140];
finder.getString("<title>" ,"</title>", tweet_buffer, 140);
String new_tweet = String(tweet_buffer);
if(DEBUG) Serial.println(new_tweet);
// Pull the timestamp from the <updated> element
if(DEBUG) Serial.print(" - timestamp: ");
char ts_buffer[17];
finder.getString("<updated>" ,"</updated>", ts_buffer, 17);
//Serial.print(ts_buffer);
//2011-09-09T17:52
//0123456789012345
ts_buffer[0] = ts_buffer[2];
ts_buffer[1] = ts_buffer[3];
ts_buffer[2] = ts_buffer[5];
ts_buffer[3] = ts_buffer[6];
ts_buffer[4] = ts_buffer[8];
ts_buffer[5] = ts_buffer[9];
ts_buffer[6] = ts_buffer[11];
ts_buffer[7] = ts_buffer[12];
ts_buffer[8] = ts_buffer[14];
ts_buffer[9] = ts_buffer[15];
for (int i = 10; i < 16; i++) {
ts_buffer[i] = ' ';
}
long new_timestamp = atol(ts_buffer);
if(DEBUG) Serial.println(new_timestamp);
// Pull the author
if(DEBUG) Serial.print(" - author: ");
char author_buffer[16];
finder.getString("<name>" ," (", author_buffer, 16);
String new_author = "@" + String(author_buffer);
new_author.toLowerCase();
if(DEBUG) Serial.println(new_author);
// Stop right now if we've seen this before
if (new_tweet == last_tweet) {
if(DEBUG) Serial.println("We've seen this tweet before! Moving on...");
return_value = false;
} else if (new_timestamp <= last_timestamp) {
if(DEBUG) Serial.println("This is an older tweet! Moving on...");
return_value = false;
} else {
// Save this info for next time
last_tweet = new_tweet;
last_timestamp = new_timestamp;
last_author = new_author;
tweetCount++;
}
} else {
printMessage("Cound not tweet", "Trying again...", 5000);
return_value = false;
}
} else {
printMessage("Disconnected!", "Trying again...", 5000);
return_value = false;
}
// Clean up
if (DEBUG) Serial.println("Cleaning up ethernet client...");
client.stop();
client.flush();
return return_value;
}
void printMessage(String line1) {
printMessage(line1, "");
}
void printMessage(String line1, String line2) {
printMessage(line1, line2, 3000);
}
void printMessage(String line1, String line2, int delayMS) {
// Reset the watchdog
wdt_reset();
// One to the serial
if(DEBUG) {
Serial.print(line1);
if (line2 != "") {
Serial.print(" ");
Serial.println(line2);
} else {
Serial.println();
}
}
// Another to the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
delay(delayMS);
//lcd.clear();
}