-
Notifications
You must be signed in to change notification settings - Fork 3
/
tuitwall.ino
181 lines (159 loc) · 6.24 KB
/
tuitwall.ino
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
/*
tuitwall is an Arduino program that shows tweets, provided by
a backend that interfaces Twitter, in a LED matrix.
Copyright (C) 2011 Santiago Reig
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ArduBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with tuitwall. If not, see <http://www.gnu.org/licenses/>.
*/
// NOTE: This sketch only compiles on Arduino v1.0 or higher
#include <SPI.h>
#include <Ethernet.h>
#include "font_5x4.h"
#include "HT1632.h"
#define API_KEY "4Z17HHTeWELgxqIsB3WMWfu9V1SESwh6YKoG77Nr" // change the default value for security purposes
#define SERVER "tuitwall.kungfulabs.com"
const char get_request[] = "GET http://" SERVER "/fetch.php?api=" API_KEY;
const int interval = 12000; // minimum time between tweet retrievals
const int vec_length = 200; // maximum tweet lenght in characters
const int speed = 40; // change to control text scroll speed
const int timeout = 2000; // maximum time to receive a tweet
// Add your ethernet shield MAC address
// New ethernet shields have the MAC address printed on a sticker
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
char msg[vec_length] = "Conectando...";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println(F("tuitwall BETA4"));
Serial.println(F("KungFu Labs - http://www.kungfulabs.com"));
Serial.println(F("tuitwall Copyright (C) 2011 Santiago Reig"));
Serial.println();
Serial.println(F("## Iniciando sistemas ##"));
Serial.println();
Serial.print(F("Panel led............ "));
HT1632.begin(7,6,5);
centerText("Welcome");
Serial.println(F("OK"));
Serial.print(F("Ethernet............. "));
if (Ethernet.begin(mac) == 0) {
Serial.println(F("ERROR - DHCP"));
centerText("E-DHCP");
// couldn't get an IP, DHCP error, halt execution
Serial.println(F("## Execution halted ##"));
for(;;);
}
Serial.println(F("OK"));
// wait a second for the ethernet shield to initialize
delay(1000);
Serial.println();
Serial.println(F("## Program executing ##"));
}
void loop()
{
// get last tweet
getTweet(msg);
// show it with a scrolling motion
scrollText(msg);
}
void scrollText(char text[]){
// calculate text width
int wd = HT1632.getTextWidth(text, FONT_5X4_WIDTH, FONT_5X4_HEIGHT);
// do the horizontal scrolling motion
for(int offset=0; offset<=OUT_SIZE+wd; offset++){
showText(text,offset);
delay(speed);
}
}
void showText(char text[], int offset){
// empty memory, draw text in memory and render it on the LED matrix
HT1632.clear();
HT1632.drawText(text, OUT_SIZE - offset, 2, FONT_5X4, FONT_5X4_WIDTH, FONT_5X4_HEIGHT, FONT_5X4_STEP_GLYPH);
HT1632.render();
}
void centerText(char text[]){
// calculate text width
int wd = HT1632.getTextWidth(text, FONT_5X4_WIDTH, FONT_5X4_HEIGHT);
// if smaller than screen, align to the center, if bigger, align to the left
int offset = (wd < OUT_SIZE) ? int(((OUT_SIZE+float(wd))/2)+0.5) : OUT_SIZE;
showText(text, offset);
}
void getTweet(char tweet[]){
static long previousTime = 0; // last time we asked for a tweet
static boolean skipWait; // should we skip the wait? (for API call limit)
int pos; // array position where to store new data
char buf[vec_length]; // array where to store tweet until fully received
char c; // last character received
// if we don't have to skip the wait but, but we haven't wait the mimimum time between requests,
// then we don't request a tweet. Twitter limits the number of requests per hour to 350
// https://dev.twitter.com/docs/rate-limiting/1.1#rest
if (!skipWait && (millis()-interval < previousTime)){
Serial.println("Limiter active - Reuse tweet");
return;
}
skipWait = true; // by default we suppose we can skip the wait
previousTime = millis();
pos = 0;
strcpy(buf,"1"); // initialize the array to a value different than /0 to know if there is an error when receving
Serial.println();
Serial.print(F("Connecting........... "));
// connect to the server at port 80 (http)
if (client.connect(SERVER, 80)) {
Serial.println(F("OK"));
// get the tweet through a GET request
client.println(get_request);
client.println();
}
else {
// if failed to connect to the server
Serial.println(F("ERROR"));
return;
}
Serial.print(F("Receiving............ "));
while (client.connected() && (millis()-timeout < previousTime)) {
if (client.available()){
c = client.read();
// if is a standar character, store it in the temporal buffer
buf[pos++] = c;
// if we are in the last array position, force the character to be \0 to prevent indexing outside the array
if (pos == vec_length){
c = '\0';
buf[pos] = c;
Serial.print(F("OVERFLOW - "));
}
// last character is 0, so we know is the end of the string
if (c == '\0') {
Serial.println(F("OK"));
// close the connection to the server
client.stop();
// copy the received array to the led matrix array to show it
strcpy(tweet, buf);
Serial.print(F("Tweet: "));
Serial.println(tweet);
// no timeout existed, so we shouldn't skip wait time
skipWait = false;
return;
}
}
}
// if connection is already closed and last character is different than 0, error
if (buf[pos] != '\0' && !client.connected()){
client.stop();
Serial.println(F("ERROR - Unknown response"));
return;
}
// if there has been a timeout, close the connection
client.stop();
Serial.println(F("ERROR - Timeout"));
}