New

Dependencies:   EthernetNetIf TextLCD mbed HTTPClient_ToBeRemoved

Committer:
jksoft
Date:
Sat Aug 11 14:26:14 2012 +0000
Revision:
0:5fdf32706674
Rev2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:5fdf32706674 1 #include "mbed.h"
jksoft 0:5fdf32706674 2 #include "EthernetNetIf.h"
jksoft 0:5fdf32706674 3 #include "TextLCD.h"
jksoft 0:5fdf32706674 4 #include "HTTPClient.h"
jksoft 0:5fdf32706674 5
jksoft 0:5fdf32706674 6 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d4-d7
jksoft 0:5fdf32706674 7
jksoft 0:5fdf32706674 8 EthernetNetIf eth;
jksoft 0:5fdf32706674 9 HTTPClient http;
jksoft 0:5fdf32706674 10
jksoft 0:5fdf32706674 11 int j_paser( const char *buf , char *word , char *out )
jksoft 0:5fdf32706674 12 {
jksoft 0:5fdf32706674 13 int i = 0;
jksoft 0:5fdf32706674 14 char *p;
jksoft 0:5fdf32706674 15 char _word[64] = "\"\0";
jksoft 0:5fdf32706674 16
jksoft 0:5fdf32706674 17 strcat(_word , word );
jksoft 0:5fdf32706674 18 strcat(_word , "\"" );
jksoft 0:5fdf32706674 19
jksoft 0:5fdf32706674 20 p = strstr( (char*)buf , _word ) + 2 + strlen(_word);
jksoft 0:5fdf32706674 21
jksoft 0:5fdf32706674 22 while( (p[i] != ',')&&(p[i] != '\n') )
jksoft 0:5fdf32706674 23 {
jksoft 0:5fdf32706674 24 out[i] = p[i];
jksoft 0:5fdf32706674 25 i++;
jksoft 0:5fdf32706674 26 }
jksoft 0:5fdf32706674 27 out[i] = '\0';
jksoft 0:5fdf32706674 28
jksoft 0:5fdf32706674 29 return(i);
jksoft 0:5fdf32706674 30 }
jksoft 0:5fdf32706674 31
jksoft 0:5fdf32706674 32 int main(void) {
jksoft 0:5fdf32706674 33
jksoft 0:5fdf32706674 34 char year[32];
jksoft 0:5fdf32706674 35 char month[32];
jksoft 0:5fdf32706674 36 char day[32];
jksoft 0:5fdf32706674 37 char hour[32];
jksoft 0:5fdf32706674 38 char usage[32];
jksoft 0:5fdf32706674 39 char capacity[32];
jksoft 0:5fdf32706674 40
jksoft 0:5fdf32706674 41 EthernetErr ethErr = eth.setup();
jksoft 0:5fdf32706674 42 if(ethErr)
jksoft 0:5fdf32706674 43 {
jksoft 0:5fdf32706674 44 lcd.locate(0,0);
jksoft 0:5fdf32706674 45 lcd.printf("Ethernet err.");
jksoft 0:5fdf32706674 46 return(-1);
jksoft 0:5fdf32706674 47 }
jksoft 0:5fdf32706674 48
jksoft 0:5fdf32706674 49 lcd.locate(0,0);
jksoft 0:5fdf32706674 50 lcd.printf("Getting info.");
jksoft 0:5fdf32706674 51
jksoft 0:5fdf32706674 52 HTTPText txt;
jksoft 0:5fdf32706674 53
jksoft 0:5fdf32706674 54 HTTPResult r = http.get("http://tepco-usage-api.appspot.com/latest.json", &txt);
jksoft 0:5fdf32706674 55
jksoft 0:5fdf32706674 56 if(r==HTTP_OK)
jksoft 0:5fdf32706674 57 {
jksoft 0:5fdf32706674 58 j_paser(txt.gets() , "year" , year);
jksoft 0:5fdf32706674 59 j_paser(txt.gets() , "month" , month);
jksoft 0:5fdf32706674 60 j_paser(txt.gets() , "day" , day);
jksoft 0:5fdf32706674 61 j_paser(txt.gets() , "hour" , hour);
jksoft 0:5fdf32706674 62 j_paser(txt.gets() , "usage" , usage);
jksoft 0:5fdf32706674 63 j_paser(txt.gets() , "capacity" , capacity);
jksoft 0:5fdf32706674 64
jksoft 0:5fdf32706674 65 lcd.locate(0,0);
jksoft 0:5fdf32706674 66 lcd.printf("%s/%s/%s %sh",year,month,day,hour);
jksoft 0:5fdf32706674 67 lcd.locate(0,1);
jksoft 0:5fdf32706674 68 lcd.printf("%s/%s %3.1f%%",usage,capacity,(((float)atoi(usage)/(float)atoi(capacity))*100.0));
jksoft 0:5fdf32706674 69 }
jksoft 0:5fdf32706674 70 else
jksoft 0:5fdf32706674 71 {
jksoft 0:5fdf32706674 72 lcd.locate(0,0);
jksoft 0:5fdf32706674 73 lcd.printf("Http get err.");
jksoft 0:5fdf32706674 74 return(-1);
jksoft 0:5fdf32706674 75 }
jksoft 0:5fdf32706674 76
jksoft 0:5fdf32706674 77 while(1)
jksoft 0:5fdf32706674 78 {
jksoft 0:5fdf32706674 79
jksoft 0:5fdf32706674 80 }
jksoft 0:5fdf32706674 81
jksoft 0:5fdf32706674 82 return(0);
jksoft 0:5fdf32706674 83 }