How do you use twitter to control arduino ethernet?

This is my first post on these forums and I tried googleing my issue but I had no luck so here I am.

I am using the code below for my arduino ethernet and the problem I'm having is that when I tweet "ledon" nothing happens. I know its connected to the internet because I can ping its ip address.

Thanks for any help.

Note: I did change the twitter username in the code I programmed to my arduino.

/*
TweetLED Code

This code is based on the code made by Tom Igoe and is used in episode 3 of the Mike's Lab Series on Youtube.
 
 */
#include <SPI.h>
#include <Ethernet.h>


// 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,125);

// initialize the library instance:
EthernetClient client;

const int requestInterval = 30000;  // 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:
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  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();
}



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);   
          // 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();
  }
  if(tweet==">ledon")
   {
      digitalWrite(8,HIGH); 
   }
   else if(tweet==">ledoff"){

    digitalWrite(8,LOW);
   }
}

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.1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
    client.println("HOST: api.twitter.com");
    client.println();
  }
  // note the time of this connect attempt:
  lastAttemptTime = millis();
}

That code makes your Arduino a server. What client is connecting to your Arduino to ask it to do something?

wouldn't it be twitter that is acting as the client? Quite honestly I don't have a whole lot of experience coding, but i'm sort of starting to understand a little better. I followed this video on youtube

Sorry. I didn't read far enough through your code. That is actually acting as a client, not a server. If the connect was successful, and the request sent, there will be a response, which you are reading, and hoping everything is perfect.

You really should be printing the characters you read from the server, so you KNOW what the server is saying. If the reply from the server is not what you expect, and knowing it doesn't clear up the mystery, post the response here.

How do I print the characters from the server? and how do I view them? When I'm running the program I completely disconnect the arduino from my computer.

When I'm running the program I completely disconnect the arduino from my computer.

You're SOL, then.

Why do you do this?

I disconnect it because it should be able to run standalone right? It should be able to run the program without it being connected to the computer because all the information it is getting is going through the network interface. Sorry if I'm not making any sense I'm new to this and I'm just trying to figure out how to make it work.

I upload the sketch to the arduino and the disconnect it. I'm still able to ping the arduino after I disconnect it so I know its still able to receive information.

I disconnect it because it should be able to run standalone right?

Sure - once you get it debugged.

It should be able to run the program without it being connected to the computer because all the information it is getting is going through the network interface. Sorry if I'm not making any sense I'm new to this and I'm just trying to figure out how to make it work.

No, you're not. You're wishing that it worked, so you wouldn't have to understand it and debug it.

I upload the sketch to the arduino and the disconnect it. I'm still able to ping the arduino after I disconnect it so I know its still able to receive information.

But, is it getting the right information? Did it even make the right request to twitter?

Ok so I did look around and found this

http://forum.arduino.cc/index.php/topic,195135.0.html

So I guess the libraries for twitter don't exactly work any more, but does that only apply to sending tweets to twitter? or just reading them as well? since thats all I really need to do.

And yeah I don't know a whole lot about this, maybe I'm getting a little too far ahead of myself. I learned how to control leds and servos without a problem, but maybe I'm taking too big a step into this right away.

I learned how to control leds and servos without a problem, but maybe I'm taking too big a step into this right away.

No. Challenges are good. You just need to approach them realistically.

Leave the Arduino connected to the PC. It will not interfere with, in any way, the communication between the Arduino and the Ethernet shield or between the Ethernet shield and the internet.

Add a line of code:

      char inChar = client.read();
      Serial.print(inChar); // <-- Add this

Open the Serial Monitor. This resets the Arduino, which will cause it to generate a GET request. See what the server responds with.

Hey ya know what I just looked and I saw in the code that something like that is in there! I was trying to figure out what it mean't but now I realize what it means and it makes sense because in the serial monitor it says connecting to server about every 30 seconds which is what the request interval is, which is also probably my problem because its not making a connection to twitter, it just repeatedly says connecting to server and nothing else.

wcjones:
Hey ya know what I just looked and I saw in the code that something like that is in there! I was trying to figure out what it mean't but now I realize what it means and it makes sense because in the serial monitor it says connecting to server about every 30 seconds which is what the request interval is, which is also probably my problem because its not making a connection to twitter, it just repeatedly says connecting to server and nothing else.

OK, now we have some information we can work with. There are a number of assumptions that your code is making. First, it is assuming that there is a DHCP server serving up dynamic IP addresses. Can you confirm that that is indeed the case?

Second, it is assuming that there is a DNS server running, in the appropriate place, that can map the server name to an IP address. Can you confirm that that is a valid assumption?

The code has no error handling, in case things fail. It really should.

All the serial monitor is saying is

Connecting to server

On my router I can confirm it is getting a dhcp assigned ip address and it is using google dns that I setup for my home network

I found this tutorial on how to do what I want to do

https://www.sparkfun.com/tutorials/409

They explain everything line by line so I think going to follow this and build the whole sketch myself and see the results. The readings they have coming through on their serial monitor are different than mine.....

for example theirs says this

Attempting to get an IP address using DHCP:
My address:192.168.20.53
connecting to server...
making HTTP request...

@aoqassam nice :slight_smile:

So later today I'll do this and get back with my results. I think going through it myself and learning everything will help me understand whats going wrong.

Thanks for your help and I'll back to you later because its 5 in the morning and this has kept me up all night and I should really get some sleep now.

Ok so I figured out that the API for twitter has changed and the old version I was using no longer works. So I set up a wamp server that my arduino can connect to and the wamp server sends the request to twitter using access tokens and what not through using a php script.

This is the code I'm using

#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[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,20);

// initialize the library instance:
EthernetClient client;
char host[] = "192.168.1.107";
char tweet[140] = "", oldTweet[140] = "";
 
void foundHashtag()
{
  Serial.println("Found the hashtag!");
}
 
 
void setup() {
// 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);
  }
 
  for(int i = 0; i < 140; i++) {
    oldTweet[i] = 0;
    tweet[i] = 0;
  }
}
 
void loop() {
 
  int i;
 
  Serial.println("Connecting to server...");
  if (client.connect(host, 80)) {
 
    client.println("GET /arduino.php HTTP/1.1");
    client.print("Host: ");
    client.println(host);
    client.println("User-Agent: Arduino/1.0");
    client.println();
 
    TextFinder finder(client);
    while (client.connected()) {
      if (client.available()) {
        if (finder.getString("<tweet>", "</tweet>", tweet, 140) != 0) {
          for (i = 0; i < 140; i++)
              if(oldTweet[i] != tweet[i])
                break;
          if (i != 140) {
            Serial.println(tweet);
            foundHashtag();
            for (i = 0; i < 140; i++)
              oldTweet[i] = tweet[i];
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
  }
  Serial.println("delay...");
  delay (40000);   // don't make this less than 30000 (30 secs), because you can't connect to the twitter servers faster (you'll be banned)
}

Although I'm still not quite getting it quite right. All my serial monitor is saying is:

Connecting to server...
Delay...

I modified the original code to work with my arduino ethernet versus with an arduino wifi shield I think thats where I think I'm running into issues.

Could someone tell me if I have it setup properly to work with my arduino ethernet?

  for(int i = 0; i < 140; i++) {
    oldTweet[i] = 0;
    tweet[i] = 0;
  }

The NULL is a stop sign. Do you REALLY need 140 stop signs? Does having more than one mean any more than having one?

    client.print("Host: ");
    client.println(host);
    client.println("User-Agent: Arduino/1.0");

None of these are needed.

It doesn't look like you are even able to connect to 192.168.1.107.