In this project, you learn how to use the MQTT Client library to send messages to an MQTT broker. The MQTT client library we used is part of the Eclipse Paho project, which provides APIs for using MQTT over multiple means of transport.

What you need

Finish the Getting Started Guide to:

  • Have your DevKit connected to Wi-Fi
  • Prepare the development environment

Step 1. Open the project folder

  1. Disconnect the DevKit from your computer, if it is already connected.

  2. Start VS Code.

  3. Connect the DevKit to your computer. VS Code automatically detects your DevKit and opens the following pages:

    • The DevKit introduction page.
    • Arduino Examples: Hands-on samples to get you started with your DevKit. Links to these two pages appear on the EXPLORER panel on the left side of the Visual Studio Code window. mini-solution-vscode

Step 2. Open the MQTTClient Arduino sample sketch

Expand left side Aduino Examples section, browse to Examples for MXCHIP AZ3166 > MQTT, and select MQTTClient. This will open a new VS Code window with a project folder in it.


If you happen to close the pane, you can reopen it. Use Ctrl+Shift+P (macOS: Cmd+Shift+P) to open the command palette, type Arduino, and then find and select Arduino: Examples.

Step 3. Build and upload the Arduino sketch to the DevKit

Type Ctrl+P (macOS: Cmd+P) to run task device-upload. Once the upload is completed, DevKit restarts and runs the sketch.


Notice: Occasionally, you may get the “Error: AZ3166: Unknown package” message. This occurs when the board package index is not refreshed. Check this FAQ steps to resolve this issue.

Test the project

In VS Code, follow these steps to open and set up the Serial Monitor:

  1. Click the COM[X] word on the status bar to set the correct COM port with STMicroelectronics: set-com-port

  2. Click the power plug icon on the status bar to open the Serial Monitor: serial-monitor

  3. On the status bar, click the number that represents the Baud Rate and set it to 115200: set-baud-rate

The Serial Monitor displays all the messages sent by the sample sketch. When the sketch runs, it connects the DevKit to Wi-Fi. When the Wi-Fi connection is successful, the sketch sends a message to the MQTT broker. After that, the sketch repeatedly sends two “” messages using QoS 0 and QoS 1, respectively.


Problems and feedback

If you encounter problems, you can refer to FAQs or reach out to us from the channels below.

See Also