udp_client

Example of the component espressif/wifi_aware v0.0.1
# Wi-Fi Aware UDP Client Example

| Supported Targets | ESP32 | ESP32-C5 | ESP32-C61 | ESP32-S2 | ESP32-S31 |
| ----------------- | ----- | -------- | --------- | -------- | --------- |

Subscriber side of the [`wifi_aware`](../../) UDP demo. This example discovers the
`ESP-Demo` / `_esp-demo._udp` service advertised by `udp_server`, runs the PIN-code bootstrap + pairing
exchange, resolves the peer's IPv6 link-local address + port, then periodically sends a UDP message and
reads the echoed reply. All NAN protocol handling lives in `example_wifi_aware.c`; `udp_client.c` only
does the UDP socket work.

See [`../README.md`](../README.md) for how this example pairs with `udp_server`, and the
[`wifi_aware` README](../../README.md) for the component API.

## Flow

```mermaid
sequenceDiagram
    autonumber
    participant App as app_main (udp_client)
    participant WA as example_wifi_aware_connect
    participant Pub as Publisher (udp_server)
    participant UDP as udp_client_run

    App->>App: nvs_flash_init / esp_netif_init / event_loop
    App->>App: esp_wifi_init()
    App->>App: pair_button_init() (optional)

    loop connect / rediscover loop
        App->>WA: example_wifi_aware_connect(&addr, &port, &netif, erase)
        WA->>WA: wifi_aware_init(trigger=WA_TRIGGER_NONE)
        WA->>WA: register WA_EVENT_BOOTSTRAP_COMPLETED
        WA->>WA: register WIFI_EVENT_NAN_PAIRING_CONFIRM
        WA->>Pub: wifi_aware_service_discover(svc, security, match)
        Pub-->>WA: discovery callback: wa_matched_peer_t

        alt PIN-pairing security enabled
            WA->>Pub: wifi_aware_bootstrap_request(instance, WA_BOOTSTRAP_PINCODE)
            Pub-->>WA: WA_EVENT_BOOTSTRAP_COMPLETED (accepted)
            WA->>Pub: wifi_aware_pairing_start(pin)
            Pub-->>WA: WIFI_EVENT_NAN_PAIRING_CONFIRM (accepted)
        end

        WA->>Pub: wifi_aware_request_connection(instance, &addr, &port)
        Pub-->>WA: IPv6 link-local + port
        WA-->>App: ESP_OK, peer endpoint + NAN netif index

        App->>UDP: udp_client_run(addr, port, netif_index)
        loop every 2 s
            UDP->>Pub: sendto() — "Message from ESP32"
            Pub-->>UDP: recvfrom() — "OK: Message from ESP32"
        end
        Note over App: on socket error: rediscover & reconnect
    end
```

## Build and flash

```bash
idf.py set-target esp32s2
idf.py build flash monitor
```

Run together with [`udp_server`](../udp_server/) on a second board of the same target.

To create a project from this example, run:

idf.py create-project-from-example "espressif/wifi_aware=0.0.1:udp_client"

or download archive (~12.39 KB)