# 47.RadioPower - WiFi radio power controls

**Layer:** L7 Application · **Build flags:** `DETWS_ENABLE_RADIO_POWER` (+ `DETWS_RADIO_WIFI_PS`, optional `DETWS_RADIO_MAX_TX_DBM`)

## What this example teaches

On a battery device the WiFi radio dominates the power budget. This applies a WiFi
modem-sleep mode (and an optional max-TX-power cap) after the link comes up, trading
a little latency for lower average current. `GET /radio` reports the mode read back
from the radio.

**Apply after the link is up.** The WiFi connect path may set its own default first,
so the settings are applied once association completes:

```cpp
detws_radio_power_apply();   // applies the build-flag-configured modem-sleep / TX cap
Serial.printf("radio modem-sleep: %s\n", detws_radio_ps_name(detws_radio_ps_get()));
```

**The mode is a build flag, not a runtime call**, so it reaches the
separately-compiled library: `DETWS_RADIO_WIFI_PS` is `0` (none), `1` (min modem
sleep), or `2` (max modem sleep), with an optional `DETWS_RADIO_MAX_TX_DBM` cap.
`detws_radio_ps_get()` reads the live mode back and `detws_radio_ps_name()` turns
it into a string for the endpoint.

## Build and run

```sh
pio ci --board=esp32dev --project-option="framework=arduino" \
  --project-option="build_flags=-DDETWS_ENABLE_RADIO_POWER=1 -DDETWS_RADIO_WIFI_PS=1" \
  --lib="." examples/L7-Application/47.RadioPower/47.RadioPower.ino
```

```sh
curl http://<ip>/radio   # {"modem_sleep":"min"}
```

## Annotated source

The complete sketch ([47.RadioPower.ino](47.RadioPower.ino)), reproduced verbatim
with added explanatory comments:

```cpp
// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later

#define DETWS_ENABLE_RADIO_POWER 1

#include "dwserver.h"
#include "network_drivers/physical/physical.h"
#include "services/radio_power/radio_power.h"
#include <WiFi.h>

static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";

DetWebServer server;

void setup()
{
    Serial.begin(115200);
    init_wifi_physical(SSID, PASSWORD);
    while (!wifi_ready())
        delay(250);
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());

    // Apply the configured modem-sleep / TX settings AFTER the link is up (the
    // WiFi connect path may set its own default first).
    detws_radio_power_apply();
    Serial.printf("radio modem-sleep: %s\n", detws_radio_ps_name(detws_radio_ps_get()));

    server.on("/radio", HTTP_GET, [](uint8_t id, HttpReq *) {
        char b[48];
        snprintf(b, sizeof(b), "{\"modem_sleep\":\"%s\"}", detws_radio_ps_name(detws_radio_ps_get()));
        server.send(id, 200, "application/json", b);
    });
    server.begin(80);
}

void loop()
{
    server.handle();
}
```
