Skip to content

feat: added mdns resolution - #210

Open
IM-TechieScientist wants to merge 3 commits into
fossasia:dev26from
IM-TechieScientist:wifi-mdns-discovery
Open

feat: added mdns resolution #210
IM-TechieScientist wants to merge 3 commits into
fossasia:dev26from
IM-TechieScientist:wifi-mdns-discovery

Conversation

@IM-TechieScientist

@IM-TechieScientist IM-TechieScientist commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Working on #208.

Adds mDNS discovery to the ESP SPI Wifi bridge.

  • Advertises the bridge as pslab-pico.local after station mode connects.
  • Publishes the TCP SCPI endpoint through _pslab._tcp.
  • Uses the official pinned espressif/mdns ESP-IDF managed component.

Users can now directly connect to their PSLab Pico through the domain name without requiring an IP address.

This PR follows up on #209 and is stacked on it as it is a direct dependency.

Summary by Sourcery

Add Wi-Fi provisioning and mDNS discovery to make the ESP SPI bridge self-configuring and discoverable on the local network.

New Features:

  • Introduce a web-based Wi-Fi provisioning flow via a temporary access point that stores station credentials in NVS.
  • Add mDNS advertising of the PSLab Pico bridge host name and TCP SCPI service for IP-free discovery.

Enhancements:

  • Refactor Wi-Fi startup to prefer stored station credentials, fall back to provisioning when needed, and fail fast when setup cannot complete.

Build:

  • Register new wifi provisioning and mDNS service sources in the main component and declare the managed mdns dependency.

Documentation:

  • Update firmware README to describe the provisioning access point workflow and mDNS-based connection using the pslab-pico.local host name.

Copilot AI lite review requested due to automatic review settings August 14, 2026 15:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sourcery-ai

sourcery-ai Bot commented Aug 14, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds mDNS-based discovery for the PSLab Pico ESP SPI Wi-Fi bridge and introduces a first-boot Wi-Fi provisioning flow with an HTTP setup portal, refactoring Wi-Fi startup to support AP+STA and persistent credentials stored in NVS.

Sequence diagram for Wi-Fi startup, provisioning, and mDNS discovery

sequenceDiagram
    participant udp_task
    participant start_wifi
    participant wifi_provisioning
    participant start_station
    participant mdns_service

    udp_task->>start_wifi: start_wifi()
    start_wifi->>wifi_provisioning: wifi_provisioning_load_credentials(credentials)
    alt credentials loaded
        start_wifi->>start_station: start_station(credentials)
        alt station connects
            start_station->>mdns_service: mdns_service_start(CONFIG_ESP_BRIDGE_LOCAL_PORT)
            mdns_service-->>start_station: success
            start_station-->>start_wifi: true
        else station fails
            start_station-->>start_wifi: false
            start_wifi->>wifi_provisioning: wifi_provisioning_run(credentials)
            wifi_provisioning-->>start_wifi: credentials
            start_wifi->>start_station: start_station(credentials)
            start_station->>mdns_service: mdns_service_start(CONFIG_ESP_BRIDGE_LOCAL_PORT)
            mdns_service-->>start_station: success
            start_station-->>start_wifi: true
        end
    else no saved credentials
        start_wifi->>wifi_provisioning: wifi_provisioning_run(credentials)
        wifi_provisioning-->>start_wifi: credentials
        start_wifi->>start_station: start_station(credentials)
        start_station->>mdns_service: mdns_service_start(CONFIG_ESP_BRIDGE_LOCAL_PORT)
        mdns_service-->>start_station: success
        start_station-->>start_wifi: true
    end
    start_wifi-->>udp_task: success / failure
    alt failure
        udp_task->>udp_task: vTaskDelete(NULL)
    end
Loading

Sequence diagram for the new Wi-Fi HTTP provisioning flow

sequenceDiagram
    actor User
    participant WiFi_AP as wifi_provisioning_run
    participant HTTP_Server as esp_http_server
    participant configure_handler
    participant NVS

    User->>WiFi_AP: connect to AP (WIFI_MODE_AP)
    WiFi_AP->>HTTP_Server: start_server()
    HTTP_Server-->>WiFi_AP: ready

    User->>HTTP_Server: HTTP GET /
    HTTP_Server->>configure_handler: setup_page_handler()
    configure_handler-->>User: setup_page HTML

    User->>HTTP_Server: HTTP POST /configure (ssid, password)
    HTTP_Server->>configure_handler: configure_handler(request)
    configure_handler->>configure_handler: httpd_req_recv(form)
    configure_handler->>configure_handler: get_form_value(form, "ssid")
    configure_handler->>configure_handler: get_form_value(form, "password")
    configure_handler->>configure_handler: credentials_are_valid(credentials)
    alt valid credentials
        configure_handler->>NVS: save_credentials(credentials)
        NVS-->>configure_handler: success
        configure_handler-->>User: httpd_resp_sendstr("Saved")
        configure_handler->>WiFi_AP: xEventGroupSetBits(PROVISIONING_COMPLETE_BIT)
        WiFi_AP->>WiFi_AP: stop_server()
        WiFi_AP->>WiFi_AP: esp_wifi_stop()
        WiFi_AP-->>WiFi_AP: return credentials
    else invalid / save error
        configure_handler-->>User: httpd_resp_send_err(...)
    end
Loading

File-Level Changes

