← index #11296Issue #15695
Related · high · value 0.848
QUERY · ISSUE

Pico W STA/AP Interfaces Sharing Same DNS?

openby Beormundopened 2023-04-19updated 2023-04-19

Discussed in https://github.com/orgs/micropython/discussions/11295

<div type='discussions-op-text'>

<sup>Originally posted by Beormund April 19, 2023</sup>
The following is causing havoc with my AP captive portal:

# Start clean: run machine.reset()
# import machine
# machine.reset()
# MicroPython v1.19.1-1016-gb525f1c9e on 2023-04-14; Raspberry Pi Pico W with RP2040

import network

wap = network.WLAN(network.AP_IF)
print(f'Default Inactive AP: {wap.ifconfig()}')
wap.active(True)
print(f'Default Active AP: {wap.ifconfig()}')
# Update AP_IF DNS
wap.ifconfig(wap.ifconfig()[:3] + (wap.ifconfig()[0],))
print(f'Updated Active AP: {wap.ifconfig()}')

sta = network.WLAN(network.STA_IF)
print(f'Default Inactive STA: {sta.ifconfig()}')
# After sta.connect(ssid, pwd), DNS is updated:
# Mimic DNS update:
sta.ifconfig(sta.ifconfig()[:3] + ('192.168.1.254',))
print(f'STA After STA DNS Update: {sta.ifconfig()}')

# Why is AP_IF DNS also updated?
print(f'AP After STA DNS Update: {wap.ifconfig()}')

#>>> %Run -c $EDITOR_CONTENT
# Default Inactive AP: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
# Default Active AP: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')
# Updated Active AP: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '192.168.4.1')
# Default Inactive STA: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '192.168.4.1')
# STA After STA DNS Update: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '192.168.1.254')
# AP After STA DNS Update: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '192.168.1.254')
#>>> 

In my application I run a captive portal using the Pico W's soft access point, along with a lightweight DNS server. This enables a user to select a local network and enter a password. This all works well until the STA_IF interface connects and gets an IP. I should be able to display this IP in the captive portal so the user can then copy it and navigate to the new IP once the Pico W has restarted. Unfortunately the captive portal stops working because the AP_IF DNS changes.

I don't think there is a correct separation of DNS between the two interfaces.</div>

CANDIDATE · ISSUE

RPI PICO W Regression: Setting DNS results in address resolution failure

openby vshymanskyyopened 2024-08-23updated 2025-10-24
bugport-rp2

Port, board and/or hardware

RPI PICO W

MicroPython version

MicroPython v1.24.0-preview.224.g6c3dc0c0b on 2024-08-22; Raspberry Pi Pico W with RP2040

Reproduction

import socket
import network
import asyncio

async def test():
    print("Connecting to WiFi...")
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    sta_if.connect("---", "---")
    while not sta_if.isconnected():
        await asyncio.sleep_ms(100)

    print("Setting DNS...")
    cfg = list(sta_if.ifconfig())
    cfg[-1] = "8.8.8.8"
    sta_if.ifconfig(cfg)

    server, port = "blynk.cloud", 443

    print("Resolving IP...")
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    addr = socket.getaddrinfo(server, port)[0][-1]

    print("Connecting...")
    sock.connect(addr)

    print("OK")

asyncio.run(test())

Expected behaviour

MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040:
MPY: soft reboot
Connecting to WiFi...
Setting DNS...
Resolving IP...
Connecting...
OK

Observed behaviour

MicroPython v1.24.0-preview.224.g6c3dc0c0b on 2024-08-22; Raspberry Pi Pico W with RP2040:
MPY: soft reboot
Connecting to WiFi...
Setting DNS...
Resolving IP...
Traceback (most recent call last):
  File "main.py", line 22, in test       # socket.getaddrinfo
OSError: -2

Additional Information

No response

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied