Wio Terminal MQTT client using Arduino IDE

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.

MQTT Overview

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.

Project overview

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 Termnal to the MQTT broker using Arduino IDE.

More Wio Terminal resources:
How to build a Weather station using Wio Terminal
Wio Terminal Machine Learning: KNN classifier

Connecting the Wio to Wifi

The first thing is connecting the device to the Wifi using the code blow:

#include "AtWiFi.h"
char *ssid = "wifi_ssid";
char *pwd = "wifi_password";
WiFiClient wifiCliient = WiFiClient();
void connectToWiFi() {
  Serial.print("Connecting to ");
  WiFi.begin(ssid, pwd);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("Connected to Wifi");
  writeLine("Connected to WiFi", TFT_GREEN);
}Code language: C++ (cpp)

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:

PubSub MQTT client using Wio Terminal with Arduino IDE

Add the following line:

#include <PubSubClient.h>Code language: CSS (css)

and then:

IPAddress mqttServer(192,168,1,143);

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:

WiFiClient wifiCliient = WiFiClient();
PubSubClient mqttClient(mqttServer, 1883, callback, wifiCliient);

Notice 1883 is the MQTT port and we will cover callback function later. Finally add the following lines:

void reconnect() {
  Serial.println("Connecting to MQTT Broker...");
  while (!mqttClient.connected()) {
      Serial.println("Reconnecting to MQTT Broker..");
      String clientId = "Wio-Client-";
      clientId += String(random(0xffff), HEX);
      if (mqttClient.connect(clientId.c_str())) {
        writeLine("Connected to MQTT", TFT_GREEN);
        // subscribe to topic
}Code language: JavaScript (javascript)

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:

mqttClient.subscribe("/channel2");Code language: JavaScript (javascript)

In this example, Wio Terminal subscribes to two different topics:

  • channel1
  • channel2

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:

PubSubClient mqttClient(mqttServer, 1883, callback, wifiCliient);

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:

void displayValues(char *topic, byte *payload, unsigned int length) {
  ypos += yoffset;
  tft.setCursor(xoffset, ypos);
  tft.print("Topic: ");
  // Message
  ypos += yoffset;
  tft.setCursor(xoffset, ypos);
  tft.print("Message: ");
  for (int i=0; i < length; i++)
    tft.print((char) payload[i]);
void callback(char* topic, byte* payload, unsigned int length) {
  displayValues(topic, payload, length);
}Code language: C++ (cpp)

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:

MQTT Wio Terminal Client

Now let us send a new message using the topic channel1:

MQTT test using Wio Terminal

this is the result on Wio device:

Wio Terminal MQTT client that subscribes to a an MQTT topic

Wrapping up

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:

Tags: , ,
  • Add Your Comment