Change Details Files
Refactor Wi-Fi startup to use stored or provisioned station credentials, support AP+STA, and integrate mDNS advertisement when the station connects.
  • Replaced hard-coded SSID/password station setup with start_station() that takes wifi_provisioning_credentials_t, handles event bits, retry counter, and power-save configuration with error handling.
  • Added start_wifi() to initialize netif, events, Wi-Fi driver, AP+STA interfaces, register handlers, load credentials from NVS, fall back to running provisioning AP if none or if station fails, then retry station.
  • Updated udp_task() to abort the task if Wi-Fi setup fails instead of unconditionally starting Wi-Fi.
  • Upon successful station connection, calls mdns_service_start() with the local TCP port and logs a warning if mDNS cannot be started.
esp_firmware/main/main.c
Introduce a captive-portal-like Wi-Fi provisioning flow using an ESP HTTP server over a temporary AP, and persist credentials to NVS.
  • Defined wifi_provisioning_credentials_t and related constants for SSID/password lengths in wifi_provisioning.h.
  • Implemented a minimal URL-encoded form parser with percent-decoding plus helper for extracting key/value pairs from POST bodies.
  • Implemented NVS-backed load and save routines for Wi-Fi credentials with validation on SSID and password length.
  • Implemented an HTTP setup page and /configure handler that receives, validates, and stores credentials, signals completion via an event group, and returns simple HTML responses.
  • Implemented wifi_provisioning_run() that configures and starts a WPA2 AP with a dynamic SSID based on MAC, runs the HTTP server, waits for credentials submission, then stops AP and outputs the chosen credentials.
esp_firmware/main/wifi_provisioning.c
esp_firmware/main/wifi_provisioning.h
Add an mDNS service helper that publishes the PSLab Pico bridge as pslab-pico.local with a _pslab._tcp service endpoint.
  • Implemented mdns_service_start() that initializes the mdns component, sets hostname and instance name, registers a _pslab._tcp service with TXT metadata, handles errors by logging and freeing mdns, and guards against double-start.
  • Declared the mDNS helper interface in mdns_service.h and integrated it via include in main.c.
  • Added an idf_component.yml declaring a dependency on the pinned espressif/mdns managed component.
esp_firmware/main/mdns_service.c
esp_firmware/main/mdns_service.h
esp_firmware/main/idf_component.yml
Update build configuration to include new source files and required ESP-IDF components.
  • Extended esp_firmware/main/CMakeLists.txt SRCS to include wifi_provisioning.c and mdns_service.c.
  • Added component REQUIRES list for driver, esp_event, esp_http_server, esp_hw_support, esp_netif, esp_timer, esp_wifi, lwip, mdns, and nvs_flash.
  • Introduced dependencies.lock to lock managed component versions (contents not shown in diff).
esp_firmware/main/CMakeLists.txt
esp_firmware/dependencies.lock
Refresh README documentation to describe the provisioning AP flow and mDNS-based access to the SCPI endpoint.
  • Documented first-boot behavior: temporary setup AP, local web setup page at http://192.168.4.1, default provisioning password, and re-entry into setup mode when station connection fails.
  • Removed instructions for configuring station SSID/password via Kconfig and replaced with provisioning AP password configuration reference.
  • Documented mDNS host name pslab-pico.local, _pslab._tcp service, example nc usage, and network/mDNS requirements.
esp_firmware/README.md

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="esp_firmware/main/main.c" line_range="201-210" />
<code_context>
+static bool start_station(wifi_provisioning_credentials_t const *credentials)
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider more consistent cleanup when station start fails

In `start_station`, failures from `esp_wifi_set_mode`, `esp_wifi_set_config`, or `esp_wifi_start` return `false` without calling `esp_wifi_stop()`. This can leave the Wi-Fi driver partially configured if earlier calls succeeded. Since you already stop Wi-Fi when `esp_wifi_set_ps` fails, consider also calling `esp_wifi_stop()` on these earlier failures to keep error handling consistent and avoid issues on retries.

Suggested implementation:

```c
static bool start_station(wifi_provisioning_credentials_t const *credentials)
{
    wifi_config_t wifi_config = { 0 };

    strncpy((char *)wifi_config.sta.ssid, credentials->ssid, sizeof(wifi_config.sta.ssid));
    strncpy((char *)wifi_config.sta.password, credentials->password, sizeof(wifi_config.sta.password));

    if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set WiFi mode to STA");
        // Stop Wi-Fi to clean up any partial configuration before returning.
        esp_wifi_stop();
        return false;
    }

    if (esp_wifi_set_config(WIFI_IF_STA, &wifi_config) != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set WiFi station configuration");
        // Stop Wi-Fi to clean up any partial configuration before returning.
        esp_wifi_stop();
        return false;
    }

    if (esp_wifi_start() != ESP_OK) {
        ESP_LOGE(TAG, "Failed to start WiFi station");
        // Stop Wi-Fi to clean up any partial configuration before returning.
        esp_wifi_stop();
        return false;
    }

    if (esp_wifi_set_ps(WIFI_PS_MAX_MODEM) != ESP_OK) {
        ESP_LOGE(TAG, "Failed to set WiFi power save mode");
        esp_wifi_stop();
        return false;
    }

    return true;
}

```

- If your actual `start_station` implementation differs (e.g., different logging messages, no `wifi_config_t` initialization, or different power-save settings), adjust the `SEARCH` block to match the exact existing code so the replacement applies cleanly.
- Ensure `esp_wifi_stop()` is safe to call in your initialization sequence; if you have additional state tracking around Wi-Fi initialization, you may want to guard these `esp_wifi_stop()` calls with that state (e.g., only stopping if Wi-Fi has been initialized).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread esp_firmware/main/main.c
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.

2 participants