Network sockets with Micropython on an Arduino Nano RP2040 #18901
-
|
I'm trying to get the time, with Micropython on an Arduino Nano RP2040 Connect but I'm getting the following error: The problem seems to be with the "addr = socket.getaddrinfo(host, 123)[0][-1]" command. "getaddrinfo" does not seem to return an address (addr). This code works fine with a real Raspberry Pi Pico W, and with a Raspberry Zero W. What could be going on? Is there some difference in the implementation of sockets, between the Arduino Nano and the Raspberry? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
As a result of your previous post on getting the nano rp2040 connect loaded with mp, and the helpful info provided in the post replies, I also got my old nano connects working again. So I had a go with one of my resurrected boards by putting my normal get ntp time that works on my pico's onto the nano, and it works ok. Its my bed time so I have not pondered over why your routine was not working, but I just post the code snippet so you may be able to work what may be be going on with your code. The ntp code is not mine but was copied from the @peterhinch repositories. So here it is, and I hope it helps. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, Beetlegigg. That helps a lot. This runs "right out of the box" with both a Raspberry Pico and the Arduino Nano RP2040, with only a minor change for the ssid and password. "addr = socket.getaddrinfo(time_server, 123)[0][-1]" returns "addr" as it should. Now to find out why this code works and mine doesn't. FYI: |
Beta Was this translation helpful? Give feedback.
-
|
You can also try the MicroPython library, import network
from config import wifi
import time, socket
import ntptime # we use MicroPython lib - ntptime
from machine import RTC
rtc = RTC()
# connect to WIFI
SSID, PWD = wifi
network.WLAN(network.AP_IF).active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PWD)
while not wlan.isconnected():
print('.', end='')
time.sleep(1)
print('\nConnected!')
# set DNS
DNS = '1.1.1.1'
ip, nm, gw, dns = wlan.ifconfig()
wlan.ifconfig((ip, nm, gw, DNS))
def lookup(host):
if not host: return
try:
hip = socket.getaddrinfo(host, 0)[0][-1]
return(hip[0])
except Exception as e:
print('Err:', e)
return None
def settime(ts, tout=5, tz=0): # ts=time server ip.
ntptime.host = ts
ntptime.timeout = tout
try:
ntptime.settime()
yr, mo, dy, hr, mn, sc, wd, yd = time.localtime()
# Monday to Sunday - 0 to 6 weekdays
# year, month, day, weekday, hours, minutes, seconds, subseconds
rtc.datetime((yr, mo, dy, wd, hr+tz, mn, sc, 0))
return True
except Exception as e:
print('--', e)
return False
retry_cnt = 5
timeout = 5 # secs
hosts = ('no.pool.ntp.org', # Norway
'europe.pool.ntp.org',
'pool.ntp.org',
'time.google.com',
'time.cloudflare.com',
'time.aws.com',
'time.windows.com',
'time.apple.com')
ok = False
# Set localtime - OSLO
# UTC+1 (Winter/CET) and UTC+2 (Summer/CEST)
# Winter (Nov - March 28, 2026)
# Summer (March 29 - Oct 24, 2026)
offset = 1 # offset = 2 for summer
for host in hosts:
if ok: break
# Look-up
ip = lookup(host)
if ip:
print(f'{host}: {ip}')
for i in range(retry_cnt):
ok = settime(ip, timeout, offset)
if ok: break
# time.sleep(5)
# time.sleep(5)
if ok:
# What is the date and time now
yr, mo, dy, hr, mn, sc, wd, yd = time.localtime()
print(f'{yr:04d}{mo:02d}{dy:02d} {hr:02d}:{mn:02d}:{sc:02d}')
else:
print('Localtime not properly set') |
Beta Was this translation helpful? Give feedback.
As a result of your previous post on getting the nano rp2040 connect loaded with mp, and the helpful info provided in the post replies, I also got my old nano connects working again. So I had a go with one of my resurrected boards by putting my normal get ntp time that works on my pico's onto the nano, and it works ok. Its my bed time so I have not pondered over why your routine was not working, but I just post the code snippet so you may be able to work what may be be going on with your code. The ntp code is not mine but was copied from the @peterhinch repositories. So here it is, and I hope it helps.