Made my own TwitterClient and I think it's better than the example one

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

/*
 * 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.
}

small proof of concept. I'll make a better video at home

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

Also a nice showcase for the textfinder class

thanks !

This video shows the feed on a Sure32x08 Dot Matrix

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

hurray hurray. :smiley:

/*
 * 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.
}

Hi JO3RI

I'm pretty new to this whole Arduino and Twitter thing so I was wanting to ask a few questions if I could :slight_smile:

Does the code that you have written and posted allow me to use my Arduino and ethernet shield to scan twitter (or my twitter profile for a specific hashtag) to then control something attached to the arduino?
Thanks
Clive

Hi Clive. With mu code you'll be able to follow a twitterperson and show it's tweets. You can use it to find a word or hashtag in the tweet, but you'll have to change the code a little bit.

If you want the Arduino to do things, depending on the words after the hashtags, you'll have to add even more code.

If you're new to Arduino, this will not be easy, but don't give up, we all had to start somewhere.

JO3RI,

I'm trying to make an Arduino app that will trigger actions based on a hashtag coming up in the public stream....Using the REST API (search) every 30 seconds would work...But wouldn't that give me duplicate tweets with the target hashtag from previous searches? Wouldn't Twitter's Streaming API be better suited for this app?

Do you have any plans to use Twitter's Streaming API for your Twitter client?

Hi,

I haven't planned to do that, but I shouldn't be that hard. Most important part is the textfinder part. All you have to do, is use an other rss or html feed.

Feel free to implement your own and show us your results.

Good luck.

Hello JO3RI,

I am working on a project that also uses a twitter feed for controlling hardware although I am trying to also use WiFi by means of this product:

http://diysandbox.com/our-products/arduino/platinum

I am having trouble however porting your code over to the wifi libraries.

Below is the modified code:

#include <SPI.h>
#include <Wirefree.h>
#include <WifiClient.h>
#include <TextFinder.h>

WIFI_PROFILE w_prof = { "R_Squared_WiFi",       /* SSID */
                        "0123456789" ,          /* WPA/WPA2 passphrase */
                        NULL,                   /* Set for DHCP */
                        NULL,                   /* Set for DHCP */
                        NULL                    /* Set for DHCP */
                      };

String TwitterName ="EETSeniorDesign";
char tweet[140];
String SearchString ="<title>";
byte charsize;
char serverName[] = "api.twitter.com";

WifiClient client(serverName, 80);

void parseRxData(String data)
{
}

void setup() 
{
  // initialize serial:
  Serial.begin(9600);
  
  // start wifi
  Wireless.begin(&w_prof, &parseRxData);
  
  // connect to twitter:
  delay(3000);
}

