Jack Berkhout / Mbed 2 deprecated ArchMax_CC3000_NTP

Dependencies:   NTPClient3 NVIC_set_all_priorities cc3000_hostdriver_mbedsocket mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "main.h"
00003 //#include "EthernetInterface.h"
00004 #include "NTPClient.h"
00005 //#include "NetworkAPI/buffer.hpp"
00006 //#include "NetworkAPI/tcp/socket.hpp"
00007 #include "cc3000.h"
00008 
00009 #define PORT 1234
00010 #define MAX_PENDING 1
00011 
00012 #define CC3000_IRQ   D3  // (D3)
00013 #define CC3000_EN    D5  // (D5)
00014 #define CC3000_CS    D10 // (D10)
00015 #define CC3000_MOSI  D11 // (D11)
00016 #define CC3000_MISO  D12 // (D12)
00017 #define CC3000_SCLK  D13 // (D13)
00018 
00019 #define SSID         "Prakjaroen"
00020 #define PHRASE       "A4B5C6D7E8F9"
00021 #define SECURITY     WPA2
00022 
00023 #define IP           "192.168.2.165"
00024 #define MASK         "255.255.255.0"
00025 #define GW           "192.168.2.1"
00026 #define DHCP         1
00027 
00028 Serial pc(USBTX, USBRX);
00029 
00030 using namespace mbed_cc3000;
00031 
00032 cc3000 wifi(CC3000_IRQ, CC3000_EN, CC3000_CS, SPI(CC3000_MOSI, CC3000_MISO, CC3000_SCLK), SSID, PHRASE, SECURITY, false); //SparkFun Board on Arduino pin definitions
00033 
00034 // array to store RM parameters from EEPROM
00035 unsigned char cRMParamsFromEeprom[128];
00036 
00037 // array to store MAC address from EEPROM
00038 unsigned char cMacFromEeprom[6];
00039 
00040 int main() {
00041     init(); /* board dependent init */
00042     pc.baud(230400);
00043     
00044     printf("\r\n--------------------------------------------------------------------------------\r\n");
00045     printf("cc3000 NTP client demo.\r\n");
00046 #if (DHCP == 1)
00047     printf("Initialize the interface using DHCP...\r\n");
00048     printf("wifi.init() ");
00049     wifi.init();
00050 #else
00051     printf("Initialize the interface using a static IP address...\r\n");
00052     printf("wifi.init(%s, %s, %s) ", IP, MASK, GW);
00053     wifi.init(IP, MASK, GW);
00054 #endif
00055     printf("done.\r\n");
00056     printf("wifi.connect() ");
00057     if (wifi.connect() == -1) {
00058         printf("failed.\r\n");
00059         printf("Failed to connect. Please verify connection details and try again. \r\n");
00060     } else {
00061         printf("done.\r\n");
00062         printf("IP address: %s \r\n", wifi.getIPAddress());
00063     }
00064 
00065     NTPClient ntp_client;
00066     time_t ct_time;
00067     char time_buffer[80];    
00068     char time_buffer_old[80];    
00069     wait(1);
00070 
00071     strcpy(time_buffer_old, "");
00072 
00073     // Parameters
00074     char* domain_name = "0.uk.pool.ntp.org";
00075     int   port_number = 123;
00076     
00077     // Read time from server
00078     printf("Reading time...\r\n");
00079     ntp_client.setTime(domain_name, port_number);
00080 
00081 //    ct_time = time(NULL);
00082 //    strftime(time_buffer, 80, "%a %b %d %T %p %z %Z\n", localtime(&ct_time));
00083 //    printf("UTC/GMT: %s\n", time_buffer);
00084 //    printf("UTC %s\n", ctime(&ct_time));
00085 
00086     // Choose standard or daylight savings time, comment out other
00087 //    ct_time= time(NULL) + 3600; // Winter time - Convert to Europe/Amsterdam Time
00088     ct_time = time(NULL) + 7200; // Summer time - Convert to Europe/Amsterdam Time
00089     set_time(ct_time);
00090 
00091 //    ct_time = time(NULL);
00092     strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
00093     printf("%s\r\n", time_buffer);
00094 
00095     while (1) {
00096         ct_time = time(NULL);
00097         strftime(time_buffer, 80, "%S", localtime(&ct_time));
00098         if (strcmp(time_buffer, time_buffer_old) != 0) {
00099             strcpy(time_buffer_old, time_buffer);
00100             strftime(time_buffer, 80, "%a %d-%b-%Y %T", localtime(&ct_time));
00101             printf("%s\r\n", time_buffer);
00102             // Sync ones a day
00103             strftime(time_buffer, 80, "%T", localtime(&ct_time));
00104             if (strcmp(time_buffer, "00:00:00") == 0) {
00105                 ntp_client.setTime(domain_name, port_number);
00106             }
00107         }
00108     }
00109 }
00110