The Smart Watch using SSD1306 and I2C. The Time obtain using SNTP protocol from NTP Server.

Dependencies:   WIZnetInterface SNTPClinet mbed-dev

Prerequisite

This example is to obtain a time from NTP server using SNTP protocol.

To implement this function, you need a Platform board and OLED.

Below are what we used.

  • WIZwiki-W7500 from WIZnet (Platform board)
  • OLED from WIZnet

Hardware Configuration

WIZwiki-W7500 Pin map

pin map

OLED (from WIZnet)

/media/uploads/stkim92/oled.png

Wiring Table

OLEDW7500
SCLPA_9
SDAPA_10
GNDGND
VCCVCC

Software

Define Pins

SSD1306.h

#if defined(TARGET_WIZwiki_W7500)
#define SDA                  PA_10
#define SCL                  PA_9
#endif

Connect to NTP server

main.cpp

SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
sntp.connect();

Get Time

main.cpp

if(sntp.getTime(&ntptime) == true)
    {
        pc.printf("%d-%d-%d, %d:%d:%d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
        wait(1.0);
    }
    else
    {
        while(sntp.getTime(&ntptime) != true)
        {
            //break;
        }
    }

Caution

watchout pins setting

Revision:
1:f0c1fe04502c
Parent:
0:6e799e373101
Child:
2:25b14c11c06c
--- a/main.cpp	Fri Jun 26 00:56:23 2015 +0000
+++ b/main.cpp	Thu Apr 13 01:14:01 2017 +0000
@@ -3,7 +3,6 @@
 #include "SNTPClient.h"
 #include "SSD1306.h"
 
-
 #if defined(TARGET_WIZwiki_W7500)
     EthernetInterface eth;
  /*   DigitalOut led1(LED1);
@@ -11,7 +10,7 @@
     DigitalOut led3(LED3);
     DigitalOut led4(LED4); */
     DigitalIn  sw(PC_0);
-#elif 
+
 
 #endif
 TCPSocketConnection sock;
@@ -26,52 +25,50 @@
 
 int main()
 {
-  uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
-  uint8_t count=0;
-  uint8_t time_x_offset =0;
-  uint8_t old_offset =0;
-
-  
+    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 
+    uint8_t count=0;
+    uint8_t time_x_offset =0;
+    uint8_t old_offset =0;
+    char buffer[32];
+    
+    pc.baud(9600);
     init();
     cls();
     OLED_DrawBMP(0,0,128,8,(unsigned char *)wiznet);
     wait(1);
-     cls();
+    cls();
     eth.init(mac_addr); //Use DHCP
     eth.connect();
-    
-   
-    printf("IP Address is %s\n\r", eth.getIPAddress());
-
+  
+    pc.printf("IP Address is %s\n\r", eth.getIPAddress());
     SNTPClient sntp("time.nist.gov", 40);   // timezone: Korea, Republic of
     sntp.connect();
     if(sntp.getTime(&ntptime) == true)
     {
-  //          printf("%d-%d-%d, %d:%d:%d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
- //           wait(1.0);
+        pc.printf("%d-%d-%d, %d:%d:%d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss);
+        wait(1.0);
     }
     else
     {
-        while(sntp.getTime(&ntptime) == true)
+        while(sntp.getTime(&ntptime) != true)
         {
-            break;
+            //break;
         }
     }
 
-      timeinfo.tm_mon = ntptime.mo-1;
-      timeinfo.tm_mday = ntptime.dd;
-      timeinfo.tm_hour = ntptime.hh;
-      timeinfo.tm_min = ntptime.mm;
-      timeinfo.tm_sec = ntptime.ss;
-      timeinfo.tm_year = ntptime.yy-1900;
-     // printf("%d-%d, %d:%d:%d\r\n", timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
-      time_t t =mktime(&timeinfo);
+    timeinfo.tm_mon = ntptime.mo-1;
+    timeinfo.tm_mday = ntptime.dd;
+    timeinfo.tm_hour = ntptime.hh;
+    timeinfo.tm_min = ntptime.mm;
+    timeinfo.tm_sec = ntptime.ss;
+    timeinfo.tm_year = ntptime.yy-1900;
+    // printf("%d-%d, %d:%d:%d\r\n", timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
+    time_t t =mktime(&timeinfo);
             
-      set_time(t);
-      t = time(NULL);
+    set_time(t);
+    t = time(NULL);
      
     while (1) {
-         char buffer[32];
         time_t seconds = time(NULL);
               
        // printf("Time as a basic string = %s\r\n", ctime(&seconds));
@@ -85,10 +82,7 @@
            time_x_offset =0;              
            if(old_offset != time_x_offset){
                old_offset = time_x_offset;
-            
-               }
-
-
+            }
             //strftime (buffer,32,"%I:%M:%S ",localtime(&seconds));
             strftime (buffer,32,"%I:%M",localtime(&seconds));
             LED_P23x32Str(time_x_offset,3,buffer);
@@ -97,25 +91,17 @@
             if(sw == 0)
             {
                 count = 0;   
-                cls();
-            
+                cls();            
             }
         
         }
         else
-        {
-           
-             strftime(buffer, 32,"%a %d %b %Y", localtime(&seconds));
-             OLED_ShowStr(10, 3, buffer,1 );
-             strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
-             OLED_ShowStr(10, 4, buffer,2 );
-             count ++;
+        {   
+            strftime(buffer, 32,"%a %d %b %Y", localtime(&seconds));
+            OLED_ShowStr(10, 3, buffer,1 );
+            strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds));
+            OLED_ShowStr(10, 4, buffer,2 );
+            count ++;
         }
-         
-         
-         
-         
-
     }
-
 }