> ## Documentation Index
> Fetch the complete documentation index at: https://launchpad.anxlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect ANX Dev Module to Wi-Fi and Cloud MQTT

> Connect your ANX dev module to a Wi-Fi network and publish sensor readings to an MQTT broker over TLS for cloud-based monitoring.

This example shows you how to connect your ANX dev module to a Wi-Fi network and publish sensor data to an MQTT broker using TLS — the foundation for any cloud-connected IoT project. By the end you'll have a device that automatically reconnects to your network on boot, reads the onboard ADC, and streams JSON payloads to a topic of your choice every five seconds.

## Prerequisites

Before you flash this example, make sure you have the following ready:

* Your Wi-Fi SSID and password (2.4 GHz network — the ANX dev module does not support 5 GHz)
* Access to an MQTT broker with TLS on port 8883 — [HiveMQ Cloud free tier](https://www.hivemq.com/mqtt-cloud-broker/) works out of the box, or run a local [Mosquitto](https://mosquitto.org/) instance
* ANX dev module running firmware **1.1.0 or later** (TLS support was added in 1.1.0)

## Configure Wi-Fi and MQTT

All runtime credentials live in `config.json` at the root of the example directory. Open the file and replace the placeholder values with your own before flashing — the ANX OS reads this file at boot and passes the settings to the Wi-Fi and MQTT drivers automatically.

```json config.json theme={null}
{
  "wifi": {
    "ssid": "YourNetwork",
    "password": "YourPassword",
    "mode": "station"
  },
  "mqtt": {
    "host": "broker.hivemq.com",
    "port": 8883,
    "tls": true,
    "client_id": "anx-device-001",
    "username": "your-username",
    "password": "your-password"
  }
}
```

<Warning>
  Never hard-code your Wi-Fi password, MQTT username, or MQTT password directly in source code. Credentials embedded in `.c` files are trivial to extract from a compiled firmware binary and will end up in version control. Always place secrets in `config.json`, which is excluded from the examples repository by `.gitignore`.
</Warning>

## Application Code

The application connects to Wi-Fi, establishes a TLS-secured MQTT session, then enters a loop that reads the ADC, formats a JSON payload, and publishes it every five seconds.

```c connectivity.c theme={null}
#include "anx/anx.h"
#include "anx/wifi.h"
#include "anx/mqtt.h"
#include "anx/adc.h"
#include "anx/log.h"
#include <stdio.h>

#define TOPIC "anx/device/sensors"

void app_main(void) {
    // Connect to Wi-Fi (reads ssid/password from config.json)
    anx_wifi_connect();
    ANX_LOGI("WiFi", "Connected. IP: %s", anx_wifi_get_ip());

    // Connect to MQTT broker (reads host/port/tls from config.json)
    anx_mqtt_connect();
    ANX_LOGI("MQTT", "Connected to broker");

    // Init ADC
    anx_adc_init(ANX_ADC0);

    while (1) {
        // Read sensor
        uint16_t raw     = anx_adc_read(ANX_ADC0);
        float    voltage = (raw / 4095.0f) * 3.3f;

        // Build JSON payload
        char payload[64];
        snprintf(payload, sizeof(payload),
                 "{\"voltage\":%.2f,\"raw\":%d}", voltage, raw);

        // Publish
        anx_mqtt_publish(TOPIC, payload, ANX_MQTT_QOS1);
        ANX_LOGI("MQTT", "Published: %s", payload);

        anx_delay_ms(5000);
    }
}
```

## Run and Verify

Flash the example and open the serial monitor to confirm that both the Wi-Fi and MQTT connections succeed before the publish loop begins:

```bash theme={null}
anx run examples/connectivity
anx monitor
```

Within a few seconds of the module resetting you should see:

```
[WiFi] Connected. IP: 192.168.1.42
[MQTT] Connected to broker
[MQTT] Published: {"voltage":1.65,"raw":2048}
```

A new `Published` line appears every five seconds. If the Wi-Fi connection drops, `anx_wifi_connect()` automatically retries in the background — MQTT publishes resume once the link is restored.

## Subscribing to Messages

To verify that your payloads are actually reaching the broker, subscribe to the same topic from your desktop using the `mosquitto_sub` CLI client:

```bash theme={null}
mosquitto_sub -h broker.hivemq.com -p 8883 --capath /etc/ssl/certs \
  -u your-username -P your-password -t "anx/device/sensors"
```

Each time your device publishes you'll see a line like the following printed to your terminal:

```json theme={null}
{"voltage":1.65,"raw":2048}
```

<Tabs>
  <Tab title="Linux / macOS">
    Install `mosquitto-clients` via your package manager (`apt install mosquitto-clients` or `brew install mosquitto`), then run the `mosquitto_sub` command above. The `--capath /etc/ssl/certs` flag points to the system CA bundle, which is sufficient for public brokers such as HiveMQ Cloud.
  </Tab>

  <Tab title="Windows">
    Download the Mosquitto installer from [mosquitto.org](https://mosquitto.org/download/). After installation, open a Command Prompt in the Mosquitto directory and replace `--capath /etc/ssl/certs` with `--cafile "C:\Program Files\mosquitto\certs\ca-bundle.crt"` in the subscribe command.
  </Tab>
</Tabs>

<Note>
  ANX OS bundles a current set of root CA certificates in the firmware image, so setting `"tls": true` in `config.json` is all you need for TLS to work — there is no certificate provisioning step on the device side. The bundled CA store is updated with each ANX OS release; keep your firmware current to stay compatible with broker certificate renewals.
</Note>
