ESP32 - update clock after boot

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

ESP32 - update clock after boot

Post by guyd » Fri Jul 20, 2018 6:16 am

Hi,
I have ESP32 with Micropython installed and connects to wifi OK.

How can I update its clock right after boot such as ntp.pool.org ?
Last edited by guyd on Sun Jul 22, 2018 6:22 am, edited 1 time in total.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: ESP32 - update clock after boot

Post by dhylands » Fri Jul 20, 2018 9:41 pm

This was what I did using the loboris port:
https://github.com/mozilla-iot/webthing ... py#L12-L24

I don't know if the same thing works or not on the official ESP32 version.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: ESP32 - update clock after boot

Post by mattyt » Sat Jul 21, 2018 2:27 pm

There is primitive NTP support in the ESP32 port (borrowed from the ESP8266 port). In short:

Code: Select all

import ntptime
ntptime.settime()  # Synchronise the system time using NTP
Caveat: There is no timezone support so the system time will be set to UTC.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: ESP32 - update clock after boot

Post by guyd » Sun Jul 22, 2018 6:02 am

mattyt wrote:
Sat Jul 21, 2018 2:27 pm
There is primitive NTP support in the ESP32 port (borrowed from the ESP8266 port). In short:

Code: Select all

import ntptime
ntptime.settime()  # Synchronise the system time using NTP
Caveat: There is no timezone support so the system time will be set to UTC.
Does ntptime module supported in Micropython or is it applicable for Python version ?

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: ESP32 - update clock after boot

Post by guyd » Sun Jul 22, 2018 6:06 am

dhylands wrote:
Fri Jul 20, 2018 9:41 pm
This was what I did using the loboris port:
https://github.com/mozilla-iot/webthing ... py#L12-L24

I don't know if the same thing works or not on the official ESP32 version.
Oh, so machine module has a ntp_sync module? I does not show in
MiccroPython DOCS http://docs.micropython.org/en/latest/w ... chine.html.
TNX!

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: ESP32 - update clock after boot

Post by mattyt » Sun Jul 22, 2018 8:20 am

`ntptime` is alluded to in the ESP8266 docs but, you're right, it's not been well documented. As best I can tell it's only in the ESP8266 and ESP32 ports (and a different implementation in the Loboris fork as Dave mentioned).

Since it's a pure python implementation it could be easily added to any of the ports though. I suspect the only reason that hasn't happened yet is that it's still quite simplistic...

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: ESP32 - update clock after boot

Post by guyd » Sun Jul 22, 2018 8:31 am

mattyt wrote:
Sun Jul 22, 2018 8:20 am
`ntptime` is alluded to in the ESP8266 docs but, you're right, it's not been well documented. As best I can tell it's only in the ESP8266 and ESP32 ports (and a different implementation in the Loboris fork as Dave mentioned).

Since it's a pure python implementation it could be easily added to any of the ports though. I suspect the only reason that hasn't happened yet is that it's still quite simplistic...
Thank for your answer.
Just to make sure I understand correctly:
1) machine.ntp_sync() if part of machine module
2) you reference to ntptime.py - is a module by its own ? or just another implementation on ESP8266 ?

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: ESP32 - update clock after boot

Post by guyd » Sun Jul 22, 2018 5:39 pm

guyd wrote:
Sun Jul 22, 2018 6:02 am
mattyt wrote:
Sat Jul 21, 2018 2:27 pm
There is primitive NTP support in the ESP32 port (borrowed from the ESP8266 port). In short:

Code: Select all

import ntptime
ntptime.settime()  # Synchronise the system time using NTP
Caveat: There is no timezone support so the system time will be set to UTC.
Does ntptime module supported in Micropython or is it applicable for Python version ?
IT WORKS!!

wr300000
Posts: 32
Joined: Sun Jan 14, 2018 3:54 pm

Re: ESP32 - update clock after boot

Post by wr300000 » Sat Aug 04, 2018 2:23 pm

my NodeMCU32 could not import ntptime as suggested, please kindly see below result.

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:4596
load:0x40078000,len:0
load:0x40078000,len:12768
entry 0x4007ad68
[0;32mI (386) cpu_start: Pro cpu up.[0m
[0;32mI (387) cpu_start: Single core mode[0m
[0;32mI (387) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (390) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM[0m
[0;32mI (396) heap_init: At 3FFC57D0 len 0001A830 (106 KiB): DRAM[0m
[0;32mI (403) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM[0m
[0;32mI (409) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (415) heap_init: At 40091B28 len 0000E4D8 (57 KiB): IRAM[0m
[0;32mI (422) cpu_start: Pro cpu start user code[0m
[0;32mI (216) cpu_start: Starting scheduler on PRO CPU.[0m
OSError: [Errno 2] ENOENT
MicroPython v1.9.4-434-g163bacd1e on 2018-08-04; ESP32 module with ESP32
Type "help()" for more information.
>>> import ntptime
>>> ntptime.settime()
Traceback (most recent call last):
File "<stdin>", line 1, in <module> <-- What does it mean ?
File "ntptime.py", line 30, in settime <-- What does it mean ?
File "ntptime.py", line 18, in time <-- What does it mean ?
IndexError: list index out of range <-- What does it mean ?
>>>

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: ESP32 - update clock after boot

Post by mattyt » Sun Aug 05, 2018 2:04 pm

The error is misleading but I've come across it before; I see it when there is no internet connection.

To connect to your wifi:

Code: Select all

import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('YourWifiSsid', 'yourwifipassword')

Post Reply