Nermal

Not Quite Normal

CATaLOG | Getting it online and talking to Twitter

While watching log output from the Arduino showing which cat has been seen was cool, I was on a roll at this point and wanted to get it talking to the wider world.

The Arduino Ethernet Shield enables the Arduino to do this really easily, with the help of the Ethernet libraries.  I also found and modified some example twitter code to send the actual tweet.

The following code sets up the interface and sets the byte array variable twitterserver to the default IP of Twitter.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 99 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

// Setup Twitter stuff
byte twitterserver[] = { 128, 121, 146, 100 }; // IP of Twitter
#define TWITTERURL "/statuses/update.json" // URL of update script
#define TWITTERUSERNAMEPW "#############" // Base64 encoded USERNAME:PASSWORD

Client client(twitterserver, 80); // Sets up the client

...and this code actually connects to the twitter IP and does a POST on an XML file to do the tweet

if (client.connect()) {
client.print("POST ");
client.print(TWITTERURL);
client.println(" HTTP/1.1");
client.println("Host: twitter.com");
client.print("Authorization: Basic ");
client.println(TWITTERUSERNAMEPW);
client.print("Content-Length: ");
client.println(10+strlen(tweet));
client.println("");
client.println("status=");
client.println(tweet);
delay(2000);
client.flush();
client.stop();
}

 

Here is the result over on Bobbin and Tuffin's twitter page :)

bobbintweet_365

As a side note, I encountered a bug in the Arduino Ethernet libraries when coding this whereby the Ethernet shield would hang after the first connection attempt.  The solution is documented over in this thread on the Arduino forums.

In order the provide more functionality the Arduino no longer tweets directly to twitter.com - it instead talks to a script on nermal.net which handles the generation of the tweet messages, as well as things like updating the live status page and logging of events for statistics.

← Return to CATaLOG project page