Twittering using ethernet shield (Basics)

Hello,

just managed to get my arduino posting the first tweet. Lacking programming skills this is only a rude skeleton of a final program :slight_smile:

Nevertheless, here is the code, maybe it helps someone (i found nearly nothing according to twitter in this forum) have fun!

// a brute test of twittering with arduino. 
// source for handling the twitter api was http://pratham.name/post/39551949/twitter-php-script-without-curl
// the scetch lacks a lot of functionality, the encoding of username and password must be done separately
// and the message is fixed (text is "Yahoo, im twittering!")
// 
// this is due to my ony basic knowledge
// maybe someone can use this snippet and have fun with it


#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 10 };                      // this is the ip within my lan
byte gateway[] = { 192, 168, 2, 1 };                  // neccessary to get access to the internet via your router
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 128, 121, 146, 100 };               // Twitter's ip

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("POST http://twitter.com/statuses/update.json HTTP/1.1");
    client.println("Host: twitter.com");
    client.println("Authorization: Basic #################");    // the string of ###s after "Basic" is the base64_encoded Text of username:password of your twitter account
                                                                        // you can do the encoding at this site: http://www.functions-online.com/en/base64_encode.html
    client.println("Content-type: application/x-www-form-urlencoded");
    client.println("Content-length: 28");                               // this is the length of the text "Yahoo, im twittering!"
    client.println("Connection: Close");
    client.println();
    client.print("status=Yahoo, im twittering!");

  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Ah, here is another solution for twittering with Arduino, not jet tested by me.

I'm pretty rubbish at programming, too, but from a simple skeleton like that, I'm sure I can figure out how to do interesting things. Thanks for sharing!

NEVER MIND...
I got it to work using the first poster's code to work.
Yes, it was the BASE64 (User ID and Password).
I used this site to encode, then I just copied and pasted to the code.
Don't forget...no spaces and keep the colon (:slight_smile:

Thanks for the code!!!

OK... I am stuck...
Is there anyone who can PM me their actual code so I can try it and see?
I can't figure it out.
Maybe it is the BASE64 (User ID and Password), maybe it is something else???
I don't know...

Thanks,

Alan

Now does anyone have any idea how to send a tweet everytime a button is pressed?

Thanks,

Alan

Why not try to use this library?
I confirmed that this library works fine and is easy to use.
The simple example clearly shows how to send a message.
Though I have not tested the library with physical buttons, the code you want will be like the following:

  if (buttonState == HIGH) {     
    Serial.println("connecting ...");
    if (twitter.post(msg)) {
      int status = twitter.wait();
      if (status == 200) {
        Serial.println("OK.");
      } else {
        Serial.print("failed : code ");
        Serial.println(status);
      }
    } else {
      Serial.println("connection failed.");
    }
    delay(1000); //prevent multiple sending at a time
  }

I tried using the twitter example on the Arduino website.
I entered my network info and userID & password only.
When I tried to upload it to the Arduino I get this error:
21: error: Twitter.h: No such file or directory In function 'void setup()':
Bad error line: -2

Line 21 is "int status = twitter.wait();"

Am I supposed to enter any more info?

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }

Thanks,

Alan

Did you read this?
After installing the library files, you may need to restart the arduino IDE.

Yes I did read that.
The problem I am having is loading the code.
I am new to Arduino coding, but what does restarting the Arduino have anything to do with problems loading the code?

Thanks,

Alan

Did you put the "unzipped" twitter directory in arduino/hardware/libraries/ directory?
If so, you should be able to load SimplePost sketch by selecting File>Examples>Twitter>SimplePost.

Ahhh OK...
I got what you are getting at.
I am using a mac for the programming so I might have messed up the file location.
I followed this thread.
I really should try and find a PC and try it again.

Thanks,

Alan

OK I found a PC and the code loads fine.
After setting it all up and running the Arduino I get a "failed : code 401"
Does anyone know what that is?
I removed the ethernet cable and I get "connection failed" so I know that it recognizes the cable.

Thanks,

Alan

The HTTP error code 401 you got means "Authorization Required / Unauthorized".
Did you change the following line
Twitter twitter("YourID:Password"); as to your real twitter account?

I think I have the right user ID and password.
I used base 64 for it.
I'll try and recheck the encoding though.
Thanks for the error code.
By the way, are the error codes posted somewhere?

Thanks again,

Alan

I used base 64 for it.

The cleaver clever library automatically encodes the characters, so you don't have to encode by yourself.
Please try to put your ID and Password directly.

Got it, I'll try that first...

Thanks,

Alan

OK, I changed the USER ID & Password to my user name & pass and it works.

I am working on the button side of the code.

After that I just need to compile them together.

Wait I didn't even say what my project is....

If you are curious see this posting...

Thanks for the help,

Alan

I have been trying for days to get my Ethernet shield to connect to the internet. I was successful once in trying to ping Google, but have not been able to repeat.

I am using the twitter library and sample code, added my "token" and get a Failed Code: 403 when the sketch is run. I had to include a 40 second delay in the sketch to give the shield time to initialize (shield will blink FULLD, LINK, 100M in unison for several seconds before settling in on a steady LINK and FULLD, sometimes 100M). (<---not sure if thats normal)

I have the shield hooked directly to my D-Link DIR-825 router. All other devices connected to that router have a valid connection.

I have a feeling that it has something to do with my login (yes I am logged out of Twitter when trying to connect w/ the Arduino)

Interestingly enough the shield has started working... randomly. The tweets go through and are displayed on twitter but I am still getting a "failed : code 403" on the Serial Monitor

holy christmas cookies..... its not working again... i changed NOTHING!!!! Now getting a code 500 error

help?