![](/media/cache/profiles/b4fcae87a46c0afe6dd16d9cff8a209a.jpg.50x50_q85.jpg)
Network clock using 7 segment. add SHT11
Dependencies: SHTx SNTPClient WIZnetInterface mbed
main.cpp@0:179f2315915f, 2016-04-05 (annotated)
- Committer:
- nanjsk
- Date:
- Tue Apr 05 11:50:51 2016 +0000
- Revision:
- 0:179f2315915f
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nanjsk | 0:179f2315915f | 1 | #include "mbed.h" |
nanjsk | 0:179f2315915f | 2 | #include "segment.h" |
nanjsk | 0:179f2315915f | 3 | #include "EthernetInterface.h" |
nanjsk | 0:179f2315915f | 4 | #include "SNTPClient.h" |
nanjsk | 0:179f2315915f | 5 | #include "SHTx/sht15.hpp" |
nanjsk | 0:179f2315915f | 6 | |
nanjsk | 0:179f2315915f | 7 | Serial pc(USBTX,USBRX); |
nanjsk | 0:179f2315915f | 8 | EthernetInterface eth; |
nanjsk | 0:179f2315915f | 9 | datetime ntptime; |
nanjsk | 0:179f2315915f | 10 | |
nanjsk | 0:179f2315915f | 11 | InterruptIn SW1(P21); |
nanjsk | 0:179f2315915f | 12 | DigitalOut led1(LED1); |
nanjsk | 0:179f2315915f | 13 | DigitalOut led2(LED2); |
nanjsk | 0:179f2315915f | 14 | |
nanjsk | 0:179f2315915f | 15 | SHTx::SHT15 sensor(P22, P23); |
nanjsk | 0:179f2315915f | 16 | |
nanjsk | 0:179f2315915f | 17 | void SW1_Interrupt(); |
nanjsk | 0:179f2315915f | 18 | void SNTP_Connect(); |
nanjsk | 0:179f2315915f | 19 | void time_set(); |
nanjsk | 0:179f2315915f | 20 | void time_update(char buffer[]); |
nanjsk | 0:179f2315915f | 21 | float update_sht11(uint8_t sensor_sel); |
nanjsk | 0:179f2315915f | 22 | |
nanjsk | 0:179f2315915f | 23 | struct tm timeinfo; |
nanjsk | 0:179f2315915f | 24 | |
nanjsk | 0:179f2315915f | 25 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0xab}; |
nanjsk | 0:179f2315915f | 26 | uint8_t display_mode = 0; |
nanjsk | 0:179f2315915f | 27 | uint8_t update_mode = 0; |
nanjsk | 0:179f2315915f | 28 | uint8_t Set_update[3] = {18, 00, 00}; //Hour,Minute,Second |
nanjsk | 0:179f2315915f | 29 | uint8_t SW_toggle = 0; |
nanjsk | 0:179f2315915f | 30 | |
nanjsk | 0:179f2315915f | 31 | void SW1_Interrupt(){ |
nanjsk | 0:179f2315915f | 32 | //led2=!led2; |
nanjsk | 0:179f2315915f | 33 | display_mode = display_mode + 1; |
nanjsk | 0:179f2315915f | 34 | |
nanjsk | 0:179f2315915f | 35 | if(display_mode == 4) display_mode = 0; |
nanjsk | 0:179f2315915f | 36 | printf("Display Mode : %d\n\r",display_mode); |
nanjsk | 0:179f2315915f | 37 | } |
nanjsk | 0:179f2315915f | 38 | |
nanjsk | 0:179f2315915f | 39 | void SNTP_Connect() { |
nanjsk | 0:179f2315915f | 40 | printf("Getting time information by using NTP...\r\n"); |
nanjsk | 0:179f2315915f | 41 | |
nanjsk | 0:179f2315915f | 42 | SNTPClient sntp("time.nist.gov", 40); // timezone: Korea, Republic of |
nanjsk | 0:179f2315915f | 43 | sntp.connect(); |
nanjsk | 0:179f2315915f | 44 | if(sntp.getTime(&ntptime) == true) { |
nanjsk | 0:179f2315915f | 45 | printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); |
nanjsk | 0:179f2315915f | 46 | printf("Completed Get and Set Time\r\n\r\n"); |
nanjsk | 0:179f2315915f | 47 | led1 = 0; |
nanjsk | 0:179f2315915f | 48 | } |
nanjsk | 0:179f2315915f | 49 | else { |
nanjsk | 0:179f2315915f | 50 | while(sntp.getTime(&ntptime) == true) { |
nanjsk | 0:179f2315915f | 51 | printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); |
nanjsk | 0:179f2315915f | 52 | printf("Completed Get and Set Time\r\n\r\n"); |
nanjsk | 0:179f2315915f | 53 | led1 = 0; |
nanjsk | 0:179f2315915f | 54 | break; |
nanjsk | 0:179f2315915f | 55 | } |
nanjsk | 0:179f2315915f | 56 | } |
nanjsk | 0:179f2315915f | 57 | } |
nanjsk | 0:179f2315915f | 58 | void time_set() { |
nanjsk | 0:179f2315915f | 59 | timeinfo.tm_mon = ntptime.mo-1; |
nanjsk | 0:179f2315915f | 60 | timeinfo.tm_mday = ntptime.dd; |
nanjsk | 0:179f2315915f | 61 | timeinfo.tm_hour = ntptime.hh; |
nanjsk | 0:179f2315915f | 62 | timeinfo.tm_min = ntptime.mm; |
nanjsk | 0:179f2315915f | 63 | timeinfo.tm_sec = ntptime.ss; |
nanjsk | 0:179f2315915f | 64 | timeinfo.tm_year = ntptime.yy-1900; |
nanjsk | 0:179f2315915f | 65 | time_t t =mktime(&timeinfo); |
nanjsk | 0:179f2315915f | 66 | set_time(t); |
nanjsk | 0:179f2315915f | 67 | t = time(NULL); |
nanjsk | 0:179f2315915f | 68 | } |
nanjsk | 0:179f2315915f | 69 | |
nanjsk | 0:179f2315915f | 70 | void time_update(char buffer[]) { |
nanjsk | 0:179f2315915f | 71 | uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48); |
nanjsk | 0:179f2315915f | 72 | uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48); |
nanjsk | 0:179f2315915f | 73 | uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48); |
nanjsk | 0:179f2315915f | 74 | |
nanjsk | 0:179f2315915f | 75 | if(update_mode == 0){ |
nanjsk | 0:179f2315915f | 76 | if(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2]){ |
nanjsk | 0:179f2315915f | 77 | SNTP_Connect(); |
nanjsk | 0:179f2315915f | 78 | time_set(); |
nanjsk | 0:179f2315915f | 79 | update_mode = 1; |
nanjsk | 0:179f2315915f | 80 | printf("Time Update Completed.\n\r\n\r"); |
nanjsk | 0:179f2315915f | 81 | } |
nanjsk | 0:179f2315915f | 82 | } |
nanjsk | 0:179f2315915f | 83 | else if(update_mode == 1){ |
nanjsk | 0:179f2315915f | 84 | if(!(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2])){ |
nanjsk | 0:179f2315915f | 85 | update_mode = 0; |
nanjsk | 0:179f2315915f | 86 | } |
nanjsk | 0:179f2315915f | 87 | } |
nanjsk | 0:179f2315915f | 88 | } |
nanjsk | 0:179f2315915f | 89 | float update_sht11(uint8_t sensor_sel){ |
nanjsk | 0:179f2315915f | 90 | sensor.update(); |
nanjsk | 0:179f2315915f | 91 | sensor.setScale(false); |
nanjsk | 0:179f2315915f | 92 | if(sensor_sel == 0) return sensor.getTemperature(); |
nanjsk | 0:179f2315915f | 93 | else if(sensor_sel == 1) return sensor.getHumidity(); |
nanjsk | 0:179f2315915f | 94 | } |
nanjsk | 0:179f2315915f | 95 | |
nanjsk | 0:179f2315915f | 96 | int main() { |
nanjsk | 0:179f2315915f | 97 | pc.baud(115200); |
nanjsk | 0:179f2315915f | 98 | wait(0.1); |
nanjsk | 0:179f2315915f | 99 | printf("\n\rHello WIZwiki-W7500!\n\r"); |
nanjsk | 0:179f2315915f | 100 | printf("===========================================\n\r"); |
nanjsk | 0:179f2315915f | 101 | led1 = led2 = 1; |
nanjsk | 0:179f2315915f | 102 | |
nanjsk | 0:179f2315915f | 103 | Seg_Reset(); |
nanjsk | 0:179f2315915f | 104 | SW1.fall(&SW1_Interrupt); |
nanjsk | 0:179f2315915f | 105 | |
nanjsk | 0:179f2315915f | 106 | sensor.setOTPReload(false); |
nanjsk | 0:179f2315915f | 107 | sensor.setResolution(true); |
nanjsk | 0:179f2315915f | 108 | |
nanjsk | 0:179f2315915f | 109 | eth.init(mac_addr); //Use DHCP |
nanjsk | 0:179f2315915f | 110 | printf("Check Ethernet Link\r\n"); |
nanjsk | 0:179f2315915f | 111 | while(1) //Wait link up |
nanjsk | 0:179f2315915f | 112 | { |
nanjsk | 0:179f2315915f | 113 | if(eth.link() == true) |
nanjsk | 0:179f2315915f | 114 | break; |
nanjsk | 0:179f2315915f | 115 | } |
nanjsk | 0:179f2315915f | 116 | printf("Link up\r\n"); |
nanjsk | 0:179f2315915f | 117 | eth.connect(); |
nanjsk | 0:179f2315915f | 118 | printf("My IP Address is %s\r\n", eth.getIPAddress()); |
nanjsk | 0:179f2315915f | 119 | |
nanjsk | 0:179f2315915f | 120 | SNTP_Connect(); |
nanjsk | 0:179f2315915f | 121 | time_set(); |
nanjsk | 0:179f2315915f | 122 | |
nanjsk | 0:179f2315915f | 123 | //ymd_buffer[0]~[3] : Year |
nanjsk | 0:179f2315915f | 124 | //ymd_buffer[4]~[5] : Month |
nanjsk | 0:179f2315915f | 125 | //ymd_buffer[6]~[7] : Day |
nanjsk | 0:179f2315915f | 126 | char ymd_buffer[8]; |
nanjsk | 0:179f2315915f | 127 | //hms_buffer[0]~[1] : Hour |
nanjsk | 0:179f2315915f | 128 | //hms_buffer[2]~[3] : Minute |
nanjsk | 0:179f2315915f | 129 | //hms_buffer[4]~[5] : Second |
nanjsk | 0:179f2315915f | 130 | char hms_buffer[6]; |
nanjsk | 0:179f2315915f | 131 | |
nanjsk | 0:179f2315915f | 132 | float temp_val = 0; |
nanjsk | 0:179f2315915f | 133 | float humi_val = 0; |
nanjsk | 0:179f2315915f | 134 | int temp_mode = 0; |
nanjsk | 0:179f2315915f | 135 | int humi_mode = 0; |
nanjsk | 0:179f2315915f | 136 | |
nanjsk | 0:179f2315915f | 137 | while(1) { |
nanjsk | 0:179f2315915f | 138 | time_t seconds = time(NULL); |
nanjsk | 0:179f2315915f | 139 | |
nanjsk | 0:179f2315915f | 140 | strftime(hms_buffer, 6, "%H%M%S\n\r", localtime(&seconds)); |
nanjsk | 0:179f2315915f | 141 | strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds)); |
nanjsk | 0:179f2315915f | 142 | |
nanjsk | 0:179f2315915f | 143 | if(hms_buffer[4] == '0' && hms_buffer[5] == '0'){ |
nanjsk | 0:179f2315915f | 144 | time_update(hms_buffer); |
nanjsk | 0:179f2315915f | 145 | } |
nanjsk | 0:179f2315915f | 146 | if(display_mode == 0) { |
nanjsk | 0:179f2315915f | 147 | SW_toggle = 0; |
nanjsk | 0:179f2315915f | 148 | display_hms(hms_buffer); |
nanjsk | 0:179f2315915f | 149 | } |
nanjsk | 0:179f2315915f | 150 | else if(display_mode == 1) { |
nanjsk | 0:179f2315915f | 151 | SW_toggle = 0; |
nanjsk | 0:179f2315915f | 152 | display_ymd(ymd_buffer); |
nanjsk | 0:179f2315915f | 153 | } |
nanjsk | 0:179f2315915f | 154 | else if(display_mode == 2) { |
nanjsk | 0:179f2315915f | 155 | if(temp_mode == 0){ |
nanjsk | 0:179f2315915f | 156 | if(hms_buffer[5]%5 == 0){ |
nanjsk | 0:179f2315915f | 157 | temp_val = update_sht11(0); |
nanjsk | 0:179f2315915f | 158 | temp_mode = 1; |
nanjsk | 0:179f2315915f | 159 | } |
nanjsk | 0:179f2315915f | 160 | } |
nanjsk | 0:179f2315915f | 161 | if(temp_mode == 1){ |
nanjsk | 0:179f2315915f | 162 | if(hms_buffer[5]%5 != 0){ |
nanjsk | 0:179f2315915f | 163 | temp_mode = 0; |
nanjsk | 0:179f2315915f | 164 | } |
nanjsk | 0:179f2315915f | 165 | } |
nanjsk | 0:179f2315915f | 166 | display_temp(temp_val); |
nanjsk | 0:179f2315915f | 167 | } |
nanjsk | 0:179f2315915f | 168 | else if(display_mode == 3) { |
nanjsk | 0:179f2315915f | 169 | if(humi_mode == 0){ |
nanjsk | 0:179f2315915f | 170 | if(hms_buffer[5]%5 == 0){ |
nanjsk | 0:179f2315915f | 171 | humi_val = update_sht11(1); |
nanjsk | 0:179f2315915f | 172 | humi_mode = 1; |
nanjsk | 0:179f2315915f | 173 | } |
nanjsk | 0:179f2315915f | 174 | } |
nanjsk | 0:179f2315915f | 175 | if(temp_mode == 1){ |
nanjsk | 0:179f2315915f | 176 | if(hms_buffer[5]%5 != 0){ |
nanjsk | 0:179f2315915f | 177 | humi_mode = 0; |
nanjsk | 0:179f2315915f | 178 | } |
nanjsk | 0:179f2315915f | 179 | } |
nanjsk | 0:179f2315915f | 180 | display_humi(humi_val); |
nanjsk | 0:179f2315915f | 181 | } |
nanjsk | 0:179f2315915f | 182 | else { |
nanjsk | 0:179f2315915f | 183 | display_hms(hms_buffer); |
nanjsk | 0:179f2315915f | 184 | } |
nanjsk | 0:179f2315915f | 185 | //Seg_Test_2(); |
nanjsk | 0:179f2315915f | 186 | } |
nanjsk | 0:179f2315915f | 187 | } |