Skip to content

Added support for mDNS in AP Mode for Raspberry Pi #10443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

awcab
Copy link

@awcab awcab commented Jun 26, 2025

Fixes #9813

Added check for AP mode in each usage and passed appropriate parameter NETIF_AP

dhalbert
dhalbert previously approved these changes Jul 8, 2025
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@anecdata
Copy link
Member

anecdata commented Jul 8, 2025

If I read the code changes correctly (haven't tested raspberrypi), AP will take precedence over station for mDNS.

On espressif (in this case, a QT Py S3 w/PSRAM), a device with both station and AP active will present the mDNS host to both networks:

import time
import os
import wifi
import mdns

PORT = 8080

time.sleep(3)  # wait for serial

# wifi station
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))

# wifi access point
wifi.radio.start_ap("Bob", "YourUncle")

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "mdns-r-us"
print(f"http://{mdns_server.hostname}.local.:{PORT}")
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=PORT)

while True:
    time.sleep(1)

macOS mDNS Discovery app output:
Screenshot 2025-07-08 at 3 19 37 PM
• 192.168.4.1 is the AP: macOS connected directly to espressif device wifi AP
• 192.168.6.40 is the station: macOS to ethernet to LAN to wifi to espressif device wifi station

If it's not possible to present on both, or preferred to have mDNS use a single NETIF on raspberrypi, the limitation should be noted in the docs?

Addendum: Adding two adafruit_httpservers to the code above (one for each IP)... due to a quirk somewhere (HTTP server library or deeper), the station HTTP server will respond to http://{wifi.radio.ipv4_address}:8080, and either HTTP server will respond to http://{mdns_server.hostname}.local.:8080, but no response is received from http://{wifi.radio.ipv4_address_ap}:8080. This is on macOS with ethernet connected to the espressif station network, and wifi connected to the espressif AP.

code.py
import time
import os
import wifi
import mdns
import socketpool
from adafruit_httpserver import Server, Request, Response

PORT = 8080

time.sleep(3)  # wait for serial

# wifi station
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))

# wifi access point
wifi.radio.start_ap("Bob", "YourUncle")

mdns_server = mdns.Server(wifi.radio)
mdns_server.hostname = "mdns-r-us"
print(f"http://{mdns_server.hostname}.local.:{PORT}")
mdns_server.advertise_service(service_type="_http", protocol="_tcp", port=PORT)

pool = socketpool.SocketPool(wifi.radio)
http_server_st = Server(pool, "/static", debug=True)
http_server_ap = Server(pool, "/static", debug=True)

@http_server_st.route("/")
def base(request: Request):
    return Response(request, f"code.py HTTP Server http://{wifi.radio.ipv4_address}:{PORT}")

@http_server_ap.route("/")
def base(request: Request):
    return Response(request, f"code.py HTTP Server http://{wifi.radio.ipv4_address_ap}:{PORT}")

http_server_st.start(str(wifi.radio.ipv4_address), port=PORT)
http_server_ap.start(str(wifi.radio.ipv4_address_ap), port=PORT)
while True:
    http_server_st.poll()
    http_server_ap.poll()

We can see in the debug logging that the client is accessing the HTTP server(s) on both networks:

192.168.5.8 -- "GET /" 384 -- "200 OK" 128 -- 13ms
192.168.5.8 -- "GET /" 388 -- "200 OK" 128 -- 16ms
192.168.4.2 -- "GET /" 388 -- "200 OK" 127 -- 17ms
192.168.4.2 -- "GET /" 388 -- "200 OK" 127 -- 16ms

@dhalbert dhalbert dismissed their stale review July 8, 2025 20:37

for further consideratin

@dhalbert
Copy link
Collaborator

dhalbert commented Jul 9, 2025

I don't know enough about mDNS usage to comment further on @anecdata's comments. @awcab do you have a comment?

@awcab
Copy link
Author

awcab commented Jul 9, 2025

These are great points. I'll test and see if it's possible for the rpi to support both and either submit a new PR or a suggest a docs update.

@anecdata
Copy link
Member

anecdata commented Jul 9, 2025

Thanks for implementing this, and sorry for the lateness of the testing.

This one always bites me a little when testing:

the TTL for address records in Multicast DNS is typically 120 seconds

https://www.rfc-editor.org/rfc/rfc6762#section-10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mDNS doesn't work on an Access Point on raspberrypi
3 participants