Wio Terminal MQTT Client Using Arduino IDE
Learn how to use the SeeedStudio Wio Terminal with MQTT.
Join the DZone community and get the full member experience.Join For Free
This tutorial covers how to create a MQTT client using Wio Terminal and Arduino IDE. We will use Wio Terminal to subscribe to MQTT topic and receive messages. Moreover, we will use the Wio Terminal LCD display to shows the MQTT messages.
Before diving into the Wio Terminal MQTT client, it is useful to have a brief overview of the MQTT protocol. MQTT protocol is a lightweight protocol designed for M2M. It is based on the publish-subscribe paradigm. There are several clients and a message broker. These MQTT clients are connected to the message broker. Clients publish messages and the message broker dispatches these messages to other clients. An MQTT client publishes messages using a topic. A topic is a virtual channel between the publisher and the subscriber. The message broker uses the topic to organize the messages and dispatches them to the clients that subscribed to the topic. If you'd like to have more information you can read this tutorial about the MQTT protocol. One important aspect you have to consider is that the MQTT uses plain text messages. If you want to make it secure you should read how to make MQTT secure.
In this tutorial, Wio Terminal subscribes to several topics and receives messages through a message broker. In our example, the MQTT message broker is a Raspberry Pi that uses Mosquitto.
Wio Terminal MQTT client is developed using PubSubclient Library. Moreover, we will use the Arduino IDE. Before using the Wio Terminal you should verify that you followed this guide.
Moreover, it is necesary to import all the libraries described in the guide into the Arduino IDE. In this way, Wio Terminal can connect to the Wifi before using the MQTT protocol.
Wio Terminal MQTT client subscribes to two different MQTT topics to receive messages. Using the Wio Terminal built-in LCD display, we will show the incoming mesages. In more details, we will learn:
- How to connect Wio Terminal to a MQTT broker using PubSub library
- Subscribe the Wio Terminal to different MQTT topics
- Receive MQTT message using topics
- Show the MQTT message using the built-in LCD display
How to Connect Wio Terminal to MQTT Broker Using PubSub
In this first step, we will develop the code to connect Wio Terminal to the MQTT broker using Arduino IDE.
Connecting the Wio to Wifi
The first thing is connecting the device to the Wifi using the code below:
Configuring the PubSub Library to Use With Wio Terminal
Before using the MQTT client, it is necessary to configure the PubSub library. It is necessary to add the library to the Arduino IDE:
Add the following line:
This is the MQTT broker address where the Wio Terminal will be connected. Replace this IP with the one of your MQTT broker.
Next, initialize the PubSub MQTT library that Wio Terminal MQTT client will use to exchange data with MQTT broker:
Notice 1883 is the MQTT port and we will cover the callback function later. Finally, add the following lines:
Subscribing Wio Terminal to MQTT topic
Once the Wio Terminal establishes the connection to the MQTT broker we can subscribe to the topics. In the previous code there are two lines:
In this example, Wio Terminal subscribes to two different topics:
The device uses these two topics to receive data from the MQTT broker.
Receiving MQTT Message Using Wio Terminal
In this last step, we will implement the code so that the Wio Terminal MQTT client can receive and handle the incoming message. Previously, when we have configured the PubSub client:
we have defined a callback function. This callback function is invoked when a new MQTT message arrives. In this callback function, we will implement all the logic we need to handle the message. In this example, we simply want to display the topic and the message. Therefore, the code is:
Add this code to your sketch using Arduino IDE. That's all your client is ready.
Testing the Wio Terminal MQTT Client
To test the Wio Terminal MQTT client we will use a Mosquitto MQTT broker running on Raspberry Pi and MQTT.fx (download it here).
Run the code on your Wio Terminal and at the beginning you will see:
Now let us send a new message using the topic channel1:
this is the result on Wio device:
In this tutorial you have learned how to develop a Wio Terminal MQTT client that subscribes to MQTT topic to receive messages. You can use this MQTT client to send messages to Wio Terminal using different topics. You can monitor your rooms and with other devices and sensors and send data to the Wio Terminal using MQTT.
If you want to explore how to use MQTT you can read:
If you want to find more about Wio Terminal read:
Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.