diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..2415548 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,22 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html +; + +[platformio] +default_envs = esp32dev +src_dir = src + +[env:esp32dev] +framework = arduino +platform = espressif32@5.2.0 ; https://registry.platformio.org/platforms/platformio/espressif32/versions +board = esp32dev +build_flags = -DCORE_DEBUG_LEVEL=0 +monitor_speed = 115200 +lib_deps = diff --git a/src/a.ino b/src/a.ino new file mode 100644 index 0000000..ce21931 --- /dev/null +++ b/src/a.ino @@ -0,0 +1,93 @@ +/* + 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 "time.h" +#include + +const char* ssid = "REPLACE_WITH_YOUR_SSID"; +const char* password = "REPLACE_WITH_YOUR_PASSWORD"; + +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = -3600 * 5; + +void setup() +{ + Serial.begin(115200); + + // Connect to Wi-Fi + Serial.print("Connecting to "); + Serial.println(ssid); + WiFi.begin("Brown-Guest"); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + Serial.println(""); + Serial.println("WiFi connected."); + + // Init and get the time + configTime(gmtOffset_sec, 3600, ntpServer); +} + +void loop() +{ + delay(1000); + struct tm timeinfo; + + if (!getLocalTime(&timeinfo)) { + Serial.println("Failed to obtain time"); + return; + } + time_t now; + time(&now); + Serial.println(now); + printLocalTime(); +} + +void printLocalTime() +{ + struct tm timeinfo; + if (!getLocalTime(&timeinfo)) { + Serial.println("Failed to obtain time"); + return; + } + time_t now; + + time(&now); + Serial.println(now); + + 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(); +}