MBED clock sync using NTP Server from from internet
Dependencies: EthernetInterface NTPClient mbed-rtos mbed
Fork of MbedClock by
main.cpp@11:4d4334d909d3, 2016-02-11 (annotated)
- Committer:
- ismaia
- Date:
- Thu Feb 11 13:42:13 2016 +0000
- Revision:
- 11:4d4334d909d3
- Parent:
- 10:d9ed3d95e5eb
- Child:
- 12:bcced6833b8f
Thread version RGB blinking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudanian | 0:4e6ae21cbd31 | 1 | #include "mbed.h" |
dudanian | 0:4e6ae21cbd31 | 2 | #include "rtos.h" |
dudanian | 0:4e6ae21cbd31 | 3 | #include "EthernetInterface.h" |
ismaia | 7:0a56268d858c | 4 | #include "NTPClient.h" |
dudanian | 0:4e6ae21cbd31 | 5 | #include <string> |
dudanian | 1:c47a2f0816bb | 6 | |
dudanian | 0:4e6ae21cbd31 | 7 | |
ismaia | 8:9693d69ec483 | 8 | EthernetInterface eth; |
dudanian | 2:c939d0501184 | 9 | TCPSocketConnection server; |
ismaia | 8:9693d69ec483 | 10 | NTPClient ntp; |
ismaia | 11:4d4334d909d3 | 11 | DigitalOut led1(LED1); |
ismaia | 11:4d4334d909d3 | 12 | DigitalOut led2(LED2); |
ismaia | 8:9693d69ec483 | 13 | time_t ctTime; |
ismaia | 7:0a56268d858c | 14 | |
ismaia | 11:4d4334d909d3 | 15 | |
ismaia | 11:4d4334d909d3 | 16 | void thread_func1(void const *args) { |
ismaia | 11:4d4334d909d3 | 17 | while (1) { |
ismaia | 11:4d4334d909d3 | 18 | led1 = !led1; |
ismaia | 11:4d4334d909d3 | 19 | Thread::wait(1000); |
ismaia | 11:4d4334d909d3 | 20 | } |
ismaia | 11:4d4334d909d3 | 21 | } |
ismaia | 11:4d4334d909d3 | 22 | |
ismaia | 11:4d4334d909d3 | 23 | void thread_func2(void const *args) { |
ismaia | 11:4d4334d909d3 | 24 | while (1) { |
ismaia | 11:4d4334d909d3 | 25 | led2 = !led2; |
ismaia | 11:4d4334d909d3 | 26 | Thread::wait(500); |
ismaia | 11:4d4334d909d3 | 27 | } |
ismaia | 11:4d4334d909d3 | 28 | } |
dudanian | 0:4e6ae21cbd31 | 29 | |
ismaia | 7:0a56268d858c | 30 | int main() { |
ismaia | 8:9693d69ec483 | 31 | printf("Setting up ethernet interface...\r\n"); |
ismaia | 8:9693d69ec483 | 32 | if (eth.init() == 0 ) { //Use DHCP |
ismaia | 8:9693d69ec483 | 33 | printf("Ethernet setup OK\r\n"); |
ismaia | 8:9693d69ec483 | 34 | }else { |
ismaia | 8:9693d69ec483 | 35 | printf("Error: cannot set ethernet interface\r\n"); |
ismaia | 8:9693d69ec483 | 36 | return 1; |
ismaia | 8:9693d69ec483 | 37 | } |
ismaia | 8:9693d69ec483 | 38 | |
ismaia | 8:9693d69ec483 | 39 | printf("Trying to connect...\r\n"); |
ismaia | 8:9693d69ec483 | 40 | wait(0.5); |
ismaia | 8:9693d69ec483 | 41 | if ( eth.connect(30000) == 0 ){ |
ismaia | 8:9693d69ec483 | 42 | printf("IP Address is %s\n", eth.getIPAddress()); |
ismaia | 8:9693d69ec483 | 43 | }else { |
ismaia | 8:9693d69ec483 | 44 | printf("Error: cannot set ethernet interface\r\n"); |
ismaia | 8:9693d69ec483 | 45 | return 1; |
ismaia | 8:9693d69ec483 | 46 | } |
ismaia | 8:9693d69ec483 | 47 | |
ismaia | 7:0a56268d858c | 48 | printf("Trying to update time...\r\n"); |
ismaia | 8:9693d69ec483 | 49 | if (ntp.setTime("0.fr.pool.ntp.org") == 0) |
ismaia | 7:0a56268d858c | 50 | { |
ismaia | 8:9693d69ec483 | 51 | printf("Set time successfully\r\n"); |
ismaia | 7:0a56268d858c | 52 | } |
ismaia | 7:0a56268d858c | 53 | else |
ismaia | 7:0a56268d858c | 54 | { |
ismaia | 8:9693d69ec483 | 55 | printf("Error: Cannot set time\r\n"); |
ismaia | 7:0a56268d858c | 56 | } |
ismaia | 7:0a56268d858c | 57 | eth.disconnect(); |
ismaia | 8:9693d69ec483 | 58 | ctTime = time(NULL); |
ismaia | 11:4d4334d909d3 | 59 | |
ismaia | 11:4d4334d909d3 | 60 | Thread thread1(thread_func1); |
ismaia | 11:4d4334d909d3 | 61 | Thread thread2(thread_func2); |
ismaia | 11:4d4334d909d3 | 62 | |
ismaia | 7:0a56268d858c | 63 | while(1) { |
ismaia | 11:4d4334d909d3 | 64 | printf("Current time is: %s\r\n", ctime(&ctTime)); |
ismaia | 7:0a56268d858c | 65 | wait(1); |
dudanian | 0:4e6ae21cbd31 | 66 | } |
dudanian | 0:4e6ae21cbd31 | 67 | } |