Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Wi-Fi Connectivity

In this chapter we connect the board to a local Wi-Fi network, obtain an IP address, and send each sensor reading to a computer on the same network. The completed code for this chapter is available in project/part2/.

The HAL initialization, I2C setup, and SHTC3 measurement sequence from Reading Sensor Data stay the same. What changes is the structure of the project and the new networking pieces: a heap, a network stack, background tasks, and compile-time credentials.

From One File to Modules

The previous chapter kept everything in main.rs. Once we add Wi-Fi and HTTP, that file would become hard to follow, so we move related code into modules:

mod http;
mod network;
mod sensor;
  • sensor.rs holds the SHTC3 measurement helper that used to live in the main loop. The I2C bus is still created in main, but starting a measurement, waiting for it, and converting the result now live in read_sensor:
pub async fn read_sensor(sht: &mut ShtC3<I2c<'static, esp_hal::Async>>) -> Option<(f32, f32)> {
    // Read sensor
    if let Err(e) = sht.start_measurement(PowerMode::NormalMode).await {
        error!("Failed to start measurement: {:?}", e);
        return None;
    }
    // Wait for the maximum measurement duration reported by the sensor driver.
    let duration = max_measurement_duration(sht, PowerMode::NormalMode);
    Timer::after(Duration::from_micros(duration.into())).await;

    match sht.get_measurement_result().await {
        Ok(m) => {
            let temp = m.temperature.as_degrees_celsius();
            let humidity = m.humidity.as_percent();
            info!("  {:.2} °C | {:.2} %RH", temp, humidity);
            Some((temp, humidity))
        }
        Err(e) => {
            error!("Failed to get measurement result: {:?}", e);
            None
        }
    }
}
  • network.rs owns the two background tasks that keep the radio associated and the IP stack running.
  • http.rs builds a JSON payload and sends it with a minimal HTTP POST.
  • main.rs still initializes the hardware, starts the network stack, and runs the application loop.

This split keeps the new networking concepts easier to read, and the same modules will grow in later chapters.

Why We Need esp-alloc

A no_std program has no global allocator unless we provide one. The previous chapter never needed a heap: the sensor driver works with stack buffers, and we never built a String or a Vec.

Wi-Fi is different. esp-radio wraps the precompiled radio driver, and that driver allocates at runtime. embassy-net and our HTTP helper also need the alloc crate, for example when format! builds the JSON body. Without a heap, the application would not even start the radio.

We therefore add esp-alloc and enable the alloc crate:

extern crate alloc;
use esp_alloc as _;

extern crate alloc; makes Vec, String, and format! available in no_std. use esp_alloc as _; pulls in the crate so its global allocator is linked, even though we do not call it by name.

Then we create the heap before starting the scheduler or the radio:

