#include
#include
#include "AFSoftSerial.h"
#include "AF_XPort.h"
#define PIEZOPIN 12
#define HOSTNAME "www.twitter.com"
#define IPADDR "128.121.146.100" // twitter.com
#define PORT 80 // HTTP
#define HTTPPATH "/seanbonner/" // the person we want to follow
#define TWEETLEN 141
char linebuffer[256]; // our large buffer for data
char tweet[TWEETLEN]; // the tweet
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*********************/
uint32_t parsenumber(char *str) {
uint32_t num = 0;
char c;
// grabs a number out of a string
while (c = str[0]) {
if ((c < '0') || (c > '9'))
return num;
num *= 10;
num += c - '0';
str++;
}
return num;
}
char * fetchtweet(void) {
uint8_t ret;
char *found=0, *start=0, *end=0;
tweet[0] = 0; // reset the tweet
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;
}
// send the HTTP command, ie "GET /username/"
xport.print("GET "); xport.println(HTTPPATH);
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);
// look for an entry (the first one)
found = strstr(linebuffer, "entry-title entry-content");
if (((int)found) != 0) {
start = strstr(found, ">") + 1;
end = strstr(found, "
");
if ((start != 0) && (end != 0)) {
//Serial.println("\n******Found first entry!*******");
end[0] = 0;
//Serial.print(start);
// save the tweet so we can display it later
strncpy(tweet, start, TWEETLEN);
tweet[TWEETLEN-1] = 0;
}
}
// next we look for a status ID (which should correspond to the previous tweet)e
found = strstr(linebuffer, "");
if ((start != 0) && (end != 0)) {
//Serial.println("\n******Found status ID!*******");
end[0] = 0;
//Serial.println(start);
// turn the string into a number
currstatus = parsenumber(start);
//Serial.println(currstatus, DEC);
// check if this is a nu tweet
if (currstatus > laststatus) {
laststatus = currstatus;
// Serial.println("New message");
// Serial.print(tweet);
} else {
tweet[0] = 0;
}
// flush the conn
xport.flush(5000); // 5 second timeout
if (tweet[0] == 0) { return 0; }
else {return tweet; }
}
}
if (((errno == ERROR_TIMEDOUT) && xport.disconnected()) ||
((XPORT_DTRPIN == 0) &&
(linebuffer[0] == 'D') && (linebuffer[1] == 0))) {
Serial.println("\nDisconnected...");
return 0;
}
}
}
void beep(uint8_t pin, uint16_t freq, int32_t us) {
int usdelay;
usdelay = 500000 / freq;
us *= 1000; //in uS
while (us > 0) {
digitalWrite(pin, HIGH);
delayMicroseconds(usdelay);
digitalWrite(pin, LOW);
delayMicroseconds(usdelay);
us -= usdelay*2;
}
}
void setup() {
pinMode(13, OUTPUT);
pinMode(PIEZOPIN, OUTPUT);
Serial.begin(57600);
// xport
xport.begin(57600);
Serial.println("Hello, world...");
}
void loop() // run over and over again
{
char *tw;
tw = fetchtweet(); // tweet tweet
Serial.println("twot");
if (tw != 0) {
// new tweeeter!
Serial.print("TWEEEEEEEEET");
beep(PIEZOPIN, 2000, 500); // 2khz, 500ms
Serial.print("---> "); Serial.println(tw);
}
delay(10000); // wait ten seconds
}