Last active
July 31, 2024 18:24
-
-
Save maxpromer/cf0af6ba286cdc8d606ed87d614eedb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Rui Santos | |
Complete project details at https://RandomNerdTutorials.com/esp32-date-time-ntp-client-server-arduino/ | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
#include <Arduino.h> | |
#include <ATD3.5-S3.h> | |
#include <ETH.h> | |
#include <SPI.h> | |
#include "time.h" | |
#define ETH_PHY_TYPE ETH_PHY_W5500 | |
#define ETH_PHY_ADDR 1 | |
#define ETH_PHY_CS 38 | |
#define ETH_PHY_IRQ 40 | |
#define ETH_PHY_RST 39 | |
// SPI pins | |
#define ETH_SPI_SCK 12 | |
#define ETH_SPI_MISO 13 | |
#define ETH_SPI_MOSI 11 | |
static bool eth_connected = false; | |
void onEvent(arduino_event_id_t event, arduino_event_info_t info) { | |
switch (event) { | |
case ARDUINO_EVENT_ETH_START: | |
Serial.println("ETH Started"); | |
//set eth hostname here | |
ETH.setHostname("esp32-eth0"); | |
break; | |
case ARDUINO_EVENT_ETH_CONNECTED: Serial.println("ETH Connected"); break; | |
case ARDUINO_EVENT_ETH_GOT_IP: Serial.printf("ETH Got IP: '%s'\n", esp_netif_get_desc(info.got_ip.esp_netif)); Serial.println(ETH); | |
#if USE_TWO_ETH_PORTS | |
Serial.println(ETH1); | |
#endif | |
eth_connected = true; | |
break; | |
case ARDUINO_EVENT_ETH_LOST_IP: | |
Serial.println("ETH Lost IP"); | |
eth_connected = false; | |
break; | |
case ARDUINO_EVENT_ETH_DISCONNECTED: | |
Serial.println("ETH Disconnected"); | |
eth_connected = false; | |
break; | |
case ARDUINO_EVENT_ETH_STOP: | |
Serial.println("ETH Stopped"); | |
eth_connected = false; | |
break; | |
default: break; | |
} | |
} | |
const char* ntpServer = "pool.ntp.org"; | |
const long gmtOffset_sec = 0; | |
const int daylightOffset_sec = 3600; | |
void printLocalTime(){ | |
struct tm timeinfo; | |
if(!getLocalTime(&timeinfo)){ | |
Serial.println("Failed to obtain time"); | |
return; | |
} | |
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); | |
Serial.print("Day of week: "); | |
Serial.println(&timeinfo, "%A"); | |
Serial.print("Month: "); | |
Serial.println(&timeinfo, "%B"); | |
Serial.print("Day of Month: "); | |
Serial.println(&timeinfo, "%d"); | |
Serial.print("Year: "); | |
Serial.println(&timeinfo, "%Y"); | |
Serial.print("Hour: "); | |
Serial.println(&timeinfo, "%H"); | |
Serial.print("Hour (12 hour format): "); | |
Serial.println(&timeinfo, "%I"); | |
Serial.print("Minute: "); | |
Serial.println(&timeinfo, "%M"); | |
Serial.print("Second: "); | |
Serial.println(&timeinfo, "%S"); | |
Serial.println("Time variables"); | |
char timeHour[3]; | |
strftime(timeHour,3, "%H", &timeinfo); | |
Serial.println(timeHour); | |
char timeWeekDay[10]; | |
strftime(timeWeekDay,10, "%A", &timeinfo); | |
Serial.println(timeWeekDay); | |
Serial.println(); | |
} | |
void setup(){ | |
Serial.begin(115200); | |
Network.onEvent(onEvent); | |
Display.begin(0); | |
SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI); | |
ETH.begin(ETH_PHY_TYPE, ETH_PHY_ADDR, ETH_PHY_CS, ETH_PHY_IRQ, ETH_PHY_RST, SPI); | |
while(!eth_connected) { | |
delay(500); | |
Serial.print("."); | |
} | |
// Init and get the time | |
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); | |
printLocalTime(); | |
} | |
void loop(){ | |
delay(1000); | |
printLocalTime(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment