NTP example

Dependencies:   NTPClient WiflyInterface mbed

Committer:
kaushalpkk
Date:
Thu Jul 12 17:27:04 2018 +0000
Revision:
2:a2c9e5deaf47
Parent:
1:8b6ea44e02bb
commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:8b6ea44e02bb 1 #include "mbed.h"
samux 1:8b6ea44e02bb 2 #include "WiflyInterface.h"
samux 1:8b6ea44e02bb 3 #include "NTPClient.h"
samux 1:8b6ea44e02bb 4
samux 1:8b6ea44e02bb 5 /* wifly interface:
samux 1:8b6ea44e02bb 6 * - p9 and p10 are for the serial communication
samux 1:8b6ea44e02bb 7 * - p19 is for the reset pin
samux 1:8b6ea44e02bb 8 * - p26 is for the connection status
samux 1:8b6ea44e02bb 9 * - "mbed" is the ssid of the network
samux 1:8b6ea44e02bb 10 * - "password" is the password
samux 1:8b6ea44e02bb 11 * - WPA is the security
samux 1:8b6ea44e02bb 12 */
kaushalpkk 2:a2c9e5deaf47 13
kaushalpkk 2:a2c9e5deaf47 14 WiflyInterface wifly(p9, p10, p30, p29, "new_frontiers", "asd", NONE);
samux 1:8b6ea44e02bb 15 NTPClient ntp;
samux 1:8b6ea44e02bb 16
samux 1:8b6ea44e02bb 17 int main()
samux 1:8b6ea44e02bb 18 {
samux 1:8b6ea44e02bb 19 wifly.init(); //Use DHCP
samux 1:8b6ea44e02bb 20
kaushalpkk 2:a2c9e5deaf47 21 while (!wifly.connect()); // join the network
kaushalpkk 2:a2c9e5deaf47 22 printf("IP Address is %s\n\r", wifly.getIPAddress());
samux 1:8b6ea44e02bb 23
samux 1:8b6ea44e02bb 24 printf("Trying to update time...\r\n");
samux 1:8b6ea44e02bb 25 if (ntp.setTime("0.pool.ntp.org") == 0)
samux 1:8b6ea44e02bb 26 {
samux 1:8b6ea44e02bb 27 printf("Set time successfully\r\n");
samux 1:8b6ea44e02bb 28 time_t ctTime;
samux 1:8b6ea44e02bb 29 ctTime = time(NULL);
samux 1:8b6ea44e02bb 30 printf("Time is set to (UTC): %s\r\n", ctime(&ctTime));
samux 1:8b6ea44e02bb 31 }
samux 1:8b6ea44e02bb 32 else
samux 1:8b6ea44e02bb 33 {
samux 1:8b6ea44e02bb 34 printf("Error\r\n");
samux 1:8b6ea44e02bb 35 }
samux 1:8b6ea44e02bb 36
samux 1:8b6ea44e02bb 37 wifly.disconnect();
samux 1:8b6ea44e02bb 38
samux 1:8b6ea44e02bb 39 while(1) {
samux 1:8b6ea44e02bb 40 }
samux 0:42b732f6238c 41 }