Can you tell me what I am missing? It doesn't make any difference if the #define NO_DEBUG is comented out or not, the Serial.prints always execute.
I am obviously missing an important point.
//Libraries
#include <ESP32Time.h>
#include <WiFi.h>
#include <time.h>
//#define NO_DEBUG
#ifndef NO_DEBUG
#define P(x) Serial.print(F(x))
#define PL(x) Serial.println(F(x))
#define PF(...) Serial.printf(__VA_ARGS__)
#else
#define P(x)
#define PL(x)
#define PF(...)
#endif
//Functions
void wifiTime();
void initWiFi();
unsigned long getTime();
// Variables
const char *ntpServer = "pool.ntp.org";
unsigned long epochTime;
bool BST=0;
int triggerClockupdate;
long timer,timer2;
//NTPtime NTPch("ch.pool.ntp.org"); // Choose server pool as required
char *ssid = "Utopia"; // Set you WiFi SSID
char *password = "GillyBob1965"; // Set you WiFi password
bool resetClock = 1;
ESP32Time rtc;
////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
clockSetup();
triggerClockupdate=rtc.getHour();
}
///////////////////////////////////////////////////////
void loop()
{
if (resetClock) {
clockSetup();
resetClock = 0;
}
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
Serial.println(rtc.getTime());
if (abs(triggerClockupdate-rtc.getHour())>=6)
{
resetClock = 1;
triggerClockupdate=rtc.getHour();
}
delay(5000);
}
////////////////////////////////////////////////////////////
void clockSetup()
{
initWiFi();
long timer = millis();
epochTime = getTime();
rtc.setTime(epochTime);
BSTUK();
timer2=millis()-timer;
Serial.print("Epoch Time: ");
Serial.println(epochTime);
Serial.print(timer2);
Serial.println(" time taken to reset time in milliseconds");
configTime(3600*BST, 0, ntpServer);// May need amending for BST
WiFi.disconnect(true);
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi disconnected");
}
}
//////////////////////////////////////////////////////
// Initialize WiFi
void initWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(LED_BUILTIN, LOW);
Serial.print('.');
delay(250);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
}
Serial.println(WiFi.localIP());
}
//////////////////////////////////////////////////////
// getTime() epochTime
unsigned long getTime()
{
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return (0);
}
time(&now);
return now;
}
/////////////////////////////////////////////////////////////////
void BSTUK()
{
int year = rtc.getYear();
rtc.setTime(0, 0, 0, 25, 3, year);
int dayOfWeek=int(rtc.getDayofWeek());
int day=(25+7-dayOfWeek);
rtc.setTime(0, 0, 0, day, 3, year);
int springHour = rtc.getDayofYear()*24+1;
rtc.setTime(0, 0, 0, 25, 10, year);
dayOfWeek=int(rtc.getDayofWeek());
day=(25+7-dayOfWeek);
rtc.setTime(0, 0, 0, day, 10, year);
int autumnHour = rtc.getDayofYear()*24+2;
rtc.setTime(epochTime);
int nowHour = rtc.getDayofYear()*24+1;
if (nowHour>springHour && nowHour<autumnHour) // Check for British Summer Time
{
BST=1;
}
else
{
BST=0;
}
return;
}