Arduino Twitter Feed Viewer

details

IMG_0370-1024x768

Description

In this project, we make the Arduino to obtain live twitter feeds from the Internet and display it on an LCD. We just modified the library so that now it displays the feeds on the LCD instead of the Serial Monitor.

 

Hardware Requirements

Arduino uno

Arduino Ethernet Shield

LCD display

 

Code

#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6, 5, 8, 3, 2);
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,177);

// initialize the library instance:
EthernetClient client;

const int requestInterval = 60000;  // delay between requests

char serverName[] = "api.twitter.com";  // twitter URL

boolean requested;                   // whether you've made a request since connecting
long lastAttemptTime = 0;            // last time you connected to the server, in milliseconds

String currentLine = "";            // string to hold the text from server
String tweet = "";                  // string to hold the tweet
boolean readingTweet = false;       // if you're currently reading the tweet

void setup() {
  // reserve space for the strings:
  
  // Print a message to the LCD.
  currentLine.reserve(256);
  tweet.reserve(150);
// initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  if (!Ethernet.begin(mac)) {
    // if DHCP fails, start with a hard-coded address:
    Ethernet.begin(mac, ip);
  }
  // connect to Twitter:
  connectToServer();
  lcd.begin(16,2);
}

void loop()
{
  if (client.connected()) {
    if (client.available()) {
      // read incoming bytes:
      char inChar = client.read();

      // add incoming byte to end of line:
      currentLine += inChar; 

      // if you get a newline, clear the line:
      if (inChar == '\n') {
        currentLine = "";
      } 
      // if the current line ends with <text>, it will
      // be followed by the tweet:
      if ( currentLine.endsWith("<text>")) {
        // tweet is beginning. Clear the tweet string:
        readingTweet = true; 
        tweet = "";
      }
      // if you're currently reading the bytes of a tweet,
      // add them to the tweet String:
      if (readingTweet) {
        if (inChar != '<') {
          tweet += inChar;
        } 
        else {
          // if you got a "<" character,
          // you've reached the end of the tweet:
          readingTweet = false;
          Serial.println(tweet);  
         lcd.setCursor(0, 0); 
          lcd.print(tweet);
          // close the connection to the server:
          client.stop(); 
        }
      }
    }   
  }
  else if (millis() - lastAttemptTime > requestInterval) {
    // if you're not connected, and two minutes have passed since
    // your last connection, then attempt to connect again:
    connectToServer();
  }
}

void connectToServer() {
  // attempt to connect, and wait a millisecond:
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    Serial.println("making HTTP request...");
  // make HTTP GET request to twitter:
    client.println("GET /1/statuses/user_timeline.xml?screen_name=siewwaihung&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

 

 

In the sketch I had to change the usual pin assignments for the LCD. Because the Ethernet Shield is using pins 10, 11, 12 and 13, I had to avoid using these for the LCD. Also I tried not to use pin 4 because that’s reserved for the SD card on the shield.

This is how it looked like, two shields on top of the Arduino Uno.

 

 

IMG_0365-1024x768

 

IMG_0376-1024x768 IMG_0371-1024x768

 

Tags: 201802, W5100, Ethernet, Arduino, Twitter

 

 

COMMENTS

Please Login to comment
  Subscribe  
Notify of