This Twitter Reader is capable of displaying recent Tweets. It was the latest exercise in the book “Building Wireless Sensor Networks” that I didn’t do yet.
How is this done?
First we need a text-only feed that can be downloaded. In my video example (below) I used a text file that I uploaded to my server (this). It is a quote (in dutch) from the current Chancellor of Germany, Angela Merkel:
“Als euro valt, valt Europa”. De Duitse bondskanselier Angela Merkel heeft in een toespraak voor het Duitse parlement verwezen naar de historische verantwoordelijkheid om de euro te verdedigen.
The book introduced me to Twansform. A web service that transforms twitter feeds into text. Par example, http://twansform.appspot.com/destandaard/text/4 returns the latest 4 tweets from the newspaper ‘De Standaard’.
This text is downloaded with the Connectport X2 and sent to the XBee (3.3V) which is connected to the Arduino. The Arduino runs code to transform this raw text into readable characters on the LCD screen (5V). The potentiometer you see on the images is used to adjust the brightness of the lcd screen.
video:
The code for the arduino can be downloaded on Robert Faludi’s website but I had problems compiling his code so I changed it into this:
* ********* Twitter Reader ********
* by Rob Faludi http://faludi.com
*
* displays 140 characters sourced from a URL
* using an XBee radio and a Digi ConnectPort running the XBee Internet Gateway
* http://faludi.com/projects/xig/
*/
#include
#include #include
// create a software serial port for the XBee
NewSoftSerial mySerial(6, 7);
// connect to an LCD using the following pins for rs, enable, d4, d5, d6, d7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// defines the character width of the LCD display
#define WIDTH 16
void setup() {
// set up the display and print the version
lcd.begin(WIDTH, 2);
lcd.clear();
lcd.print("Twitter_Reader");
lcd.setCursor(0,1);
lcd.print("v1.04");
delay(1000);
lcd.clear();
lcd.print("powered by XIG");
lcd.setCursor(0,1);
lcd.print("->faludi.com/xig");
delay(2000);
// set the data rate for the NewSoftSerial port,
// can be slow when only small amounts of data are being returned
mySerial.begin(9600);
}
void loop() {
// prepare to load some text
String text;
unsigned long startTime = millis();
lcd.clear();
lcd.print("loading...");
// remove anything weird from the buffer
mySerial.flush();
// request the text string from the server
mySerial.print("http://labs.stijncoppens.com/text2");
mySerial.print("\r");
// parse the incoming characters into a local String variable
char newChar;
int timeout = 4000;
while (millis()-startTime < timeout) { if (mySerial.available()) { newChar = (char)mySerial.read(); if (newChar == '\r' || newChar == '\n') { break; } else { text += newChar; } } } // clear the lcd and present the String if (text.length()>0) {
unsigned long displayTime = 60000; //300000 = 5 minutes
while(millis()-startTime < displayTime){
lcd.clear();
showText(text);
// pause after showing the string
delay(2000);
lcd.clear();
}
}
}
// displays the text on an lcd with correct line breaks between words
void showText(String theText) {
String text; // String variable for the text we are displaying
text += theText; // puts the incoming text into our String variable
String lineBuffer; // temporary storage for the last displayed line
int cpos=0; // keeps track of the current cursor position
int line=0; // keeps track of the current line
// step through the text one character at a time
for (int i=0; i
// in general, don't make a linefeed
boolean linefeed = false;
if (text[i]==' ') {
// if the current character is a space, then make a line feed
linefeed = true;
// ...but check first that there isn't another space before the edge of the screen
for (int j=i+1; j
if (text[j]==' ') linefeed=false; // another space before the edge of the screen
else if (j == text.length()-1) linefeed=false; // all of the text completes before the edge of the screen
}
}
// make a linefeed if we reach the edge of the screen (if a word is greater in length than the width)
if (cpos==WIDTH) {
linefeed==true;
}
// on linefeeds
if (linefeed==true) {
switch (line) {
case 0:
lcd.setCursor(0,1);
line = 1;
break;
case 1:
delay(400); // brief pause at end of line
// clear the screen
lcd.clear();
lcd.setCursor(0,0);
line = 0;
break;
}
cpos=0; // reset the cursor tracker to the beginning of the screen
}
// if this isn't a line feed
else {
// print the current character, add it to the line buffer and advance the cursor position
lcd.print(text[i]);
switch (text[i]) {
case '.':
delay (500);
break;
case ',':
delay(300);
break;
}
cpos++;
delay(100); // wait a moment after each character
}
}
}