Marutsu mbed seminar 11-09-2010

Dependencies:   EthernetNetIf mbed

Committer:
nxpfan
Date:
Sat Sep 11 08:19:56 2010 +0000
Revision:
0:2a57fec4e078

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxpfan 0:2a57fec4e078 1 /*
nxpfan 0:2a57fec4e078 2 Update: 21-06-2010
nxpfan 0:2a57fec4e078 3 The basic authentication service for twitter is going down at the end of the week.
nxpfan 0:2a57fec4e078 4 To continue using that program, the code has been updated to use http://supertweet.net which acts as an API proxy.
nxpfan 0:2a57fec4e078 5 Simply visit the website to setup your twitter account for this API.
nxpfan 0:2a57fec4e078 6 See: http://www.supertweet.net/about/documentation
nxpfan 0:2a57fec4e078 7 */
nxpfan 0:2a57fec4e078 8
nxpfan 0:2a57fec4e078 9 #include "mbed.h"
nxpfan 0:2a57fec4e078 10 #include "EthernetNetIf.h"
nxpfan 0:2a57fec4e078 11 #include "HTTPClient.h"
nxpfan 0:2a57fec4e078 12 #include "TextLCD.h"
nxpfan 0:2a57fec4e078 13
nxpfan 0:2a57fec4e078 14 TextLCD lcd(p24, p25, p26, p27, p28, p29); // rs, e, d0-d3
nxpfan 0:2a57fec4e078 15 //TextLCD lcd( p24, p26, p27, p28, p29, p30 ); // rs, e, d0-d3
nxpfan 0:2a57fec4e078 16
nxpfan 0:2a57fec4e078 17 EthernetNetIf eth;
nxpfan 0:2a57fec4e078 18
nxpfan 0:2a57fec4e078 19 int main() {
nxpfan 0:2a57fec4e078 20
nxpfan 0:2a57fec4e078 21 printf("Init\n"); lcd.locate( 0, 0 ); lcd.printf("Init ");
nxpfan 0:2a57fec4e078 22 printf("\r\nSetting up...\r\n"); lcd.locate( 0, 0 ); lcd.printf("Setting up ");
nxpfan 0:2a57fec4e078 23
nxpfan 0:2a57fec4e078 24 EthernetErr ethErr = eth.setup();
nxpfan 0:2a57fec4e078 25 if(ethErr)
nxpfan 0:2a57fec4e078 26 {
nxpfan 0:2a57fec4e078 27 printf("Error %d in setup.\n", ethErr); lcd.locate( 0, 0 ); lcd.printf("error %d", ethErr);
nxpfan 0:2a57fec4e078 28 return -1;
nxpfan 0:2a57fec4e078 29 }
nxpfan 0:2a57fec4e078 30 printf("\r\nSetup OK\r\n"); lcd.locate( 0, 0 ); lcd.printf("Setup OK ");
nxpfan 0:2a57fec4e078 31
nxpfan 0:2a57fec4e078 32 HTTPClient twitter;
nxpfan 0:2a57fec4e078 33
nxpfan 0:2a57fec4e078 34 HTTPMap msg;
nxpfan 0:2a57fec4e078 35
nxpfan 0:2a57fec4e078 36 //msg["status"] = "twitter test AAAA"; //A good example of Key/Value pair use with Web APIs
nxpfan 0:2a57fec4e078 37 LocalFileSystem local("local");
nxpfan 0:2a57fec4e078 38 char s[256];
nxpfan 0:2a57fec4e078 39 FILE *fp;
nxpfan 0:2a57fec4e078 40
nxpfan 0:2a57fec4e078 41 printf("\r\nreading a message file.\r\n");
nxpfan 0:2a57fec4e078 42
nxpfan 0:2a57fec4e078 43 if(NULL == (fp = fopen("/local/tweet.txt","r")) ) {
nxpfan 0:2a57fec4e078 44 printf("\r\nError: The message file cannot be accessed\r\n"); lcd.locate( 0, 0 ); lcd.printf("File access error");
nxpfan 0:2a57fec4e078 45 return -1;
nxpfan 0:2a57fec4e078 46 }
nxpfan 0:2a57fec4e078 47
nxpfan 0:2a57fec4e078 48 fgets(s,256,fp);
nxpfan 0:2a57fec4e078 49 fclose(fp);
nxpfan 0:2a57fec4e078 50
nxpfan 0:2a57fec4e078 51 msg["status"] = s; lcd.locate( 0, 0 ); lcd.printf("File read done ");
nxpfan 0:2a57fec4e078 52 twitter.basicAuth("USER_ID", "PASSWORD"); //We use basic authentication, replace with you account's parameters
nxpfan 0:2a57fec4e078 53
nxpfan 0:2a57fec4e078 54 //No need to retieve data sent back by the server
nxpfan 0:2a57fec4e078 55 HTTPResult r = twitter.post("http://api.supertweet.net/1/statuses/update.xml", msg, NULL);
nxpfan 0:2a57fec4e078 56 if( r == HTTP_OK )
nxpfan 0:2a57fec4e078 57 {
nxpfan 0:2a57fec4e078 58 printf("Tweet sent with success!\n"); lcd.locate( 0, 0 ); lcd.printf("Done ");
nxpfan 0:2a57fec4e078 59 }
nxpfan 0:2a57fec4e078 60 else
nxpfan 0:2a57fec4e078 61 {
nxpfan 0:2a57fec4e078 62 printf("Problem during tweeting, return code %d\n", r);lcd.locate( 0, 0 ); lcd.printf("Got error ");
nxpfan 0:2a57fec4e078 63 }
nxpfan 0:2a57fec4e078 64
nxpfan 0:2a57fec4e078 65 return 0;
nxpfan 0:2a57fec4e078 66
nxpfan 0:2a57fec4e078 67 }