cc3000_ntp_demo_F446RE
Dependencies: NTPClient2 NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed
Fork of cc3000_ntp_demo by
Diff: main.cpp
- Revision:
- 8:6870e6801838
- Parent:
- 6:4cc48f349aed
diff -r 1c08892623a2 -r 6870e6801838 main.cpp --- a/main.cpp Tue Jun 03 08:41:53 2014 +0000 +++ b/main.cpp Sat Oct 17 23:12:10 2015 +0000 @@ -18,20 +18,28 @@ #include "main.h" #include "NTPClient.h" +#define CC3000_IRQ D3 // (D3) +#define CC3000_EN D5 // (D5) +#define CC3000_CS D10 // (D10) +#define CC3000_MOSI D11 // (D11) +#define CC3000_MISO D12 // (D12) +#define CC3000_SCLK D13 // (D13) + +#define SSID "Prakjaroen" +#define PHRASE "A4B5C6D7E8F9" +#define SECURITY WPA2 + +#define IP "192.168.2.165" +#define MASK "255.255.255.0" +#define GW "192.168.2.1" +#define DHCP 1 + using namespace mbed_cc3000; -/* cc3000 module declaration specific for user's board. Check also init() */ -#if (MY_BOARD == WIGO) -cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "ssid", "key", WPA2, false); +#if defined TARGET_NUCLEO_F446RE +//cc3000 wifi(IRQ, EN, CS, SPI(MOSI, MISO, SCLK), SSID, PHRASE, SECURITY, false); +cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13), "Prakjaroen", "A4B5C6D7E8F9", WPA2, false); Serial pc(USBTX, USBRX); -#elif (MY_BOARD == WIFI_DIPCORTEX) -cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), "ssid", "key", WPA2, false); -Serial pc(UART_TX, UART_RX); -#elif (MY_BOARD == MBED_BOARD_EXAMPLE) -cc3000 wifi(p9, p10, p8, SPI(p5, p6, p7), "ssid", "key", WPA2, false); -Serial pc(USBTX, USBRX); -#else - #endif /** @@ -41,31 +49,72 @@ */ int main() { init(); /* board dependent init */ - pc.baud(115200); + pc.baud(230400); - printf("cc3000 NTP client demo. \r\n"); + printf("\r\n--------------------------------------------------------------------------------\r\n"); + printf("cc3000 NTP client demo.\r\n"); +#if (DHCP == 1) + printf("Initialize the interface using DHCP...\r\n"); + printf("wifi.init() "); wifi.init(); +#else + printf("Initialize the interface using a static IP address...\r\n"); + printf("wifi.init(%s, %s, %s) ", IP, MASK, GW); + wifi.init(IP, MASK, GW); +#endif + printf("done.\r\n"); + printf("wifi.connect() "); if (wifi.connect() == -1) { + printf("failed.\r\n"); printf("Failed to connect. Please verify connection details and try again. \r\n"); } else { - printf("IP address: %s \r\n",wifi.getIPAddress()); + printf("done.\r\n"); + printf("IP address: %s \r\n", wifi.getIPAddress()); } - NTPClient ntp; + NTPClient ntp_client; + time_t ct_time; + char time_buffer[80]; + char time_buffer_old[80]; + wait(1); + + strcpy(time_buffer_old, ""); + + // Parameters + char* domain_name = "0.uk.pool.ntp.org"; + int port_number = 123; + + // Read time from server + printf("Reading time...\r\n"); + ntp_client.setTime(domain_name, port_number); + +// ct_time = time(NULL); +// strftime(time_buffer, 80, "%a %b %d %T %p %z %Z\n", localtime(&ct_time)); +// printf("UTC/GMT: %s\n", time_buffer); +// printf("UTC %s\n", ctime(&ct_time)); - printf("Getting time, 10s timeout. \r\n"); - if (ntp.setTime("0.uk.pool.ntp.org") == 0) - { - printf("Set time successfully. \r\n"); - time_t ctTime; - ctTime = time(NULL); - printf("Time is set to (UTC): %s \r\n", ctime(&ctTime)); + // Choose standard or daylight savings time, comment out other +// ct_time= time(NULL) + 3600; // Winter time - Convert to Europe/Amsterdam Time + ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time + set_time(ct_time); + +// ct_time = time(NULL); + strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time)); + printf("%s\r\n", time_buffer); + + while (1) { + ct_time = time(NULL); + strftime(time_buffer, 80, "%S", localtime(&ct_time)); + if (strcmp(time_buffer, time_buffer_old) != 0) { + strcpy(time_buffer_old, time_buffer); + strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time)); + printf("%s\r\n", time_buffer); + // Sync ones a day + strftime(time_buffer, 80, "%T", localtime(&ct_time)); + if (strcmp(time_buffer, "00:00:00") == 0) { + ntp_client.setTime(domain_name, port_number); + } + } } - else - { - printf("Error. \r\n"); - } - - wifi.disconnect(); }