Go Down

Topic: Twittering using ethernet shield (Basics) (Read 10832 times) previous topic - next topic

snafu

Hello,

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

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



Code: [Select]
// 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(;;)
     ;
 }
}

snafu

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

hurley_108

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!

aland

#3
Aug 23, 2009, 07:39 am Last Edit: Aug 23, 2009, 09:42 am by alanderego Reason: 1
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 (:)

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

aland

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

Thanks,

Alan

tasasaki

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

aland

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?

Code: [Select]

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

tasasaki

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

aland

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

tasasaki

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.

aland

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

aland

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

tasasaki

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

aland

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

tasasaki

#14
Aug 24, 2009, 10:40 am Last Edit: Aug 24, 2009, 10:56 am by tasasaki Reason: 1
Quote
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.

Go Up