I'm attempting to get the current time from a server using ntp.
I was finding that time being printed was in the 1970s. When I added in some lines in the setup to debug what was happening (below) I found that the arduino nano esp32 was not connecting to wifi (it was printing '.')
I'm connecting to eduroam
#include <WiFi.h>
#include "time.h"
#include "esp_wpa2.h"
const char* ssid = "eduroam";
const char* identity= "id@email.ac.uk";
const char* username= "id@email.ac.uk";
const char* password = "password";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
void setup() {
Serial.begin(9600);
delay(1000);
WiFi.disconnect(true);
WiFi.begin(ssid, WPA2_AUTH_PEAP, identity, username, password);
//WiFi.begin(ssid, WPA2_AUTH_PEAP, username, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
void loop() {
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%d %B %Y %H:%M:%S");
delay(1000);
}