Welcome, Guest. Please Login or Register
Arduino: Forum
09.09.2010 at 08:23:42


Pages: 1 2 
Twittering using ethernet shield (Basics) (Read 16056 times)
snafu
YaBB Newbies
*
Offline

Arduino rocks

Posts: 5

Twittering using ethernet shield (Basics)
07.02.2009 at 18:36:40
 
Hello,

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

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



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

Back to top
 
 
View Profile   IP Logged
snafu
YaBB Newbies
*
Offline

Arduino rocks

Posts: 5

Re: Twittering using ethernet shield (Basics)
Reply #1 - 09.02.2009 at 08:23:09
 
Ah, here is another solution for twittering with Arduino, not jet tested by me.
Back to top
 
 
View Profile   IP Logged
hurley_108
YaBB Newbies
*
Offline

Arduino rocks

Posts: 19

Re: Twittering using ethernet shield (Basics)
Reply #2 - 06.03.2009 at 23:41:48
 
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!
Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #3 - 23.08.2009 at 06:39:10
 
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
Back to top
 
« Last Edit: 23.08.2009 at 08:42:14 by aland »  
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #4 - 23.08.2009 at 08:38:45
 
Now does anyone have any idea how to send a tweet everytime a button is pressed?

Thanks,

Alan
Back to top
 
 
View Profile   IP Logged
tasasaki
Junior Member
**
Online

Trackballer

Posts: 74
Tokyo, Japan
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #5 - 24.08.2009 at 02:02:56
 
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:
  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
  }  

Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #6 - 24.08.2009 at 04:48:09
 
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:
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
Back to top
 
 
View Profile   IP Logged
tasasaki
Junior Member
**
Online

Trackballer

Posts: 74
Tokyo, Japan
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #7 - 24.08.2009 at 05:09:21
 
Did you read this?
After installing the library files, you may need to restart the arduino IDE.
Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #8 - 24.08.2009 at 06:23:31
 
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
Back to top
 
 
View Profile   IP Logged
tasasaki
Junior Member
**
Online

Trackballer

Posts: 74
Tokyo, Japan
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #9 - 24.08.2009 at 06:47:22
 
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.
Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #10 - 24.08.2009 at 07:09:49
 
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
Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #11 - 24.08.2009 at 08:46:53
 
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
Back to top
 
 
View Profile   IP Logged
tasasaki
Junior Member
**
Online

Trackballer

Posts: 74
Tokyo, Japan
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #12 - 24.08.2009 at 09:10:33
 
The HTTP error code 401 you got means "Authorization Required / Unauthorized".
Did you change the following line
Code:
Twitter twitter("YourID:Password"); 

as to your real twitter account?
Back to top
 
 
View Profile   IP Logged
aland
YaBB Newbies
*
Offline

A Tweeting Washing
Machine?

Posts: 17
Honolulu, Hawaii
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #13 - 24.08.2009 at 09:34:18
 
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
Back to top
 
 
View Profile   IP Logged
tasasaki
Junior Member
**
Online

Trackballer

Posts: 74
Tokyo, Japan
Gender: male
Re: Twittering using ethernet shield (Basics)
Reply #14 - 24.08.2009 at 09:40:34
 
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.
Back to top
 
 
View Profile   IP Logged
Pages: 1 2