RTC set with the http client.

Dependencies:   mbed lwip

RTCsetwithHttpClient.cpp

Committer:
nucho
Date:
2010-06-05
Revision:
0:d868e62f9a3b

File content as of revision 0:d868e62f9a3b:

#include "mbed.h"
#include "HTTPClient.h"

#define PAGE_SIZE 77

HTTPClient http;

int main(void) {
    char result[PAGE_SIZE+1];
    char buf[40];
    char *e;

    time_t seconds;

    http.get("http://ntp-a1.nict.go.jp/cgi-bin/ntp", result,PAGE_SIZE);
    result[PAGE_SIZE]='\0';

    
    int start_body=strstr(result,"<BODY>")-result+7;
    int end_body = strstr(result,"</BODY>")-result-1;
    strncpy(buf, result+start_body, end_body-start_body);
    buf[end_body-start_body]='\0';
    
    seconds = strtoul(buf,&e,10)-2208988800UL;
    seconds += 32400;//JST TIME
    set_time(seconds);

    // Work is done!
    while (1) {
        seconds = time(NULL);
        strftime(buf,sizeof(buf),"%Y/%m/%d %a\n%H:%M:%S  Ready!", localtime(&seconds));
        printf("%s\n", buf);
         wait(1);
    }
}