This shows you how to tweet from your mbed.
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.
Information
This demo does not yet use the official mbed networking stack
Step-by-step¶
- Initialize & setup your network interface (here, Ethernet)
- Create your HTTPClient instance
- Write your tweet in an HTTPMap instance
- POST your tweet ;) (but do not try to recover server's response, it's big and useless!)
- Read the result code
...and your tweet is online :).
Example code¶
Program¶
Information
The basic authentication service for twitter is going down at the end of the week. To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy. Simply visit the website to setup your twitter account for this API. See: http://www.supertweet.net/about/documentation
#include "mbed.h" #include "EthernetNetIf.h" #include "HTTPClient.h" EthernetNetIf eth; int main() { printf("Init\n"); printf("\r\nSetting up...\r\n"); EthernetErr ethErr = eth.setup(); if(ethErr) { printf("Error %d in setup.\n", ethErr); return -1; } printf("\r\nSetup OK\r\n"); 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 HTTPResult r = twitter.post("http://api.supertweet.net/1/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; }
You can also import the published program:
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.
To go a bit further...¶
You can also play around with the different storage classes: HTTPMap, HTTPFile, HTTPText (or even create your own)!