Tweeting with mbed! (using SuperTweet)

Dependencies:   EthernetNetIf NTPClient_NetServices TextLCD mbed

main.cpp

Committer:
kagamikan
Date:
2011-06-11
Revision:
0:2e771f40cf84
Child:
1:5f287a9e95c8

File content as of revision 0:2e771f40cf84:

/*
 * UDENOKAI #5 SAMPLE PROGRAM: Tweeting with mbed!
 * 
 * Using SuperTweet.Net API Proxy (setup required).
 * Based on cookbook/Twitter
 */
#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"
#include "TextLCD.h"    // Orange Board

Serial pc(USBTX, USBRX); // tx, rx
TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7
EthernetNetIf eth;

int main() {
  char message[64], name[32], twID[32], twPW[32];
  sprintf(message, "hogehogefugafuga");
  sprintf(name, "INPUT_YOUR_NAME");         // ex) @kagamikan
  sprintf(twID, "INPUT_YOUR_ID");           // ex) udenokai
  sprintf(twPW, "INPUT_YOUR_PASSWORD");     // ex) hoge

  pc.printf("\r\nSetting up...\r\n");
  lcd.printf("Setting up...");
  EthernetErr ethErr = eth.setup();
  if(ethErr) {
    printf("Error %d in setup.\n", ethErr);
    lcd.printf("\nError %d in setup.", ethErr);
    return -1;
  }
  IpAddr ip = eth.getIp();
  pc.printf("\r\nSetup OK\r\n");
  lcd.cls();
  lcd.printf("IP Address:\n%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  
  time_t sec = time(NULL);
  char tweet[128], ts[32];
  strftime(ts, 32, "%I:%M %p\n", localtime(&sec));
  sprintf(tweet, "%s (%s's mbed at %s UTC) #udenokai", message, name, ts);

  HTTPClient twitter;  
  HTTPMap msg;
  msg["status"] = tweet;
  twitter.basicAuth(twID, twPW);
  HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL); 
  if(r == HTTP_OK) {
    pc.printf("Tweet success!\n");
    lcd.cls();
    lcd.printf("\nTweet success!");
  } else {
    pc.printf("Tweet failed (Code:%d)\n", r);
    lcd.cls();
    lcd.printf("\nTweet failed (%d)", r);
  }
  return 0;
}