The Smart Watch using SSD1306 and I2C. The Time obtain using SNTP protocol from NTP Server.
Dependencies: SNTPClinet WIZnetInterface mbed-src
Fork of SSD1306_smart_watch by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "SNTPClient.h" 00004 #include "SSD1306.h" 00005 00006 00007 #if defined(TARGET_WIZwiki_W7500) 00008 EthernetInterface eth; 00009 /* DigitalOut led1(LED1); 00010 DigitalOut led2(LED2); 00011 DigitalOut led3(LED3); 00012 DigitalOut led4(LED4); */ 00013 DigitalIn sw(PC_0); 00014 #elif 00015 00016 #endif 00017 TCPSocketConnection sock; 00018 00019 DigitalOut myled(PA_6); 00020 00021 Serial pc(USBTX, USBRX); // tx, rx 00022 00023 datetime ntptime; 00024 struct tm timeinfo; 00025 00026 00027 int main() 00028 { 00029 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02}; 00030 uint8_t count=0; 00031 uint8_t time_x_offset =0; 00032 uint8_t old_offset =0; 00033 00034 00035 init(); 00036 cls(); 00037 OLED_DrawBMP(0,0,128,8,(unsigned char *)wiznet); 00038 wait(1); 00039 cls(); 00040 eth.init(mac_addr); //Use DHCP 00041 eth.connect(); 00042 00043 00044 printf("IP Address is %s\n\r", eth.getIPAddress()); 00045 00046 SNTPClient sntp("time.nist.gov", 40); // timezone: Korea, Republic of 00047 sntp.connect(); 00048 if(sntp.getTime(&ntptime) == true) 00049 { 00050 // printf("%d-%d-%d, %d:%d:%d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); 00051 // wait(1.0); 00052 } 00053 else 00054 { 00055 while(sntp.getTime(&ntptime) == true) 00056 { 00057 break; 00058 } 00059 } 00060 00061 timeinfo.tm_mon = ntptime.mo-1; 00062 timeinfo.tm_mday = ntptime.dd; 00063 timeinfo.tm_hour = ntptime.hh; 00064 timeinfo.tm_min = ntptime.mm; 00065 timeinfo.tm_sec = ntptime.ss; 00066 timeinfo.tm_year = ntptime.yy-1900; 00067 // printf("%d-%d, %d:%d:%d\r\n", timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); 00068 time_t t =mktime(&timeinfo); 00069 00070 set_time(t); 00071 t = time(NULL); 00072 00073 while (1) { 00074 char buffer[32]; 00075 time_t seconds = time(NULL); 00076 00077 // printf("Time as a basic string = %s\r\n", ctime(&seconds)); 00078 if(count == 50) 00079 { 00080 cls(); 00081 count++; 00082 } 00083 if(count > 50) 00084 { 00085 time_x_offset =0; 00086 if(old_offset != time_x_offset){ 00087 old_offset = time_x_offset; 00088 00089 } 00090 00091 00092 //strftime (buffer,32,"%I:%M:%S ",localtime(&seconds)); 00093 strftime (buffer,32,"%I:%M",localtime(&seconds)); 00094 LED_P23x32Str(time_x_offset,3,buffer); 00095 00096 //intBigTime(buffer); 00097 if(sw == 0) 00098 { 00099 count = 0; 00100 cls(); 00101 00102 } 00103 00104 } 00105 else 00106 { 00107 00108 strftime(buffer, 32,"%a %d %b %Y", localtime(&seconds)); 00109 OLED_ShowStr(10, 3, buffer,1 ); 00110 strftime(buffer, 32, "%I:%M:%S %p", localtime(&seconds)); 00111 OLED_ShowStr(10, 4, buffer,2 ); 00112 count ++; 00113 } 00114 00115 00116 00117 00118 00119 } 00120 00121 }
Generated on Wed Jul 13 2022 19:48:13 by 1.7.2