Send messages to an MQTT server
Internet of Things (IoT) systems often deal with intermittent, poor quality, or slow internet connections. MQTT is a machine-to-machine (M2M) connectivity protocol, which was developed with such challenges in mind.
The MQTT client library used here is part of the Eclipse Paho project, which provides APIs for using MQTT over multiple means of transport.
What you learn
In this project, you learn: - How to use the MQTT Client library to send messages to an MQTT broker. - How to configure your MXChip Iot DevKit as an MQTT client.
What you need
Finish the Getting Started Guide to:
- Have your DevKit connected to Wi-Fi
- Prepare the development environment
Open the project folder
If the DevKit is already connect to your computer, disconnect it.
Start VS Code.
Connect the DevKit to your computer.
Open the MQTTClient Sample
Expand left side ARDUINO EXAMPLES section, browse to Examples for MXCHIP AZ3166 > MQTT, and select MQTTClient. A new VS Code window opens with a project folder in it.
[NOTE] You can also open example from command palette. Use
Cmd+Shift+P) to open the command palette, type **Arduino, and then find and select Arduino: Examples.**
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.
[NOTE] You may receive an "Error: AZ3166: Unknown package" error message. This error occurs when the board package index is not refreshed correctly. To resolve this error, refer to the development section of the IoT DevKit FAQ.
Test the project
In VS Code, follow this procedure to open and set up the Serial Monitor:
COM[X]word on the status bar to set the right 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. The sketch connects the DevKit to Wi-Fi. Once the Wi-Fi connection is successful, the sketch sends a message to the MQTT broker. After that, the sample repeatedly sends two "iot.eclipse.org" messages using QoS 0 and QoS 1, respectively.
Problems and feedback
If you encounter problems, refer to the IoT DevKit FAQ or connect using the following channels:
Now that you have learned how to configure your MXChip Iot DevKit as an MQTT client and use the MQTT Client library to send messages to an MQTT broker, here are the suggested next steps: