NTPClient demo using ESP8266 driver library built in function. Original code from Jack Berkhout that works very fast, may depend on users connection, however I get the time back and RTC set within 200mS.

Dependencies:   mbed ESP8266-OS2

Committer:
star297
Date:
Thu Feb 14 00:40:25 2019 +0000
Revision:
1:b7cce2519e5e
Parent:
0:c284c33c96f1
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:c284c33c96f1 1 #include "mbed.h"
star297 0:c284c33c96f1 2 #include "ESP8266.h"
star297 0:c284c33c96f1 3
star297 0:c284c33c96f1 4 Serial pc(USBTX,USBRX);
star297 0:c284c33c96f1 5 ESP8266 esp(D10,D2,0); // tx, rx, debug (STM32F401RE)
star297 0:c284c33c96f1 6 DigitalOut led(LED1);
star297 0:c284c33c96f1 7
star297 0:c284c33c96f1 8 char ssid[32] = "ssid"; // enter your router ssid inside the quotes
star297 0:c284c33c96f1 9 char pwd [32] = "pwd"; // enter your router password inside the quotes
star297 0:c284c33c96f1 10 int Seconds;
star297 0:c284c33c96f1 11 char timebuf[60];
star297 0:c284c33c96f1 12 struct tm t;
star297 0:c284c33c96f1 13
star297 0:c284c33c96f1 14 Timer t1;
star297 0:c284c33c96f1 15
star297 0:c284c33c96f1 16 int main() {
star297 0:c284c33c96f1 17
star297 0:c284c33c96f1 18 pc.baud(460800); //set a fast terminal (9600 Mbed default)
star297 0:c284c33c96f1 19
star297 0:c284c33c96f1 20 pc.printf("\033[0m\033[2J\033[H\n ----- NTP Client to set RTC -----\r\n\n\n");
star297 0:c284c33c96f1 21 pc.printf("----- NTP can take up to 30 Seconds to aquire -----\r\n\n\n");
star297 0:c284c33c96f1 22 pc.printf("Initialise!\r\n\n");
star297 0:c284c33c96f1 23 pc.printf("ESP8266 Firmware:\r\n%s\r\n\n", esp.getFirmware());
star297 0:c284c33c96f1 24 time_t seconds = time(NULL);
star297 0:c284c33c96f1 25 strftime(timebuf,100,"Initial RTC Time= %H:%M:%S Date= %a %d %b %Y", localtime(&seconds));
star297 0:c284c33c96f1 26 pc.printf("\r\n%s\r\n\n\n",timebuf);
star297 0:c284c33c96f1 27 pc.printf("Checking if AUTOCONNECT with last settings\r\n\n");
star297 0:c284c33c96f1 28 wait(2); // allow time for ESP to autoconnect
star297 0:c284c33c96f1 29 esp.startup(3);
star297 0:c284c33c96f1 30
star297 0:c284c33c96f1 31 while(1){
star297 0:c284c33c96f1 32
star297 0:c284c33c96f1 33 if(!esp.isConnected()){
star297 0:c284c33c96f1 34 printf("AUTOCONNECT failed\r\n\n");
star297 0:c284c33c96f1 35 printf("Connecting to AP ssid: %s, pwd: %s\r\n",ssid,pwd);
star297 0:c284c33c96f1 36 esp.connect(ssid, pwd);
star297 0:c284c33c96f1 37 }
star297 0:c284c33c96f1 38 pc.printf("Connected\r\n\n");
star297 0:c284c33c96f1 39 pc.printf("Wi-Fi IP - %s\r\n", esp._STAIP_buffer);
star297 0:c284c33c96f1 40 pc.printf("Wi-Fi RSSI: %d\r\n\n", esp.getRSSI());
star297 0:c284c33c96f1 41 pc.printf("Get NTP time...\r\n\n\n");
star297 0:c284c33c96f1 42 t1.reset();t1.start();
star297 0:c284c33c96f1 43 Seconds = esp.getNTP("1.nl.pool.ntp.org",3600,1);
star297 0:c284c33c96f1 44 t1.stop();
star297 0:c284c33c96f1 45 if (Seconds>0){
star297 0:c284c33c96f1 46 pc.printf("\033[1;36m Seconds since 1970: %d\r\n\n", Seconds);
star297 0:c284c33c96f1 47 pc.printf("\033[0m NTP response... %f Seconds\r\n\n\n",t1.read());
star297 0:c284c33c96f1 48 }
star297 0:c284c33c96f1 49 else {pc.printf("\033[1;36m Error\r\n\n");}
star297 0:c284c33c96f1 50
star297 0:c284c33c96f1 51 pc.printf(" Time: Date:\r\n");
star297 0:c284c33c96f1 52 pc.printf("\033[1;32m\r\n");
star297 0:c284c33c96f1 53 for (unsigned i = 0; i < 10; i++) {
star297 0:c284c33c96f1 54 time_t t = time(NULL);
star297 0:c284c33c96f1 55 strftime(timebuf,100," %H:%M:%S %a %d %b %Y", localtime(&t));
star297 0:c284c33c96f1 56 pc.printf("\033[2K\033[1A%s\r\n",timebuf);
star297 0:c284c33c96f1 57 while(t==time(NULL)){led=!led;wait(.1);}
star297 0:c284c33c96f1 58 }
star297 0:c284c33c96f1 59 pc.printf("\033[0m\033[2J\033[H\n\nRestart every 10 seconds\r\n\n");
star297 0:c284c33c96f1 60 }
star297 0:c284c33c96f1 61 }