esp_alloc::heap_allocator!(#[ram(reclaimed)] size: 64 * 1024);
esp_alloc::heap_allocator!(size: 36 * 1024);

heap_allocator! can be invoked more than once. Each invocation adds a region to the same global allocator. For the ESP32-C3 used in this training, the calls reserve two heap regions:

  • 64 KiB of reclaimed memory, selected by #[ram(reclaimed)].
  • Another 36 KiB of memory.

See Allocating Memory in The Rust on ESP Book for more details.

esp-radio requires both a heap and a running scheduler. We still call esp_rtos::start(...) before esp_radio::wifi::new(...), just as the crate documentation requires.

Initializing the Network Stack

Networking is split into two layers. esp-radio drives the Wi-Fi hardware. embassy-net is the IP stack that sits on top of that interface and gives us DHCP, DNS, and TCP.

Creating the Wi-Fi Interface

After the scheduler is running, we take ownership of the Wi-Fi peripheral and create a station-mode interface:

let (controller, interfaces) = esp_radio::wifi::new(peripherals.WIFI, Default::default())
    .expect("Failed to create WiFi controller");

let wifi_interface = interfaces.station;

esp_radio::wifi::new returns a WifiController and a set of interfaces. The controller associates with an access point. interfaces.station is the embassy-net driver: a Wi-Fi client that can send and receive Ethernet frames.

embassy-net Configuration

embassy-net is configured in two places: Cargo features, and the Config value we pass at runtime.

The crate features select which protocols are compiled in:

embassy-net = { version = "0.9.1", features = [
  "dhcpv4",
  "dns",
  "medium-ethernet",
  "tcp",
  "udp",
] }
  • medium-ethernet is required because a Wi-Fi station presents an Ethernet-like interface.
  • dhcpv4 lets the stack request an IPv4 address from the access point.
  • udp is used by DHCP and DNS.
  • tcp and dns are used later when we open a socket and resolve a host.

We also enable Embassy support in esp-rtos, and the wifi feature on esp-radio:

esp-radio = { version = "0.18.0", features = [
  "esp32c3",
  "log-04",
  "unstable",
  "wifi",
] }
esp-rtos = { version = "0.3.0", features = [
  "embassy",
  "esp-radio",
  "esp32c3",
  "log-04",
] }

At runtime we ask for a DHCP lease and seed the stack from the hardware RNG:

let config = embassy_net::Config::dhcpv4(Default::default());

let rng = Rng::new();
let seed = (rng.random() as u64) << 32 | rng.random() as u64;

Config::dhcpv4 is the embassy-net equivalent of “get an address from the router”. A static address would also work, but DHCP is the usual setup on a development network and avoids hard-coding an IP in firmware.

The random seed is not a cryptographic key. embassy-net uses it for protocol values that must not be predictable across devices, such as TCP initial sequence numbers and DHCP transaction IDs.

Why the Stack Resources Are Static

embassy_net::new needs a Wi-Fi driver, the config, a random seed, and a StackResources<N> buffer. N is the number of sockets the stack can keep open at once. We use 3, which is enough for DHCP, DNS, and one TCP connection.

// Init network stack
static STACK_RESOURCES_CELL: static_cell::StaticCell<StackResources<3>> =
    static_cell::StaticCell::new();
let (stack, runner) = embassy_net::new(
    wifi_interface,
    config,
    STACK_RESOURCES_CELL
        .uninit()
        .write(StackResources::<3>::new()),
    seed,
);

Those resources, the Runner, and the spawned tasks all need a 'static lifetime. Embassy tasks are not allowed to borrow data from main’s stack: once spawned, a task can run for the rest of the program, so every argument must outlive main.

A plain local let resources = StackResources::new(); would be dropped when main returned (it never does, but the type system cannot use that fact) and could not be given to a 'static task. A static mut would compile only with unsafe.

StaticCell solves this. It is a one-time cell: we initialize it at runtime, then get a &'static mut StackResources<3>. After that, embassy_net::new returns a Stack we can use from application code and a Runner that must be polled continuously.

Why We Use Tasks

The stack is not useful until two loops run in the background:

spawner.spawn(connection(controller).expect("failed to spawn connection task"));
spawner.spawn(net_task(runner).expect("failed to spawn network task"));

Both functions are Embassy tasks, created with #[embassy_executor::task].

connection owns the WifiController. It configures station mode, connects, and if the link drops it waits and tries again:

#[embassy_executor::task]
pub async fn connection(mut controller: WifiController<'static>) {
    debug!("start connection task");
    loop {
        if controller.is_connected() {
            let _ = controller.wait_for_disconnect_async().await;
            Timer::after(Duration::from_millis(5000)).await;
            continue;
        }

        let station_config = WifiConfig::Station(
            StationConfig::default()
                .with_ssid(SSID)
                .with_password(PASSWORD.into()),
        );
        controller
            .set_config(&station_config)
            .expect("Failed to set WiFi configuration");

        debug!("About to connect...");
        match controller.connect_async().await {
            Ok(_) => info!("Wifi connected!"),
            Err(e) => {
                error!("Failed to connect to wifi: {e:?}");
                Timer::after(Duration::from_millis(5000)).await
            }
        }
    }
}

net_task owns the Runner. Its only job is to keep polling the IP stack:

#[embassy_executor::task]
pub async fn net_task(mut runner: Runner<'static, Interface<'static>>) {
    runner.run().await
}

We use tasks because these two loops never finish, and they must make progress at the same time as the application. If main called runner.run().await itself, it would never read the sensor or send HTTP. If it only ran the connection loop, DHCP and TCP would stall. Embassy’s executor interleaves the tasks whenever one of them .awaits, so the radio, the IP stack, and the application loop can all run on a single core.

The 'static lifetimes on WifiController and Runner are the same constraint as before: a spawned task may outlive the function that spawned it.

Environment Variables

The network name and password are not written as string literals. They are read at compile time:

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("PASSWORD");

env! expands to a &'static str taken from the environment when rustc runs. If SSID or PASSWORD is missing, the build fails. That keeps credentials out of the source tree, and it means you pass them when you build or flash:

SSID="your-ssid" PASSWORD="your-password" cargo run --release

Cargo forwards process environment variables to the compiler, so these values are baked into the firmware. Changing the network later requires rebuilding.

The HTTP helper uses optional variables for the receiver on your computer:

const HOST_IP: Option<&'static str> = option_env!("HOST_IP");
const HTTP_PORT: Option<&'static str> = option_env!("HTTP_PORT");
const DEFAULT_HTTP_PORT: u16 = 8080;

option_env! yields None when the variable is unset, so the firmware still builds. At runtime, send_sensor_data logs an error and returns if HOST_IP is missing. HTTP_PORT defaults to 8080.

Waiting for an Address, Then Sending Data

After the tasks are spawned, main waits until the link is up and DHCP has assigned an address:

stack.wait_link_up().await;

debug!("Waiting to get IP address...");
loop {
    if let Some(config) = stack.config_v4() {
        debug!("Got IP: {}", config.address);
        break;
    }
    embassy_time::Timer::after(Duration::from_millis(500)).await;
}

wait_link_up() completes when the station is associated. The following loop waits until config_v4() is Some, which means the DHCP lease is in place. Only then do we start sending.

The application loop is now two calls: read the sensor, and if that succeeds, post the values:

// Read sensor
if let Some((temp, humidity)) = read_sensor(&mut sht).await {
    // Send sensor data via HTTP
    let _ = send_sensor_data(stack, temp, humidity).await;
}

send_sensor_data formats a small JSON body, opens a TCP socket, and writes a hand-built HTTP/1.0 POST to /sensor:

// Prepare HTTP payload (JSON)
let temperature_str = format!("{:.2}", temperature);
let humidity_str = format!("{:.2}", humidity);
let body = format!(
    r#"{{"temperature":{},"humidity":{}}}"#,
    temperature_str, humidity_str
);

This chapter uses HTTP only as a simple way to prove that the device can reach another host. The next chapter replaces this with MQTT.

Running the Application

The firmware posts to a computer on the same network. From the repository root, start the receiver first:

cargo xtask http-server

The task prints a host IP when it can detect one. Use that value as HOST_IP. Then, from project/part2/, flash the application:

SSID="your-ssid" PASSWORD="your-password" HOST_IP="192.168.1.10" cargo run --release

Replace the SSID, password, and IP with your network and the address printed by xtask. You can also set HTTP_PORT if the server is not on 8080.

Once the device associates, you should see a connection log on the serial monitor:

INFO - Wifi connected!
INFO -   24.31 °C | 42.18 %RH
INFO - HTTP request sent

The http-server terminal should print each payload:

sensor payload={"temperature":24.31,"humidity":42.18}

At this point the board can join a Wi-Fi network, obtain an IP address, and send measurements off-device. In the next chapter we will publish those readings to an MQTT broker.