udp_server

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

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

Publisher side of the [`wifi_aware`](../../) UDP demo. This example advertises a
NAN service (`ESP-Demo` / `_esp-demo._udp` by default) and runs a UDP server that echoes back each
datagram it receives, prefixed with `OK: `. The transport and NAN protocol handling are kept in
`example_wifi_aware.c`; `udp_server.c` only does the UDP socket work.

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

## Flow

```mermaid
sequenceDiagram
    autonumber
    participant App as app_main (udp_server)
    participant WA as example_wifi_aware
    participant Peer as Subscriber (udp_client)
    participant UDP as udp_server_task

    App->>App: nvs_flash_init / esp_netif_init / event_loop
    App->>App: register IP_EVENT_GOT_IP6 handler
    App->>App: build hostname from base MAC
    App->>App: esp_wifi_init()
    App->>WA: example_wifi_aware_start(erase=false)
    WA->>WA: wifi_aware_init(trigger=WA_TRIGGER_NONE, hostname)
    WA->>WA: register WA_EVENT_BOOTSTRAP_INDICATION
    WA->>WA: wifi_aware_service_advertise(svc, port, security, 0)
    Note over WA,Peer: Service advertised over NAN

    alt PIN-pairing security enabled
        Peer-->>WA: bootstrap request from subscriber
        WA->>WA: WA_EVENT_BOOTSTRAP_INDICATION
        WA->>WA: init_pairing_pin() — log 6-digit PIN
        WA->>WA: wifi_aware_bootstrap_response(NULL, accept=true)
        WA->>WA: wifi_aware_set_credentials(pin)
        WA-->>Peer: NAN pairing confirm
    end

    Note over WA,Peer: NAN datapath brought up by subscriber
    WA-->>App: IP_EVENT_GOT_IP6 on NAN netif
    App->>UDP: xTaskCreate(udp_server_task)
    loop echo loop
        Peer->>UDP: recvfrom() — "Message from ESP32"
        UDP->>Peer: sendto() — "OK: Message from ESP32"
    end
```

## Build and flash

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

Run together with [`udp_client`](../udp_client/) 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_server"

or download archive (~8.95 KB)