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
Disconnect the DevKit from your computer, if it is already connected.
Start VS Code.
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.
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
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
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:
COM[X]word on the status bar to set the correct COM port with
Click the power plug icon on the status bar to open the Serial Monitor:
On the status bar, click the number that represents the Baud Rate and set it to
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 “iot.eclipse.org” 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.