Currently I'm trying to create a lap timer using a microcontroller ESP32. I'm thinking of using NTP server pool.ntp.org
to get a precision timing of several milliseconds.
I tried to do something like this
#include <WiFi.h>
#include "time.h"
String time_str;
time_t epoch;
const char* ssid = "SSID";
const char* password = "PASSWORD";
void setup() {
Serial.begin(115200);
Start_WiFi(ssid,password);
configTime(0, 0, "pool.ntp.org");
}
void loop() {
setenv("TZ", " WIB-7", 1);
Serial.println(" Jakarta Time = "+printLocalTime());
Serial.println();
delay(100);
}
String printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return "Time Error";
}
char output[80];
//epoch = mktime(&timeinfo);
//return epoch;
strftime(output, 80, "%d-%b-%y, %H:%M:%S", &timeinfo);
time_str = String(output);
return time_str;
}
int Start_WiFi(const char* ssid, const char* password){
int connAttempts = 0;
Serial.println("\r\nConnecting to: "+String(ssid));
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
if(connAttempts > 20) return -5;
connAttempts++;
}
return 1;
}
I would like to get millisecond precision from struct tm timeinfo
. I tried to use strftime
and mktime
but i realise that both strftime
and mktime
returns only up to second.