Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
internet_clock/main.cpp@0:6a73d3dc037e, 2014-07-28 (annotated)
- Committer:
- ShawnHymel
- Date:
- Mon Jul 28 20:29:28 2014 +0000
- Revision:
- 0:6a73d3dc037e
Added main.cpp files from each of the demo programs. Libraries are not included.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ShawnHymel | 0:6a73d3dc037e | 1 | // Internet Clock with LCD based on the work by Jim Hamblen and Tyler Lisowski |
| ShawnHymel | 0:6a73d3dc037e | 2 | |
| ShawnHymel | 0:6a73d3dc037e | 3 | #include "mbed.h" |
| ShawnHymel | 0:6a73d3dc037e | 4 | #include "EthernetInterface.h" |
| ShawnHymel | 0:6a73d3dc037e | 5 | #include "NTPClient.h" |
| ShawnHymel | 0:6a73d3dc037e | 6 | #include "uLCD_4DGL.h" |
| ShawnHymel | 0:6a73d3dc037e | 7 | |
| ShawnHymel | 0:6a73d3dc037e | 8 | // Parameters |
| ShawnHymel | 0:6a73d3dc037e | 9 | char* domain_name = "0.uk.pool.ntp.org"; |
| ShawnHymel | 0:6a73d3dc037e | 10 | int port_number = 123; |
| ShawnHymel | 0:6a73d3dc037e | 11 | |
| ShawnHymel | 0:6a73d3dc037e | 12 | // Networking |
| ShawnHymel | 0:6a73d3dc037e | 13 | EthernetInterface eth; |
| ShawnHymel | 0:6a73d3dc037e | 14 | NTPClient ntp_client; |
| ShawnHymel | 0:6a73d3dc037e | 15 | |
| ShawnHymel | 0:6a73d3dc037e | 16 | // Graphic LCD - TX, RX, and RES pins |
| ShawnHymel | 0:6a73d3dc037e | 17 | uLCD_4DGL uLCD(p9,p10,p11); |
| ShawnHymel | 0:6a73d3dc037e | 18 | |
| ShawnHymel | 0:6a73d3dc037e | 19 | int main() { |
| ShawnHymel | 0:6a73d3dc037e | 20 | |
| ShawnHymel | 0:6a73d3dc037e | 21 | time_t ct_time; |
| ShawnHymel | 0:6a73d3dc037e | 22 | char time_buffer[80]; |
| ShawnHymel | 0:6a73d3dc037e | 23 | |
| ShawnHymel | 0:6a73d3dc037e | 24 | // Initialize LCD |
| ShawnHymel | 0:6a73d3dc037e | 25 | uLCD.baudrate(115200); |
| ShawnHymel | 0:6a73d3dc037e | 26 | uLCD.background_color(BLACK); |
| ShawnHymel | 0:6a73d3dc037e | 27 | uLCD.cls(); |
| ShawnHymel | 0:6a73d3dc037e | 28 | |
| ShawnHymel | 0:6a73d3dc037e | 29 | // Connect to network and wait for DHCP |
| ShawnHymel | 0:6a73d3dc037e | 30 | uLCD.locate(0,0); |
| ShawnHymel | 0:6a73d3dc037e | 31 | uLCD.printf("Getting IP Address\n"); |
| ShawnHymel | 0:6a73d3dc037e | 32 | eth.init(); |
| ShawnHymel | 0:6a73d3dc037e | 33 | if ( eth.connect() == -1 ) { |
| ShawnHymel | 0:6a73d3dc037e | 34 | uLCD.printf("ERROR: Could not\nget IP address"); |
| ShawnHymel | 0:6a73d3dc037e | 35 | return -1; |
| ShawnHymel | 0:6a73d3dc037e | 36 | } |
| ShawnHymel | 0:6a73d3dc037e | 37 | uLCD.printf("IP address is \n%s\n\n",eth.getIPAddress()); |
| ShawnHymel | 0:6a73d3dc037e | 38 | wait(1); |
| ShawnHymel | 0:6a73d3dc037e | 39 | |
| ShawnHymel | 0:6a73d3dc037e | 40 | // Read time from server |
| ShawnHymel | 0:6a73d3dc037e | 41 | uLCD.printf("Reading time...\n\r"); |
| ShawnHymel | 0:6a73d3dc037e | 42 | ntp_client.setTime(domain_name, port_number); |
| ShawnHymel | 0:6a73d3dc037e | 43 | uLCD.printf("Time set\n"); |
| ShawnHymel | 0:6a73d3dc037e | 44 | wait(2); |
| ShawnHymel | 0:6a73d3dc037e | 45 | eth.disconnect(); |
| ShawnHymel | 0:6a73d3dc037e | 46 | |
| ShawnHymel | 0:6a73d3dc037e | 47 | // Reset LCD |
| ShawnHymel | 0:6a73d3dc037e | 48 | uLCD.background_color(WHITE); |
| ShawnHymel | 0:6a73d3dc037e | 49 | uLCD.textbackground_color(WHITE); |
| ShawnHymel | 0:6a73d3dc037e | 50 | uLCD.color(RED); |
| ShawnHymel | 0:6a73d3dc037e | 51 | uLCD.cls(); |
| ShawnHymel | 0:6a73d3dc037e | 52 | uLCD.text_height(2); |
| ShawnHymel | 0:6a73d3dc037e | 53 | |
| ShawnHymel | 0:6a73d3dc037e | 54 | // Loop and update clock |
| ShawnHymel | 0:6a73d3dc037e | 55 | while (1) { |
| ShawnHymel | 0:6a73d3dc037e | 56 | uLCD.locate(0, 1); |
| ShawnHymel | 0:6a73d3dc037e | 57 | ct_time = time(NULL); |
| ShawnHymel | 0:6a73d3dc037e | 58 | strftime(time_buffer, 80, " %a %b %d\n %T %p %z\n %Z\n", \ |
| ShawnHymel | 0:6a73d3dc037e | 59 | localtime(&ct_time)); |
| ShawnHymel | 0:6a73d3dc037e | 60 | uLCD.printf(" UTC/GMT:\n%s", time_buffer); |
| ShawnHymel | 0:6a73d3dc037e | 61 | wait(0.1); |
| ShawnHymel | 0:6a73d3dc037e | 62 | } |
| ShawnHymel | 0:6a73d3dc037e | 63 | } |