#include <WiFi.h>

#define NTP_SERVER     "pool.ntp.org"
#define UTC_OFFSET     0
#define UTC_OFFSET_DST 0

void printLocalTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("NTP Connection Error");
    return;
  }

  Serial.println(&timeinfo, "Date: %d/%m/%Y");
  Serial.println(&timeinfo, "Time: %H:%M:%S %Z");
}

void setup() {
  // Wait for USB Serial to connect
  while (!Serial) delay(50);

  Serial.begin(115200);
  delay(10);

  Serial.print("Connecting to Wifi");

  WiFi.begin("Wokwi-GUEST", "");
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected! ");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Updating time from NTP server...");

  configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);

  printLocalTime();
}

void loop() {
  delay(100);
}
Loading
franzininho-wifi