Aegidius
 Plüss   Aplulogo
     
 www.aplu.ch      Print Text 
© 2021, V10.4
 
 
 
Examples 4: Miscellaneous
micro:bit
 

 

NTP time server

Internet time servers offer time services for several hundred million systems worldwide. They use a standardized data format, the Network Time Protocol (NTP).

The LinkUp's ESP32 uses a library that implements the NTP. With the import of the small module ntptime the two functions getTimeRaw() and getTime() are available to the micro:bit programmer, which return the current date time as tuple or in string format.

In the following example the ESP32 logs on to an access point, retrieves the time information in raw format first from the standard server pool.ntp.org and then in string format from the server ch.pool.ntp.org.

# NtpTime1.py

import linkup
import ntptime

print("Retrieving time from NTP servers...")
linkup.connectAP("myssid", "mypassword")
data = ntptime.getTimeRaw()
print("pool.ntp.org returns in raw format:", data)
data = ntptime.getTime("ch.pool.ntp.org")
print("ch.pool.ntp.org returns in string format:", data)
 

A typical outout in the console window is:

Retrieving time from NTP servers...
pool.ntp.org returns in raw format: (2019, 9, 9, 14, 18, 36, 2, 252)
ch.pool.ntp.org returns in string format: Mo 2019-09-09 14:18:37 GMT

With a few lines of code, you can create a continuously running clock that displays the current time very accurately on the micro:bits LED display.

# NtpTime2.py

from microbit import *
import linkup
import ntptime

linkup.connectAP("myssid", "mypassword")
while True:
    tme = ntptime.getTimeRaw()
    display.scroll("-> %02d:%02d:%02d" % (tme[3], tme[4], tme[5]))
    sleep(5000)                 
 

Further examples will follow.