nucho
/
RTCsetwithHttpClient
RTC set with the http client.
RTCsetwithHttpClient.cpp@0:d868e62f9a3b, 2010-06-05 (annotated)
- Committer:
- nucho
- Date:
- Sat Jun 05 16:42:53 2010 +0000
- Revision:
- 0:d868e62f9a3b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nucho | 0:d868e62f9a3b | 1 | #include "mbed.h" |
nucho | 0:d868e62f9a3b | 2 | #include "HTTPClient.h" |
nucho | 0:d868e62f9a3b | 3 | |
nucho | 0:d868e62f9a3b | 4 | #define PAGE_SIZE 77 |
nucho | 0:d868e62f9a3b | 5 | |
nucho | 0:d868e62f9a3b | 6 | HTTPClient http; |
nucho | 0:d868e62f9a3b | 7 | |
nucho | 0:d868e62f9a3b | 8 | int main(void) { |
nucho | 0:d868e62f9a3b | 9 | char result[PAGE_SIZE+1]; |
nucho | 0:d868e62f9a3b | 10 | char buf[40]; |
nucho | 0:d868e62f9a3b | 11 | char *e; |
nucho | 0:d868e62f9a3b | 12 | |
nucho | 0:d868e62f9a3b | 13 | time_t seconds; |
nucho | 0:d868e62f9a3b | 14 | |
nucho | 0:d868e62f9a3b | 15 | http.get("http://ntp-a1.nict.go.jp/cgi-bin/ntp", result,PAGE_SIZE); |
nucho | 0:d868e62f9a3b | 16 | result[PAGE_SIZE]='\0'; |
nucho | 0:d868e62f9a3b | 17 | |
nucho | 0:d868e62f9a3b | 18 | |
nucho | 0:d868e62f9a3b | 19 | int start_body=strstr(result,"<BODY>")-result+7; |
nucho | 0:d868e62f9a3b | 20 | int end_body = strstr(result,"</BODY>")-result-1; |
nucho | 0:d868e62f9a3b | 21 | strncpy(buf, result+start_body, end_body-start_body); |
nucho | 0:d868e62f9a3b | 22 | buf[end_body-start_body]='\0'; |
nucho | 0:d868e62f9a3b | 23 | |
nucho | 0:d868e62f9a3b | 24 | seconds = strtoul(buf,&e,10)-2208988800UL; |
nucho | 0:d868e62f9a3b | 25 | seconds += 32400;//JST TIME |
nucho | 0:d868e62f9a3b | 26 | set_time(seconds); |
nucho | 0:d868e62f9a3b | 27 | |
nucho | 0:d868e62f9a3b | 28 | // Work is done! |
nucho | 0:d868e62f9a3b | 29 | while (1) { |
nucho | 0:d868e62f9a3b | 30 | seconds = time(NULL); |
nucho | 0:d868e62f9a3b | 31 | strftime(buf,sizeof(buf),"%Y/%m/%d %a\n%H:%M:%S Ready!", localtime(&seconds)); |
nucho | 0:d868e62f9a3b | 32 | printf("%s\n", buf); |
nucho | 0:d868e62f9a3b | 33 | wait(1); |
nucho | 0:d868e62f9a3b | 34 | } |
nucho | 0:d868e62f9a3b | 35 | } |
nucho | 0:d868e62f9a3b | 36 |