0
Offline
Newbie
Karma: 0
Posts: 9
Arduino rocks
|
 |
« on: February 07, 2009, 12:36:40 pm » |
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! // 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(;;) ; } }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 9
Arduino rocks
|
 |
« Reply #1 on: February 09, 2009, 02:23:09 am » |
Ah, here is another solution for twittering with Arduino, not jet tested by me.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #2 on: March 06, 2009, 05:41:48 pm » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #3 on: August 23, 2009, 12:39:10 am » |
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
|
|
|
|
« Last Edit: August 23, 2009, 02:42:14 am by alanderego »
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #4 on: August 23, 2009, 02:38:45 am » |
Now does anyone have any idea how to send a tweet everytime a button is pressed? Thanks, Alan
|
|
|
|
|
Logged
|
|
|
|
|
Tokyo, Japan
Offline
Jr. Member
Karma: 0
Posts: 77
Trackballer
|
 |
« Reply #5 on: August 23, 2009, 08:02:56 pm » |
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 }
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #6 on: August 23, 2009, 10:48:09 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Tokyo, Japan
Offline
Jr. Member
Karma: 0
Posts: 77
Trackballer
|
 |
« Reply #7 on: August 23, 2009, 11:09:21 pm » |
Did you read this? After installing the library files, you may need to restart the arduino IDE.
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #8 on: August 24, 2009, 12:23:31 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Tokyo, Japan
Offline
Jr. Member
Karma: 0
Posts: 77
Trackballer
|
 |
« Reply #9 on: August 24, 2009, 12:47:22 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #10 on: August 24, 2009, 01:09:49 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #11 on: August 24, 2009, 02:46:53 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Tokyo, Japan
Offline
Jr. Member
Karma: 0
Posts: 77
Trackballer
|
 |
« Reply #12 on: August 24, 2009, 03:10:33 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Honolulu, Hawaii
Offline
Newbie
Karma: 0
Posts: 20
A Tweeting Washing Machine?
|
 |
« Reply #13 on: August 24, 2009, 03:34:18 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Tokyo, Japan
Offline
Jr. Member
Karma: 0
Posts: 77
Trackballer
|
 |
« Reply #14 on: August 24, 2009, 03:40:34 am » |
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.
|
|
|
|
« Last Edit: August 24, 2009, 03:56:16 am by tasasaki »
|
Logged
|
|
|
|
|
|