/*
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.
Modified by Panagiotis Toumpaniaris on 10/4/2025
*/
#include <WiFi.h>
#include "time.h"
const char* ssid = "Vodafone_2.4G-12329";
const char* password = "9XfndzdcCnPdXD6k";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7200;
const int daylightOffset_sec = 3600;
void setup(){
pinMode(2, OUTPUT);
Serial.begin(115200);
// digitalWrite(2,HIGH);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
//disconnect WiFi as it's no longer needed
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
}
void loop(){
delay(1000);
printLocalTime();
}
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");
int hh1=timeinfo.tm_hour;
Serial.print("hh1= ");
Serial.println(hh1);
// Serial.print("Day of week: ");
// Serial.print(&timeinfo, "%A");
// Serial.print("Month: ");
// Serial.print(&timeinfo, "%B");
// Serial.print("Day of Month: ");
// Serial.print(&timeinfo, "%d");
// Serial.println("Year: ");
// Serial.print(&timeinfo, "%Y");
// Serial.println("Hour: ");
Serial.print(&timeinfo, "%H");
// Serial.print("Hour (12 hour format): ");
// Serial.print(&timeinfo, "%I");
// Serial.print("Minute: ");
Serial.print(&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();
// int hh1=timeinfo.tm_hour;
int mm1=timeinfo.tm_min;
int ss1=timeinfo.tm_sec;
settimes(20,12,hh1, mm1, ss1) ;
settimes(8,15,hh1, mm1, ss1);
// set time2
settimes(9,0,hh1, mm1, ss1);
settimes(9,5,hh1, mm1, ss1);
// set time3
settimes(9,50,hh1, mm1, ss1);
settimes(10,0,hh1, mm1, ss1);
// set time4
settimes(10,45,hh1, mm1, ss1);
settimes(10,55,hh1, mm1, ss1);
// set time 5
settimes(11,40,hh1, mm1, ss1);
settimes(11,50,hh1, mm1, ss1);
// set time 6
settimes(12,35,hh1, mm1, ss1);
settimes(12,40,hh1, mm1, ss1);
// set time 7
settimes(13,25,hh1, mm1, ss1);
settimes(13,30,hh1, mm1, ss1);
//set time 8
settimes(14,10,hh1, mm1, ss1);
}
void settimes(int h1,int m1,int hh,int mm,int ss) {
struct tm timeinfo;
if(h1==hh and m1==mm) {
if (ss > 0 & ss<5) {
digitalWrite(2,HIGH);
// delay(1000);
}
else if(ss < 2 or ss >4){
digitalWrite(2,LOW);
delay(100);
}
}
}
Σχόλια
Δημοσίευση σχολίου