Skip to main content
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 works out of the box, or run a local Mosquitto 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.
config.json
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.

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.
connectivity.c

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:
Within a few seconds of the module resetting you should see:
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:
Each time your device publishes you’ll see a line like the following printed to your terminal:
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.
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.