Go Down

Topic: Made my own TwitterClient and I think it's better than the example one (Read 14881 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.
}
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

JO3RI

http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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

http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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.
}
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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 :)

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

JO3RI

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.
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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?

JO3RI

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.
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

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:

Code: [Select]
#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:

Quote
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?

JO3RI

Ok I'll try to help  :smiley-red:

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


Code: [Select]
while (client.connect()) shouldn't this be:
Code: [Select]
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.  ;)

Oh, and if does work, would you be so kind to just put my name in your code? Thanks
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

zifwat

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..

JO3RI

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);

Code: [Select]
// 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);
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

zifwat

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

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..

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)
http://www.JO3RI.be

Nederlandstalige sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Go Up