I bet you've never seen this before - it's a transparent OLED display - I'm probably
the first person to...
Ok, so you've seen it before.
But I bet you've never seen a transparent OLED being used as a clock - I can't imagine
anyone…
Ok, so that's been done to death as well, why am I even bothering to show you this.
There’s no denying it’s pretty cool and there are some impressive things you can do
like stacking multiple displays to make a 3D display.
However, what I find exciting about this project is that the time and date are correct.
Now, I can already hear your mouse moving to click away from this video but stick with me.
I can remove power from the microcontroller and it automatically knows what the time is when power is restored.
The microcontroller I’m using - the ESP32 - doesn’t have a built-in real-time clock
and there’s no battery backup.
How does this work?
I can see you're still not impressed?
We’ve got used to knowing what the time is - our computer displays it all the time
and so does our phone.
But how do they actually know what the time is?
Would you believe me if I said that my ESP32 was getting its time from an atomic clock?
Would you believe me if I told you that my ESP32 is probably getting its time from multiple
atomic clocks that are orbiting the earth at around 14,000 km/hour?
Let me introduce you to the Network Time Protocol - or NTP.
This is what I'm using in my clock and it's most likely what your computer and phone are
using as well.
But first, I want to give a quick plug to PCBWay who sponsors the channel - I’ve got
a bunch of videos coming up using PCBs that I’ve had manufactured by them, so don’t
forget to hit that subscribe button and the bell so you don’t miss anything.
So what is the Network Time Protocol?
Wikipedia says:
The Network Time Protocol (NTP) is a networking protocol for clock synchronization between
computer systems over packet-switched, variable-latency data networks.
What's impressive is that this protocol is that it has been In operation since 1985,
it's one of the oldest Internet protocols in current use.
It's probably been around longer than a lot of you have been alive.
How does it work?
Well, the first thing we need is some accurate clocks.
The most accurate clocks we’ve managed to create are atomic clocks.
There are a few flavours of this - we can use vibrating Caesium atoms - these vibrate
at nine billion one hundred ninety-two million six hundred thirty-one thousand seven hundred
seventy (9,192,631,770) times per second.
That's a big number!
Another popular choice is to use rubidium and you also have the very excitingly named
hydrogen maser clock.
Obviously not everyone has an atomic clock lying around - they are pretty expensive.
So a lot of people rely on Global Navigation Satellite Systems, such as GPS.
A typical GPS satellite will have up to four atomic clocks, we can use these atomic clocks
to create our accurate clock.
These super-accurate clocks are known in the NTP world as “Stratum 0” devices.
They don’t offer any time services over the network they just provide the time.
Connected directly to the Stratum 0 devices we have Stratum 1 devices - these devices
do offer time synchronisation services over the network,
Below these, we have Stratum 2 devices.
These devices are connected over a network connection to Stratum 1 devices.
And below these we have Stratum 3 devices - this goes on up to a maximum level of Stratum 15.
Stratum 16 indicates that the device is not synchronised and you shouldn't use it.
So let’s have a look at this in action.
We can install an application called WireShark on our computer.
This lets us look at all the network traffic that is being generated on our network.
The NTP protocol runs over UDP port 123.
So we can tell WireShark that we only want to see this traffic.
Let’s generate some requests to an NTP server.
We can see the packet going out from our computer and we can see the response coming back in.
If we look at the response we can see that we were talking to a Stratum 1 device.
And we can also see that it’s got its time from GPS.
We’ve just got time from some an atomic clock in orbit around the earth.
It’s mind-blowing.
There are also 4 timestamps in the response.
The reference, origin, receive and transmit timestamps.
These are used by our computers to compensate for network delays.
We have a delay due to sending a packet from our computer to the server, and we have a
delay from the server sending a packet to our computer.
The origin timestamp is the time the request left our computer.
the receive timestamp is the time the request arrived at the server
the transmit timestamp is the time the server sent its reply
Our computer also records the timestamp the reply arrived.
From these four timestamps, we can estimate the network delay using this formula.
We can also estimate the offset from our local clock from the server clock.
These values are then used to adjust the local clock so that it matches the correct time.
In most applications having time change can have a disastrous effect - particularly if
time goes backwards.
Imagine we’re collecting a sequence of events coming into our system, if the time on our
machine goes backwards our events will suddenly be recorded in a different order from when
they occurred.
To prevent this most systems will gradually adjust the local clock to match the server
time and won’t allow time to go backwards.
Obviously for this to work you need to be starting with a time that is already almost
correct.
So, this is all pretty complicated - how do we actually use it on the ESP32?
Well, there are actually only 2 lines of code required.
And these two lines are all the code that is going to be in this video.
First, we need to tell the system which NTP server we want to use.
And then once we've done that we can just use this line of code to get the time,
This will automatically refresh the time from the time server - that’s all we need to do.
It’s magic.
Now you may have noticed that I'm using a pool of machines to get my time.
This is much more reliable than using just one machine as an individual machine may go down and you won't be able to get the time.
Two lines of code to connect to an atomic clock.
Pretty cool if you ask me.
There is one last thing that is worth mentioning.
A natural thought is to assume that you should always try and connect to a Stratum 1 device
as that should have the most accurate time - it is closest to the actual clocks
The problem with this thought is that it ignores any network delays.
A stratum 1 device may be quite far away from you in terms of network hops.
And the route from your machine to the server may be completely different from the route
back from the server to your machine.
This can introduce quite a large error in your calculations.
It may be much more sensible to connect to a higher stratum device.
These may be closer to you and may be hosted by companies that have high-speed direct connections
to somewhere closer to a lower stratum device.
The other thing to bear in mind is that you might be connecting to a server that is overloaded if you all try to connect to the same stratum 1 device.
This could cause your time to be completely out of whack as the server might be struggling to serve all the requests that are coming in.
So it's much better to use either a pool of machines or use a time server that isn't a stratum 1 device.
So, hope this was interesting, don’t forget to like and subscribe - I’ll see you in the next video!