You are viewing an older revision! See the latest version

Twitter

Twitter Example

This shows you how to tweet from your mbed, using the new networking stack.

This is basically a simple demo of the HttpClient and HttpMap classes, and is a good example for anyone looking to use web-based APIs.

Step-by-step

  1. Initialize & setup your network interface (here, Ethernet)
  2. Create your HttpClient instance
  3. Write you tweet in an HttpMap instance
  4. POST your tweet ;) (but do not try to recover server's response, it's big and useless!)
  5. Read the result code

...and your tweet is online :).

Example code

Program

#include "mbed.h"
#include "if/net/net.h"
#include "if/eth/ethIf.h"
#include "http/client/HttpClient.h"
#include "http/client/data/HttpMap.h"

EthernetNetIf eth; 

int main() {

  printf("Init\n");

  eth.setup();

  HttpClient twitter;
  
  HttpMap msg;
  msg["status"] = "I am tweeting from my mbed!"; //A good example of Key/Value pair use with Web APIs
  
  twitter.basicAuth("myuser", "mypass"); //We use basic authentication, replace with you account's parameters
  
  //No need to retieve data sent back by the server
  HTTP_RESULT r = twitter.post("http://twitter.com/statuses/update.xml", msg, NULL); 
  if( r == HTTP_OK )
  {
    printf("Tweet sent with success!\n");
  }
  else
  {
    printf("Problem during tweeting, return code %d\n", r);
  }
  
  return 0;

}

Results

Example trace with debug on:

Setting up...

HW Addr is : 00:00:00:f0:22:**.

In Setup.

DHCP Started, waiting for IP...

Connected, IP : 10.1.193.21

Setup OK

URL parsed,
Host: twitter.com
Port: 0
Path: /statuses/update.xml

DNS Query...

DNS Resolved.

Connecting...

Event 0 in HttpClient::onTcpSocketEvent()

Data [len 22]:
status=I+am+an+mbed%21

Writing headers:

Authorization: Basic ZGdfY****************==

Content-Length: 22

Content-Type: application/x-www-form-urlencoded

Event 3 in HttpClient::onTcpSocketEvent()

Event 3 in HttpClient::onTcpSocketEvent()

Event 2 in HttpClient::onTcpSocketEvent()

Response OK

Read header : Date: Fri, 09 Apr 2010 16:04:14 GMT
Read header : Server: hi
Read header : Status: 200 OK
Read header : X-Transaction: 1270829053-*****-*****
Read header : ETag: "ab0674524dc*********"
Read header : Last-Modified: Fri, 09 Apr 2010 16:04:13 GMT
Read header : X-Runtime: 0.46079
Read header : Content-Type: application/xml; charset=utf-8
Read header : Content-Length: 1869
Read header : Pragma: no-cache
Read header : X-Revision: DEV
Read header : Expires: Tue, 31 Mar 1981 05:00:00 GMT
Read header : Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Read header : Set-Cookie: guest_id=1270829053758; path=/; expires=Sun, 09 May 2010 16:04:13 GMT
Read header : Set-Cookie: lang=en; path=/
Read header : Set-Cookie: _twitter_sess=********
Read header : Vary: Accept-Encoding
Read header : Connection: close
All headers read.

Done :)!

HTTP Result 0

Req completed.

/media/uploads/donatien/tweetfrommbed.jpg

To go a bit further...

You can also play around with the different storage classes: HttpMap, HttpFile, HttpText (or even create your own)!


All wikipages