---
title: "IoT (Wi-Fi + MQTT)"
description: "Connectivity helpers for putting an E1M module on the network."
source: https://docs.alplab.ai/sdk/api/iot
---

# `<alp/iot.h>` — Wi-Fi & MQTT

Connectivity helpers for putting an E1M module on the network.

## Header

```c
#include <alp/iot.h>
```

## Wi-Fi station

```c
alp_wifi_t *wifi = alp_wifi_open();          /* station singleton */
if (wifi == NULL) {
    /* alp_last_error(): ALP_ERR_NOSUPPORT or ALP_ERR_NOT_READY */
}

alp_status_t s = alp_wifi_connect(wifi, &(alp_wifi_credentials_t){
    .ssid = "MyNetwork",
    .psk  = "secret",                        /* NULL for an open network */
}, 15000u);                                  /* timeout, ms */

/* ... */
alp_wifi_disconnect(wifi);
alp_wifi_close(wifi);
```

`alp_wifi_connect()` blocks until the association completes, and returns `ALP_OK`, `ALP_ERR_TIMEOUT`, `ALP_ERR_IO` (authentication failed), `ALP_ERR_INVAL` or `ALP_ERR_NOSUPPORT`. There is no separate connection-state poll.

On AEN this flows through the [CC3501E coprocessor](https://docs.alplab.ai/e1m/e1m-aen#cc3501e-coprocessor). On V2N / V2N-M1 (Linux on the A55) `alp_wifi_*` has no real backend: the Yocto build links a fallback whose `alp_wifi_connect()` returns `ALP_ERR_NOT_IMPLEMENTED`. There the on-module Murata radio is brought up by Linux system configuration (wpa_supplicant / NetworkManager), and applications use the resulting network through plain sockets.

## MQTT

```c
alp_mqtt_t *mqtt = alp_mqtt_open(&(alp_mqtt_config_t){
    .broker_uri = "mqtt://broker.example.com:1883",
    .client_id  = "alp-device-001",
});

alp_mqtt_connect(mqtt, 5000u);   // timeout_ms
alp_mqtt_publish(mqtt, "sensors/temperature", (const uint8_t *)"23.5", 4,
                 ALP_MQTT_QOS_0, false);   // retain = false

void on_message(const char *topic, const uint8_t *payload, size_t len, void *user) {
    /* handle incoming message */
}
alp_mqtt_subscribe(mqtt, "actuators/+/cmd", ALP_MQTT_QOS_1, on_message, NULL);

// Drive the loop from your main thread, or spawn a worker
while (1) {
    alp_mqtt_loop(mqtt, 100);   // process pending events, 100 ms budget
}

alp_mqtt_close(mqtt);
```

## TLS (`mqtts://`)

```c
alp_mqtt_t *mqtt = alp_mqtt_open(&(alp_mqtt_config_t){
    .broker_uri = "mqtts://broker.example.com:8883",
    .client_id  = "alp-device-001",
    .tls = &(alp_mqtt_tls_config_t){
        .ca_file   = "/etc/alp/ca.pem",       // PEM CA bundle; NULL = system store
        .cert_file = "/etc/alp/device.crt",   // optional client certificate (PEM)
        .key_file  = "/etc/alp/device.key",   // optional client private key (PEM)
        .insecure  = false,                   // true skips peer verification (dev only)
    },
});
```

The Yocto backend routes TLS through OpenSSL (`mosquitto_tls_set`); the Zephyr backend uses MbedTLS configured via [`<alp/security.h>`](https://docs.alplab.ai/sdk/api/security).

:::warning[`mqtts://` now fails closed on a non-TLS build]
v0.10 fixes a silent plaintext downgrade ([GHSA-gqjv-932h-c5gm](https://github.com/alplabai/alp-sdk/security/advisories)). The Zephyr MQTT backend, when `CONFIG_MQTT_LIB_TLS` was **disabled**, discarded the parsed TLS result and always selected a non-secure transport — transmitting an `mqtts://` connection's credentials and payloads in **plaintext** while the app believed the scheme enforced TLS.

An `mqtts://` broker URI on a build without `CONFIG_MQTT_LIB_TLS` is now rejected with **`ALP_ERR_NOSUPPORT`** before any connection is attempted. If you relied on the old behaviour, you were not getting TLS — enable `CONFIG_MQTT_LIB_TLS` or switch the scheme to `mqtt://` deliberately.
:::

## `board.yaml`

```yaml
iot:
  wifi: true
  mqtt: true
  tls:  true
```

## Default-initialiser macro

`ALP_MQTT_CONFIG_DEFAULT(id)` (v0.10) takes the **broker URI** as its identity argument:

```c
alp_mqtt_config_t cfg = ALP_MQTT_CONFIG_DEFAULT("mqtt://broker.example.com:1883");
cfg.client_id = "e1m-node-1";
```

It sets `broker_uri` from `id` plus: `client_id = NULL`, `username = NULL`, `password = NULL`, `keepalive_s = 60`, `clean_session = true`, `tls = NULL` (no TLS).

See [the shared macro contract](https://docs.alplab.ai/sdk/api/peripheral#default-initialiser-macros) for the C++ compound-literal caveat that applies to every `ALP_*_CONFIG_DEFAULT`.

## See also

- [`<alp/security.h>`](https://docs.alplab.ai/sdk/api/security) — TLS / PSA Crypto
- [`<alp/ble.h>`](https://docs.alplab.ai/sdk/api/ble) — BLE 5.4
- [Examples: iot-connected-camera](https://github.com/alplabai/alp-sdk/tree/main/examples/connectivity/iot-connected-camera)
