For an index to all my stories click this text
Getting the right time is often a requirement for IOT and other projects.
Usually we use an NTP server for this like described in a previous story on this weblog which you can find here: http://lucstechblog.blogspot.com/2023/04/getting-right-time-with-micropython.html
There is just a small problem with this. If you have read that story you will know that you need to make a manual adjustment in the software for the Daylight Savings Time to get the right time. And that is annoying with a finished project. This means that you must update your program twice a year.
What if you want your program to automatically adjust to the right time ??
Well here is how to do that with MicroPython.
The pitfall.
There is no way to automatically get the right time if your finished project is regularly placed in another location, unless you are sure they are in the same timezone.
Your exact time is dependent on your geographic location. The local time in London is different from the time in Amsterdam for example.
So when your project is moved internationally an adjustment must be made for the location where it is at.
There is a trick for getting your location and we are going to use that here. However when the project moves from London to Amsterdam undoubtedly the microcontroller needs to contact another router for wifi access. And to achieve that you need to fill in the routers credentials. And there is your manual involvement.
Getting your location.
For getting the right time your program needs to know your location. And here is a nice trick for getting your location.
Your router is likely connected to a local internet provider. So you will get an internet IP address that is likely to be from your own timezone.
By connecting to the ip-api.com service you will get your internet IP address.
Let's try this first.
Just point your browser to https://ip-api.com/
scroll a bit down and there is your information.
I camouflaged my own IP number as I want some privacy.
But look at the info and you can see that IP-API.COM estimates that my IP number originates from the town Boekel in the Netherlands.
Well actually that is not my location but the important part is that this is in the same Time-Zone as I am. So using my Internet IP number indeed points to the right timezone.
When you move your project to a different location you only have to point your browser to https://ip-api.com/ to know if your program will automatically get the right timezone. If that is so you do not need to make any changes to your program.
We will need the IP address in the next step which is getting the right time. But to achieve that we first need our program to fetch the IP address from the ip-api.com website.
Using the Internet IP address to get the right time
Now let's have a look at the ip-api.com documents page: https://ip-api.com/docs
Above shows the part from the documentaion that tells how to use the api to get the response in JSON code.
http://ip-api.com/json
So this is the only part we need to translate into MicroPython. And here it is.
import network import urequests # Router credentials ssid = "YOUR-ROUTERS-NAME" pw = "YOUR PASSWORD" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) # station mode wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass def get_public_ip_address(): response = urequests.get('http://ip-api.com/json') json_data = response.json() return json_data['query'] ip_address = get_public_ip_address() print(ip_address)
Running this program will put the Internet IP-address into the variable ip_address and prints it out in Thonny's shell.
I dont think this needs any explanation. The received data is JSON code and we distill the information from the line "query" from that and that presents us the IP address.
If you need more information on how to program in MicroPython and how to use the requests library or to distill JSON information I can recommend my book: Raspberry Pico W simplified. These things are discussed in details in the book.
You can buy it from Amazon. Click here or the above picture for the link.
Using the IP-Address to get the right time.
Now we have our Internet IP-Address we can use that to get the right time for our time-zone. We are going to use the https://timeapi.io/ service for that.
https://timeapi.io/ is a free service that will provide us the right time for our location. To do so we use their API call and provide our Internet IP-address. You can find all documentation on this site: https://timeapi.io/swagger/index.html
As you can see there are a lot of possibilities. We can use our IP address to get the right time and we can also use our latitude and longitude if they are known. Let's stick to getting the time by using the just found IP address.
The page offers you the possibility to try it out. And if you do you will get an example on how to code the API and the results are displayed further on. We are interested in how the API call should be used and the page shows how it is done:
https://timeapi.io/api/Time/current/ip?ipAddress=237.71.232.203
The provided internet IP=address is just a sample. We will have to replace that with our own IP address.
This is how that is done:
response = urequests.get('https://timeapi.io/api/Time/current/ip?ipAddress='+ip_address) json_data = response.json() print("Hour : "+str(json_data["hour"])) print("Time : "+str(json_data["minute"]))
The urequests library calls the API and in the call we added our own just found IP-address.

And here is the information shown in Thonny's shell. And believe me the time was accurate.
The complete code.
For your convenience I hereby give you the complete program
import network import urequests # Router credentials ssid = "YOUR-ROUTERS-NAME" pw = "YOUR-PASSWORD" print("Connecting to wifi...") # wifi connection wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect(ssid, pw) # wait for connection while not wifi.isconnected(): pass def get_public_ip_address(): response = urequests.get('http://ip-api.com/json') json_data = response.json() return json_data['query'] ip_address = get_public_ip_address() print(ip_address) response = urequests.get('https://timeapi.io/api/Time/current/ip?ipAddress='+ip_address) json_data = response.json() print("Hour : "+str(json_data["hour"])) print("Time : "+str(json_data["minute"]))
Power Failure
I am sure you will notice that this is a great way to get the accurate time for your IOT and other projects. Get the significant part of this program and use it in your own project so you will automatically get the right time even after a power failure.
Till next time
Have fun
Luc Volders