Skip to content

Commit

Permalink
getting started with time
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Nov 24, 2022
1 parent 6dd5c82 commit e356638
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
22 changes: 22 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -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 = [email protected] ; https://registry.platformio.org/platforms/platformio/espressif32/versions
board = esp32dev
build_flags = -DCORE_DEBUG_LEVEL=0
monitor_speed = 115200
lib_deps =
93 changes: 93 additions & 0 deletions src/a.ino
Original file line number Diff line number Diff line change
@@ -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 <WiFi.h>

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();
}

0 comments on commit e356638

Please sign in to comment.