#include "AFSoftSerial.h" #include "AF_XPort.h" #include #include #define HOSTNAME "www.twitter.com" #define IPADDR "128.121.146.100" // twitter.com #define PORT 80 // HTTP #define USERNAMEPASS "username:password" // your twitter username and password, seperated by a : #define HTTPPATH "/statuses/update.xml" // the person we want to follow #define TWEETLEN 141 char linebuffer[256]; // oi int lines = 0; #define XPORT_RXPIN 2 #define XPORT_TXPIN 3 #define XPORT_RESETPIN 4 #define XPORT_DTRPIN 5 #define XPORT_CTSPIN 6 #define XPORT_RTSPIN 7 AF_XPort xport = AF_XPort(XPORT_RXPIN, XPORT_TXPIN, XPORT_RESETPIN, XPORT_DTRPIN, XPORT_RTSPIN, XPORT_CTSPIN); uint8_t errno; uint32_t laststatus = 0, currstatus = 0; /************************SKETCH START*********************/ void setup() { uint8_t ret; pinMode(13, OUTPUT); Serial.begin(57600); // set the data rate for the SoftwareSerial port xport.begin(57600); Serial.println("Hello, world..."); ret = posttweet("beep"); // tweet tweet if (ret) Serial.println("twot successful"); } void loop() // run over and over again { } /********************TWITTER STUFF**********************/ uint8_t posttweet(char *tweet) { uint8_t ret; uint8_t success = 0; ret = xport.reset(); Serial.print("Ret: "); Serial.print(ret, HEX); switch (ret) { case ERROR_TIMEDOUT: { Serial.println("Timed out on reset!"); return 0; } case ERROR_BADRESP: { Serial.println("Bad response on reset!"); return 0; } case ERROR_NONE: { Serial.println("Reset OK!"); break; } default: Serial.println("Unknown error"); return 0; } // time to connect... ret = xport.connect(IPADDR, PORT); switch (ret) { case ERROR_TIMEDOUT: { Serial.println("Timed out on connect"); return 0; } case ERROR_BADRESP: { Serial.println("Failed to connect"); return 0; } case ERROR_NONE: { Serial.println("Connected..."); break; } default: Serial.println("Unknown error"); return 0; } base64encode(USERNAMEPASS, linebuffer); // send the HTTP command, ie "GET /username/" xport.print("POST "); xport.print(HTTPPATH); xport.println(" HTTP/1.1"); Serial.print("POST "); Serial.print(HTTPPATH); Serial.println(" HTTP/1.1"); // next, the authentication xport.print("Host: "); xport.println(IPADDR); Serial.print("Host: "); Serial.println(IPADDR); xport.print("Authorization: Basic "); Serial.print("Authorization: Basic "); xport.println(linebuffer); Serial.println(linebuffer); xport.print("Content-Length: "); xport.println(7+strlen(tweet), DEC); Serial.print("Content-Length: "); Serial.println(7+strlen(tweet), DEC); xport.print("\nstatus="); xport.println(tweet); Serial.print("\nstatus="); Serial.println(tweet); xport.println(""); while (1) { // read one line from the xport at a time ret = xport.readline_timeout(linebuffer, 255, 3000); // 3s timeout // if we're using flow control, we can actually dump the line at the same time! Serial.print(linebuffer); if (strstr(linebuffer, "HTTP/1.1 200 OK") == linebuffer) success = 1; if (((errno == ERROR_TIMEDOUT) && xport.disconnected()) || ((XPORT_DTRPIN == 0) && (linebuffer[0] == 'D') && (linebuffer[1] == 0))) { Serial.println("\nDisconnected..."); return success; } } } void base64encode(char *s, char *r) { char padstr[4]; char base64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; uint8_t i, c; uint32_t n; i = 0; c = strlen(s) % 3; if (c > 0) { for (i=0; c < 3; c++) { padstr[i++] = '='; } } padstr[i]=0; i = 0; for (c=0; c < strlen(s); c+=3) { // these three 8-bit (ASCII) characters become one 24-bit number n = s[c]; n <<= 8; n += s[c+1]; if (c+2 > strlen(s)) { n &= 0xff00; } n <<= 8; n += s[c+2]; if (c+1 > strlen(s)) { n &= 0xffff00; } // this 24-bit number gets separated into four 6-bit numbers // those four 6-bit numbers are used as indices into the base64 character list r[i++] = base64chars[(n >> 18) & 63]; r[i++] = base64chars[(n >> 12) & 63]; r[i++] = base64chars[(n >> 6) & 63]; r[i++] = base64chars[n & 63]; } i -= strlen(padstr); for (c=0; c