void loop()
{  
  Serial.println("debug - void loop()");  //debug
  
  if (client.connect());
  {
    Serial.println("debug - client.connect() = true");  //debug
  
    TextFinder finder( client,2 );
  
    client.print("GET /1/statuses/user_timeline.rss?screen_name=");    
    client.print(TwitterName);
    client.print("&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  
    Serial.println("debug - HTTP Request Sent");  //debug
  
    while (client.connect())
    {    
      Serial.println("debug - client.connect() = true");  //debug
      
      if (client.available())
      {
        Serial.println("debug - client.available() = true");  //debug
        
        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;
      }    
      else
      {
        Serial.println("debug - client.available() = false");  //debug
      }
    }
    delay(1);
    client.stop();
    Serial.println("debug - client.stop()");  //debug
  }

delay(60000);
Serial.println("debug - delay (600000)");  //debug 
}

This is the response I get from the serial output:

AT+WWPA=0123456789
AT+WA=R_Squared_WiFi
AT+NDHCP=1
debug - void loop()
AT+NCTCP=api.twitter.com,80
debug - client.connect() = true
debug - HTTP Request Sent
AT+NCTCP=api.twitter.com,80
debug - client.stop()
debug - delay (600000)

Would you or anyone else have any suggestions?

Ok I'll try to help :blush:

if (client.connect()); shouldn't this be: if (client.connect(serverName, 80)) {

while (client.connect()) shouldn't this be: while (client.connected()) {

and ... you have to be sure, your wifi shield get's an ip for a dns-server. I'm guessing you're using dhcp to get ip, mask, gw and dns. Maybe you should try static first.

good luck and if it works, don't forget to post something to show off. :wink:

Oh, and if does work, would you be so kind to just put my name in your code? Thanks

hi jo3ri,

frankly speaking, i'm really new to this arduino thing.. i would like to ask for your favor..
currently i have 2 unit of sure electronic 32x16 led matrix that i have connected to my arduino UNO..
and i have also bought an ethernet shield..
as i am writing this post, i have succeed in displaying character on the LED matrix display..
now i want to connect the ethernet shield to my UNO so that i can receive witter update and display it on the LED matrix display..

so my question is, do i need to merge your TwitterClient code into my existing LED matrix display coding or do i just upload your code to my UNO and it will automatically display the update accordingly on the LED matrix? if i do need to merge your TwitterClient code, how am i going to do so? as for now thats my only question.. hope you can help me solve this.. thanks jo3ri..

p/s : this is the LED matrix display coding that i said before..

Hi,

It's nice you want to use my code, Thanks. So, because you're new to Arduino, you probably will have to read a lot about coding and stuff.

For your question, I'll give you some guidance. Want you'll need to do is combine the code for the Twitter client with the code for showing it on a DOT matrix. It is possible (I did this in an other project of mine: CastDuino, google it, if you want, but CastDuino is way more complicated).

My Twitter client get's the tweet and puts it in a buffer for show it on the serial monitor. You can use the same buffer to show the tweet on an LCD or DOT-Matrix. It isn't that hard to accomplish.

This is only a part of the code, but you should put instead of the part: Serial.println(tweet);

// show tweet buffer
  wd = HT1632.getTextWidth(tweet, FONT_7X5_WIDTH, FONT_7X5_HEIGHT);
  HT1632.drawTarget(BUFFER_BOARD(1));
  HT1632.clear();
  HT1632.drawText(tweet, 2*OUT_SIZE - i, 0, FONT_7X5, FONT_7X5_WIDTH, FONT_7X5_HEIGHT, FONT_7X5_STEP_GLYPH);
  HT1632.render();
  
  HT1632.drawTarget(BUFFER_BOARD(2));
  HT1632.clear();
  HT1632.drawText(tweet, OUT_SIZE - i, 0, FONT_7X5, FONT_7X5_WIDTH, FONT_7X5_HEIGHT, FONT_7X5_STEP_GLYPH);
  HT1632.render();

  i = (i+1)%(wd + OUT_SIZE * 2);

i'm sorry but i need to say that i didnt get what you mean by

This is only a part of the code, but you should put instead of the part: Serial.println(tweet);

i would realy appreciate if you can explain to me in more details.. thanks..

and btw, the part of the code, is it from your project, castduino?

thanks jo3ri..

Ok, but first try to get my twitterclient working on your own arduino with the serial monitor. Let me know if you get that working

And yes, it is a very small part from CastDuino (output part)

hey JO3RI,

I've started a similar project that uses a freetronics etherTen board(basically a board similar and compatible with the arduino).

I hooked it up to a 20 by 4 LCD display and everything is going fine. I'm just stuck on coding with the board that is suppose to connect it to the internet to retrieve tweets from my twitter account. do i just run your code and edit what is necessary to get it up and running then edit it?

also how do i get the ip, mac address, subnet mask, gateway and dns? do i just leave it as it is and rely on dhcp or?

appreciate your help. Have a few books on arduino but need a little kick to get this up and running.

cheers!

Hi,

First just try the code as is, but manually change ip, mask, gw and dns (the code doesn't use DHCP). You should know this things from your own network.

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

ip: pick a free ip in your network
subnet: take the same one from your computer
gateway: take the same one from your computer
dns: take the same one from your computer

juest leave the mac address as is.

once this is running, you can continue to get the message on your LCD

Hi JO3RI,

I've made amendments to the program but i received this error.

'byte dns []' redeclared as different kind of symbol

could my version of Ethernet.h library be different?

i tried commenting the byte dns line but nothing showed up on the LCD.

Appreciate your advise!

cheers!

Hi again,

strange thing:

'byte dns []' redeclared as different kind of symbol

I just uploaded the code (you find in the very beginning of this topic), and uploaded it to my arduino ethernet and arduino IDE 1.0

guess what, it just works :~

Did you try to use dhcp any way on top of my code? (it seems dns got declared more then once)

if you can't find the problem, just post your code here, and I'll cross check it.

Hi again.

This morning I tried Arduino IDE 1.0.1 and guess what ... yep:

'byte dns []' redeclared as different kind of symbol'

So, it must be something with Arduino IDE 1.0.1, but I don't know what. I'll try to figure it out, bur for now, you beter use Arduino IDE 1.0.0 http://arduino.googlecode.com/files/arduino-1.0-windows.zip