Go Down

Topic: Made my own TwitterClient and I think it's better than the example one (Read 14 times) previous topic - next topic

JO3RI

Well, I needed a TwitterCliƫnt getting a twitter feed, but using less SRAM than the one from the example. Also I find the example one being very slow en not responsive.

This is what I made off it. Of course you'll need an Arduino Ethernet or an Arduino with EthernetShield

Code: [Select]
/*
* TWITTERFEED SHOWN on SERIAL MONITOR
*
* last changed 23/02/2012
*
* Version 1.0
*
* Arduino 1.0 is needed !!!
*
* Made by JO3RI check check http://www.jo3ri.be/arduino
*
*/



#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE4 };
byte ip[] =  { 192,168,1,10}; //change this
byte subnet[] = {255,255,255,0}; //change this
byte gateway[] = {192,168,1,1}; //change this
byte dns[] = {192,168,1,1}; //change this
EthernetClient client;
String TwitterName ="JO3RI"; //change this to your own twittername, or follow me ;-)
char tweet[140];
String SearchString ="<title>";
byte charsize;
char serverName[] = "api.twitter.com";  // twitter URL


void setup() {
  // initialize serial:
  Serial.begin(9600);
  // attempt a DHCP connection:
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  // connect to Twitter:
  delay(3000);
}



void loop(){
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    TextFinder  finder( client,2 );
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.print("GET /1/statuses/user_timeline.rss?screen_name=");
    client.print(TwitterName);
    client.println("&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
    Serial.println("sended HTTP request...");
    while (client.connected()) {
      if (client.available()) {
        Serial.println("looking for tweet...");
        SearchString = SearchString + TwitterName + ": ";
        charsize = SearchString.length() + 1;
        char StartHere[charsize];
        char EndHere[] = "</title>";
        SearchString.toCharArray(StartHere,charsize);
        if((finder.find("<item>")&&(finder.getString(StartHere,EndHere,tweet,140)!=0)))
        Serial.println(tweet);
        break;
      }
    }
    delay(1);
    client.stop();
  }
  Serial.println("delay...");
  delay (60000);
  // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
  // off course it would be better to use the "Blink without delay", but I leave that to you.
}

JO3RI


robtillaart


nice work, one can feed the tweets to a robot or a dogfeeder or anything to control it.. :)

Also a nice showcase for the textfinder class

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

JO3RI


JO3RI

Meanwhile, I found out how the get a hashtag feed, so you can show the last tweets holding a hashtag like #Arduino.

hurray hurray.  :D

Code: [Select]
/*
* TWITTERFEED SHOWN on SERIAL MONITOR
*
* last changed 27/02/2012
*
* Version 2.0 hashtag version
*
* Arduino 1.0 is needed !!!
*
* Made by JO3RI check check http://www.jo3ri.be/arduino
*
*/



#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE4 };
byte ip[] =  { 192,168,1,10}; //change this
byte subnet[] = {255,255,255,0}; //change this
byte gateway[] = {192,168,1,1}; //change this
byte dns[] = {192,168,1,1}; //change this
EthernetClient client;
char TwitterHashtag[] = "arduino"; //change this to your own twitter hashtag, or follow arduino ;-)
char tweet[140];
char serverName[] = "search.twitter.com";  // twitter URL


void setup() {
  // initialize serial:
  Serial.begin(9600);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  // connect to Twitter:
  delay(3000);
}



void loop(){
  Serial.println("connecting to server...");
  if (client.connect(serverName, 80)) {
    TextFinder  finder( client,2 );
    Serial.println("making HTTP request...");
    // make HTTP GET request to twitter:
    client.print("GET /search.atom?q=%23");
    client.print(TwitterHashtag);
    client.println("&count=1 HTTP/1.1");
    client.println("HOST: search.twitter.com");
    client.println();
    Serial.println("sended HTTP request...");
    while (client.connected()) {
      if (client.available()) {
        Serial.println("looking for tweet...");
        if((finder.find("<published>")&&(finder.getString("<title>","</title>",tweet,140)!=0)))
        Serial.println(tweet);
        break;
      }
    }
    delay(1);
    client.stop();
  }
  Serial.println("delay...");
  delay (60000);
  // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
  // off course it would be better to use the "Blink without delay", but I leave that to you.
}

Go Up