Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TwitterExample.cpp@0:b1c85511a5ca, 2010-07-24 (annotated)
- Committer:
- k_ochiai
- Date:
- Sat Jul 24 08:39:29 2010 +0000
- Revision:
- 0:b1c85511a5ca
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| k_ochiai | 0:b1c85511a5ca | 1 | /* |
| k_ochiai | 0:b1c85511a5ca | 2 | Update: 21-06-2010 |
| k_ochiai | 0:b1c85511a5ca | 3 | The basic authentication service for twitter is going down at the end of the week. |
| k_ochiai | 0:b1c85511a5ca | 4 | To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy. |
| k_ochiai | 0:b1c85511a5ca | 5 | Simply visit the website to setup your twitter account for this API. |
| k_ochiai | 0:b1c85511a5ca | 6 | See: http://www.supertweet.net/about/documentation |
| k_ochiai | 0:b1c85511a5ca | 7 | */ |
| k_ochiai | 0:b1c85511a5ca | 8 | |
| k_ochiai | 0:b1c85511a5ca | 9 | #include "mbed.h" |
| k_ochiai | 0:b1c85511a5ca | 10 | #include "EthernetNetIf.h" |
| k_ochiai | 0:b1c85511a5ca | 11 | #include "HTTPClient.h" |
| k_ochiai | 0:b1c85511a5ca | 12 | |
| k_ochiai | 0:b1c85511a5ca | 13 | EthernetNetIf eth; |
| k_ochiai | 0:b1c85511a5ca | 14 | |
| k_ochiai | 0:b1c85511a5ca | 15 | int main() { |
| k_ochiai | 0:b1c85511a5ca | 16 | |
| k_ochiai | 0:b1c85511a5ca | 17 | printf("Init\n"); |
| k_ochiai | 0:b1c85511a5ca | 18 | |
| k_ochiai | 0:b1c85511a5ca | 19 | printf("\r\nSetting up...\r\n"); |
| k_ochiai | 0:b1c85511a5ca | 20 | EthernetErr ethErr = eth.setup(); |
| k_ochiai | 0:b1c85511a5ca | 21 | if(ethErr) |
| k_ochiai | 0:b1c85511a5ca | 22 | { |
| k_ochiai | 0:b1c85511a5ca | 23 | printf("Error %d in setup.\n", ethErr); |
| k_ochiai | 0:b1c85511a5ca | 24 | return -1; |
| k_ochiai | 0:b1c85511a5ca | 25 | } |
| k_ochiai | 0:b1c85511a5ca | 26 | printf("\r\nSetup OK\r\n"); |
| k_ochiai | 0:b1c85511a5ca | 27 | |
| k_ochiai | 0:b1c85511a5ca | 28 | HTTPClient twitter; |
| k_ochiai | 0:b1c85511a5ca | 29 | |
| k_ochiai | 0:b1c85511a5ca | 30 | HTTPMap msg; |
| k_ochiai | 0:b1c85511a5ca | 31 | //msg["status"] = "I am tweeting from my mbed!"; //A good example of Key/Value pair use with Web APIs |
| k_ochiai | 0:b1c85511a5ca | 32 | LocalFileSystem local("local"); |
| k_ochiai | 0:b1c85511a5ca | 33 | char s[256]; |
| k_ochiai | 0:b1c85511a5ca | 34 | FILE *fp; |
| k_ochiai | 0:b1c85511a5ca | 35 | |
| k_ochiai | 0:b1c85511a5ca | 36 | printf("\r\nreading a message file.\r\n"); |
| k_ochiai | 0:b1c85511a5ca | 37 | |
| k_ochiai | 0:b1c85511a5ca | 38 | if(NULL == (fp = fopen("/local/tweet.txt","r")) ) { |
| k_ochiai | 0:b1c85511a5ca | 39 | printf("\r\nError: The message file cannot be accessed\r\n"); |
| k_ochiai | 0:b1c85511a5ca | 40 | return -1; |
| k_ochiai | 0:b1c85511a5ca | 41 | } |
| k_ochiai | 0:b1c85511a5ca | 42 | |
| k_ochiai | 0:b1c85511a5ca | 43 | fgets(s,256,fp); |
| k_ochiai | 0:b1c85511a5ca | 44 | fclose(fp); |
| k_ochiai | 0:b1c85511a5ca | 45 | |
| k_ochiai | 0:b1c85511a5ca | 46 | msg["status"] = s; |
| k_ochiai | 0:b1c85511a5ca | 47 | |
| k_ochiai | 0:b1c85511a5ca | 48 | twitter.basicAuth("user", "pass"); //We use basic authentication, replace with you account's parameters |
| k_ochiai | 0:b1c85511a5ca | 49 | |
| k_ochiai | 0:b1c85511a5ca | 50 | //No need to retieve data sent back by the server |
| k_ochiai | 0:b1c85511a5ca | 51 | HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL); |
| k_ochiai | 0:b1c85511a5ca | 52 | if( r == HTTP_OK ) |
| k_ochiai | 0:b1c85511a5ca | 53 | { |
| k_ochiai | 0:b1c85511a5ca | 54 | printf("Tweet sent with success!\n"); |
| k_ochiai | 0:b1c85511a5ca | 55 | } |
| k_ochiai | 0:b1c85511a5ca | 56 | else |
| k_ochiai | 0:b1c85511a5ca | 57 | { |
| k_ochiai | 0:b1c85511a5ca | 58 | printf("Problem during tweeting, return code %d\n", r); |
| k_ochiai | 0:b1c85511a5ca | 59 | } |
| k_ochiai | 0:b1c85511a5ca | 60 | |
| k_ochiai | 0:b1c85511a5ca | 61 | return 0; |
| k_ochiai | 0:b1c85511a5ca | 62 | |
| k_ochiai | 0:b1c85511a5ca | 63 | } |