Ahh ok. What is the reason for it to stop?
Do you remember all the concerns that computers would stop working in year 2000? It's a similar thing.
Because time_t is a 32-bit integer on ESP32, and it will roll over to zero in 2038.
Certainly, this line will be a problem
while (lastTime < 1000000000UL) {
because lastTime will legitimately be less than that value. Maybe @Wawa 's suggested code will be ok, I don't know.
My solution waits until there has been a successful NTP update, which is built into ESP-time.
That eliminates the problems with using an epoch threshold value to start the code.
Leo..
Yes, @Wawa 's code is better for that reason.
But do we know if ESP-time will break in 2038?
Still running and to the exact second (when comparing to the Clock app on my phone). Fyi. Will let it run for a longer time though, and then check again.
I'm also considering making a high pass filter on the two outputs from the ESP32 to the H-Bridge; to avoid the h-bridge giving constant voltage to the coil in case the ESP32 freezes or have some programming error. Will look into this shortly - would be such a shame if something broke during testing or running in general.
Is there a danger this could damage the mechanism?
Does it draw much current after making the "tick"? It might be worth measuring to see if it is worthwhile being concerned about it.
I don't know - but better safe then sorry perhaps. Not sure what amount of current that could be damaging over time (heating the coil - if the program stalls keeping the h-bridge engaged on +24v) - I can measure the current with the multimeter and program the hbridge to keep +24v over the time of measuring.
The current programming only sends the 250ms pulse length right?
Yeah, just 250ms, but it could be longer if the program freezes. I can't see any reason why the program would freeze, unless you modify it.
Also, ESP chips have built-in watchdog timers which will cause the chip to reset if the code freezes.
If your current measurements show that it could be a problem, one method could be to use a 556 dual timer chip between the ESP and the H-bridge. Each chip could be set for one-shot mode to give a 250ms pulse when triggered by the ESP output pins.
I'm also considering making a high pass filter on the two outputs from the ESP32 to the H-Bridge
I don't think that's wise.
Can't you use the EN pins of the L298.
The clock is just a stepper motor. Poster jremington hinted that the motor could be used center-tapped on a 12volt supply. Then both coils can be switched to ground with two 2N2222 transistors. Then there is no need for the L298, and there is no issue with coils staying on when the ESP is powered off.
Leo..
@PaulRB
It's still keeping time to the second (has been running for 1 day now).
Last time i checked (on the simple code that does not have NTP) it drifted 30 seconds a week. So we must assume your code correct the time correctly ![]()
It would properly be a good idea to implement the code @Wawa suggested, that corrects the time over a period though (I also imagine that the NTP can correct the time "incorrectly" if the latency of the internet connection is high? - If the network is being hammered with high traffic e.g. Perhaps the NTP code corrects this, but i don't know if that's the case).
Also the issue with the clock not working after 2038 - perhaps the code Leo made, this is not an issue?
I also checked the power going into the coil if the program somehow stalls:
15mA is going into the coil when the power is kept on it.
So that will be 0.36 Watts
Not sure if this needs any protection. I think i will leave that safety feature for now.
I have another question also:
Is it possible/easy to switch between two programs on the ESP32 with a psychical switch?
Lets say we have this NTP code running as the primary code (used when the clock is in my home) and then the simple code that just runs the clock with the delay values (If i take the clock away, to show family e.g.).
Something like this perhaps where a switch is connected with power to one of the input pins on the ESP:
If that input is LOW, then NTP program runs.
If that input is HIGH, then the simple program runs.
while (!ntp);
That could be time-limited, say 3-5 minutes.
Usually there is an NTP lock within a minute, or maybe two.
If that doesn't happen, then use the millis() value as epoch.
The digital time won't be correct, but the second tick/tocks will be ok.
Another option is to add a web portal to the code,
so you can quickly connect to your friend/family's router when there.
The ESP can hold multiple SSID and passwords for different locations, with auto select.
Leo..
the simple code... drifted 30 seconds a week. So we must assume your code correct the time correctly
No, unfortunately we can't assume that. The simple code did not synchronise with either NTP or the ESP32's internal clock. Using delay() like that doesn't even synchronise with the internal clock, so you would expect it to drift a lot.
It could be that the current code is synchronising with the ESP's internal clock but not correctly with NTP.
I remember another forum member reporting recently that their ESP32 kept very good time by it's internal clock and only required 1 second adjustment compared to NTP after 2~3 weeks. This may be what you are experiencing also. But the ESP's internal clock is not guaranteed to be so accurate and accuracy will vary from board to board even of the same design.
(I also imagine that the NTP can correct the time "incorrectly" if the latency of the internet connection is high? - If the network is being hammered with high traffic e.g. Perhaps the NTP code corrects this, but i don't know if that's the case).
You are correct, I think, network latency will cause the time to be always slightly off, but won't cause it to drift further off over time. The NTP code in the ESP32's core doesn't (and can't) correct for that as far as I know.
If that's a real concern (as opposed to a little OCD thing for you ;) ) then getting the time from GPS might be an improvement. This is also relatively easy to do with Arduino, but it needs extra hardware and a clear view of the sky.
No, unfortunately we can't assume that. The simple code did not synchronise with either NTP or the ESP32's internal clock. Using delay() like that doesn't even synchronise with the internal clock, so you would expect it to drift a lot.
Ahh i see.
Will see if i can make some code that uses the ticking seconds of the ESP clock then, and not the delay values - also to see how much it drifts then over time.
Try this. The millis() function runs off the ESP's internal clock so is always in sync with it.
const int in1 = 5;
const int in2 = 4;
unsigned long lastTime;
void setup() {
// put your setup code here, to run once:
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
}
void loop() {
// run
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
while (millis() - lastTime < 250);
// pause no pulse
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
while (millis() - lastTime < 1000);
// change polarity
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
while (millis() - lastTime < 1250);
// pause no pulse
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
while (millis() - lastTime < 2000);
lastTime += 2000;
}
Thank you
Will try letting the clock run with this for some time - see how much it drifts
Try this. The millis() function runs off the ESP's internal clock so is always in sync with it.
I had it running for a little under 3 days. Still drifts a little: around 6 seconds fast.
That's not to bad, but it would be nice with the NTP function (also to stay true to the clock's original purpose and seeing the difference between the ticking hand and the one driven by 220v/50Hz).
I have looked into the code from @Wawa.
It does not compile for the ESP32, so i will try to modify the code for the ESP32 if i can.
So far I've gotten this far:
#include <WiFi.h>
#include <time.h>
time_t now;
unsigned long tick, prevMillis;
int interval = 500;
bool ntp, toggle;
/* Configuration of NTP */
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7200;
const int daylightOffset_sec = 0;
void time_is_set() {
Serial.println(F("time was updated"));
ntp = true;
}
void setup() {
Serial.begin(115200);
Serial.println(F("\nESP32 NTP clock"));
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
settimeofday_cb(time_is_set);
WiFi.mode(WIFI_STA);
WiFi.begin("The Promised LAN", "*******");
while (WiFi.status() != WL_CONNECTED) delay(200);
Serial.println(F("\nConnected\nEpoch"));
while (!ntp);
time(&now);
tick = now;
}
void loop() {
if (millis() - prevMillis >= interval) {
prevMillis = millis(); // reset
time(&now); // read the current time
interval = 500 + (tick - now); // correction
toggle = !toggle; // 1Hz
//digitalWrite(pin, toggle);
if (toggle) { // when HIGH
Serial.print(now);
Serial.print("\t");
Serial.print(tick);
Serial.print("\t");
Serial.println(interval);
tick ++; // update
}
}
}
The "settimeofday_cb(time_is_set);" is giving me problems though:
C:\Users\graas\OneDrive\Clock\NTP_CLOCK_3\NTP_CLOCK_3.ino: In function 'void setup()':
C:\Users\graas\OneDrive\Clock\NTP_CLOCK_3\NTP_CLOCK_3.ino:22:3: error: 'settimeofday_cb' was not declared in this scope
settimeofday_cb(time_is_set);
^~~~~~~~~~~~~~~
C:\Users\graas\OneDrive\Clock\NTP_CLOCK_3\NTP_CLOCK_3.ino:22:3: note: suggested alternative: 'settimeofday'
settimeofday_cb(time_is_set);
^~~~~~~~~~~~~~~
settimeofday
exit status 1
Compilation error: 'settimeofday_cb' was not declared in this scope
Just got this in the mail also, so I can have an external antenna outside the metal enclosure of the clock (ESP32 DEVKITC V4 WROVER B IPEX):
correct the time "incorrectly" if the latency of the internet connection is high?
I believe in NTP, if fully implemented, it compensates for network,latency.
I read about this 20-odd years ago, Iād have to dig around to find anything.
const long gmtOffset_sec = 7200; const int daylightOffset_sec = 0; configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
This is crude, and not needed if you have a location string.
Q: What time zone do you live in (post number from the list).
I mentioned PLL (phase locked loop) a few times.
I think you should have a freerunning clock is nudged slightly until in sync with epoch.
Leo..
Q: What time zone do you live in (post number from the list).
330
I mentioned PLL (phase locked loop) a few times.
I think you should have a freerunning clock is nudged slightly until in sync with epoch.
That explanation you made in your post makes sense to me - it is smart to have the internal clock of the ESP being synced with the NTP and then correct that difference over some time (to speed up/down the ticking until it is in sync with the internal clock again) - I'm just a real noob at coding this - learning slowly how this works ![]()
