Dear Thiago Alves, Lets me explain, I think you have misunderstood me.
If I run your code without touching it, it will not get the hole bytes of epochTimestamp it only give 1 or 2 digits.
It means current time become 9 second before the RTC. I see this from the Remote Debugger.
Here is a screenshot of your PLC program that I have not touch, the digits is 9 and not what I aspects like 1731677843
Why I use a delay is to debug your code in Arduino IDE and output to Serial Console from setup loop() that is only running once.
When we know what a variable value contains we remove this delay. In other words the delay has nothing to do with your PLC program.
What it does explain is that epochTimestamp variable return something different from Arduino IDE compare to OpenPLC + Arduino extension.
I use Nano ESP32 and not same board you have been using.
Lets say your PLC program start before the WiFi connection is success it will not get RTC. And will not check again because it doesn't know if the
data is correct or not. So I used Arduino IDE with your Arduino extension code and it was the same. So to rule out if there is connection delay.
I need to set a delat in setup loop so the epochTimestamp have time to get it data. That was the reason for the delay to debug in Arduino IDE.
If you do not believe me test this code and if you could print in console the correct epochTimestamp without a delay in Arduino IDE then
you system is different from mine. I will do more research why it doesn't work for Nano Esp32.
My conclusion is if Serial.println() function couldn't print the correct RTC from a variable it could be that the connection is delayed.
The code below is exactly the code you made, I have only include things so I could use WiFi.
I also included offset of 3600 seconds to match my time +1
Let me know what below Arduino Code return with or without delay in Arduino IDE (Not in OpenPLC)
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiUdp.h>
const char *ssid = "ssid";
const char *password = "password";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"europe.pool.ntp.org", 3600, 60000);
int cycleCount = 0;
bool clockSet;
unsigned long epochTimestamp;
void setup()
{
// Read time from NTP Server
Serial.begin(9600);
WiFi.begin(ssid, password);
// wait 1 second so we have time to connect.
//delay(1000);
timeClient.begin();
timeClient.update();
epochTimestamp = timeClient.getEpochTime();
Serial.println(epochTimestamp);
}
void loop()
{
// Wait for a couple scan cycles to start RTC clock
if (cycleCount < 10)
{
cycleCount++;
}
else
{
clockSet = true; // Start RTC clock
};
}