ESP32 Feather NTP libraries

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
karltbraun
 
Posts: 13
Joined: Tue Dec 07, 2021 4:48 pm

ESP32 Feather NTP libraries

Post by karltbraun »

CircuitPython Version: 7.1.1
Board: ESP32-S2 Feather

I'm trying to get myself familiar with clock routines - both getting the current time off the internet (with, of course, NTP) and for doing time-related functions on the controller.

I'm using the Adafruit ESP32-S2 feather boards. Following the examples I've found on the CircuitPython site, the sampe code wants to use the following libraries:

import time
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_ntp import NTP

But I get "ImportError: no module named 'adafruit_esp32spi.adafruit_esp32spi'
I've read on some of the posts here that the Feather isn't compatible with the ESP32spi routines. What do I need to use the NTP routines?

(I've successfully connected to WiFi and have a functional MQTT client working with the BME280 sensor; so I'm just tying to get the library routines to allow me to get time from an NTP server - don't want to use the adafruit-specific servers).

Any help appreciated.

User avatar
bonemender
 
Posts: 8
Joined: Tue Nov 30, 2021 10:37 pm

Re: ESP32 Feather NTP libraries

Post by bonemender »

did you copy the adafruit_esp32spi folder from the circuitpython-bundle lib folder to the feather's lib folder?

User avatar
karltbraun
 
Posts: 13
Joined: Tue Dec 07, 2021 4:48 pm

Re: ESP32 Feather NTP libraries

Post by karltbraun »

Yes, but it's not an SPI board (for these features) so that didn't work.

User avatar
AmazingCircuitPythonFR35
 
Posts: 99
Joined: Thu Feb 05, 2015 10:18 am

Re: ESP32 Feather NTP libraries

Post by AmazingCircuitPythonFR35 »

Hi,

I would also be very interested in a solution.

Indeed, the circuitpython module 'ntp' takes as argument an ESP_SPI (like airlift or other).
Can it work on a board with an ESP32-S2 where there is no wifi co-processor ?

Mike

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: ESP32 Feather NTP libraries

Post by tannewt »

Unfortunately the current NTP library is a bit of a lie. It just uses a NINA FW call to get the time.

However, I did make an actual NTP version here: https://github.com/tannewt/Adafruit_Cir ... ee/raw_ntp

Would you mind trying it and letting me know how it works? We can figure out how to swap them out soon.

User avatar
AmazingCircuitPythonFR35
 
Posts: 99
Joined: Thu Feb 05, 2015 10:18 am

Re: ESP32 Feather NTP libraries

Post by AmazingCircuitPythonFR35 »

tannewt wrote:Unfortunately the current NTP library is a bit of a lie. It just uses a NINA FW call to get the time.

However, I did make an actual NTP version here: https://github.com/tannewt/Adafruit_Cir ... ee/raw_ntp

Would you mind trying it and letting me know how it works? We can figure out how to swap them out soon.
Hi,

I tried your 'adafruit_ntp' module (the one from github) but I get an error about the 'time' module.
The error says that the 'time' module does not contain a 'gmtime' attribute.
I tried replacing 'gmtime' with 'localtime' but I get another error.

Here are the contents of my 'code.py' file (I used a FeatherS2 with CPY 7.x):

Code: Select all

# modules circuitpython natifs
import wifi
import socketpool
import time
# modules circuitpython externes (dossier lib)
import adafruit_ntp

# Récupère le SSID et le MDP du réseau WiFi dans le fichier secrets.py
try:
    from secrets import secrets
except ImportError:
    print("Fichier secrets.py non disponible")
    raise

# ********************************
# *** CONNEXION AU RESEAU WIFI ***
# ********************************
print("Réseaux WiFi disponibles :")
for network in wifi.radio.start_scanning_networks():
    print(f'\t{str(network.ssid, "utf-8")}\t\tRSSI: {network.rssi}\tCanal: {network.channel}')
wifi.radio.stop_scanning_networks()

print(f'Tentative de connexion au réseau {secrets["ssid"]}')
print("........")
wifi.radio.connect(secrets["ssid"], secrets["password"])
print(f'Connecté au réseau {secrets["ssid"]} !')

# ***********************************
# *** RECUPERATION DE L'HEURE NTP ***
# ***********************************
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool)

while True:
    print(ntp.datetime)
    time.sleep(1)


User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: ESP32 Feather NTP libraries

Post by tannewt »

What error do you get?

User avatar
Bill_r
 
Posts: 31
Joined: Tue May 26, 2015 8:56 am

Re: ESP32 Feather NTP libraries

Post by Bill_r »

Hi Tannewt,

I would also be interested in using NTP on the Feather ESP32-S2

The code below obtains time from Adafruit IO. I commented out that portion, imported your ntp library from github, and added the last two lines to get NTP time. However, this always returns an AttributeError:

Code: Select all

import time, board, adafruit_ds3231
import ipaddress, ssl, wifi, socketpool, adafruit_requests, secrets, adafruit_ntp
from rtc import RTC
ds3231=adafruit_ds3231.DS3231(board.I2C())
from secrets import secrets
aio_username = secrets["aio_username"]; aio_key = secrets["aio_key"]; location = secrets.get("timezone", None)
TIME_URL = "https://io.adafruit.com/api/v2/%s/integrations/time/strftime?x-aio-key=%s" % (aio_username, aio_key)
TIME_URL += "&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S.%25L+%25j+%25u+%25z+%25Z"

print("Getting time... ", end="")
wifi.radio.connect(secrets["ssid"], secrets["password"])
pool = socketpool.SocketPool(wifi.radio)
'''
requests = adafruit_requests.Session(pool, ssl.create_default_context())
response = requests.get(TIME_URL); times=response.text; times=times.split(" ")
print(times)
the_date=times[0];  the_time=times[1]; yday=int(times[2]); wday=int(times[3]); isdst=None
year,mon,mday=[int(x) for x in the_date.split("-")]
the_time=the_time.split(".")[0]; hour,min,sec=[int(x) for x in the_time.split(":")]
ds3231.datetime=RTC().datetime=time.struct_time((year,mon,mday,hour,min,sec,wday,yday,isdst)) #set rtc to internet timetime: "
print ("Time: ",time.localtime())
'''
ntp=adafruit_ntp.NTP(pool)
print(ntp.datetime)
Output in the repl is as follows:

Getting time... Traceback (most recent call last):
File "code.py", line 23, in <module>
File "/lib/adafruit_ntp.py", line 61, in __init__
AttributeError: 'module' object has no attribute 'gmtime'

Code done running.

Not sure if I missed something or not. Any assistance would be appreciated.

Thanks.

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: ESP32 Feather NTP libraries

Post by tannewt »

Bill, try replacing gmtime with localtime and then let me know what the error is. I developed this library on desktop (aka CPython) which has more functions than CircuitPython does.

User avatar
Bill_r
 
Posts: 31
Joined: Tue May 26, 2015 8:56 am

Re: ESP32 Feather NTP libraries

Post by Bill_r »

OK, replaced gmtime with localtime in two places in the library. Error is now:

Getting time... Traceback (most recent call last):
File "code.py", line 23, in <module>
File "/lib/adafruit_ntp.py", line 61, in __init__
OverflowError: timestamp out of range for platform time_t

Code done running.


Bill

User avatar
Bill_r
 
Posts: 31
Joined: Tue May 26, 2015 8:56 am

Re: ESP32 Feather NTP libraries

Post by Bill_r »

OK - a little further testing.

commented out lines 61 & 62 in the library

Code: Select all

#        if time.localtime(0).tm_year != 1970:
#            raise OSError("Epoch must be 1970")
Results are now:

code.py output:
Getting time... struct_time(tm_year=2022, tm_mon=3, tm_mday=4, tm_hour=18, tm_min=56, tm_sec=22, tm_wday=4, tm_yday=63, tm_isdst=-1)

Code done running.

Press any key to enter the REPL. Use CTRL-D to reload.

Looks pretty good to me!

Bill

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: ESP32 Feather NTP libraries

Post by tannewt »

Nice! That looks good.

User avatar
AmazingCircuitPythonFR35
 
Posts: 99
Joined: Thu Feb 05, 2015 10:18 am

Re: ESP32 Feather NTP libraries

Post by AmazingCircuitPythonFR35 »

tannewt wrote:Nice! That looks good.
Hello,

It works perfectly well for me too!
Great!

Would it be possible to modify the official version of the library in the CircuitPython library bundle?
Thank you.

Mike

User avatar
tannewt
 
Posts: 3304
Joined: Thu Oct 06, 2016 8:48 pm

Re: ESP32 Feather NTP libraries

Post by tannewt »

Unfortunately, it isn't very easy to do because other libraries use this one. We've got an issue to track this here: https://github.com/adafruit/Adafruit_Ci ... /issues/16

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”