How to build an Arduino MQTT client

This Arduino MQTT client describes how to build an MQTT client that uses MQTT to send and receive data. It happens quite often that we have to control, remotely, an Arduino board or a set of peripherals connected to Arduino itself remotely, such as a web interface. For this reason, this tutorial describes how to use MQTT to control Arduino through Ubidots that behaves as a broker that dispatches data.  We have already covered the MQTT protocol and how it works and we already know the MQTT protocol is used to send data from remote sensors. By the way, just to recap briefly MQTT is a lightweight protocol used in the telemetry. The MQTT protocol is an IoT messaging system. Therefore, it is used to exchange data between a central MQTT server called MQTT broker and one or more clients (MQTT clients).


Several IoT platforms support MQTT as IoT messaging system to exchange data with remote IoT devices. Usually, IoT boards use MQTT to connect to the IoT platforms that ingest information. In this scenario, the Arduino board uses MQTT to transfer data to the remote IoT cloud platform. Data comes from sensors for example. Therefore, the Arduino board is the client while the IoT cloud platform is the broker. As we know already, an MQTT broker can handle several MQTT clients. Applying the publisher/subscriber paradigm, a publisher (one client) publishes the information that is dispatched to other clients subscribed to the same channel through the broker.

Anyway, it is possible to use MQTT protocol in a slightly different way to send commands (not only sensor data) from an MQTT client to another MQTT client through an MQTT broker. In this way, the MQTT protocol can be used to control a remote device or peripherals connected to this device. Let us see how to implement this.

How to develop an Arduino MQTT client to control Arduino remotely through MQTT

In this project, we want to control a prototyping board (like Arduino MKR1000) using MQTT. In more detail, the target of this project is controlling an RGB Led Matrix remotely using MQTT.
To build this project, we will use:

  • Arduino MKR1000
  • 4×4 RGB Led Matrix

The Arduino MKR1000 will connect to Ubidots cloud (IoT platform) using MQTT. This project is divided into two steps:

  1. Configure the Ubidots to handle the RGB color components using the web interface
  2. Develop an MQTT client to connect to Ubidots and manage the RGB Led Matrix

At the end of this project, we control the RGB Led matrix remotely using Ubidots web interface.


Configure the IoT platform: Ubidots MQTT

In this first step, we will configure the Ubidots interface to handle the three color components. Therefore, we will use sliders to control them. Using these sliders we will control the three color components and the changes will be reflected the RGB LEDs through Arduino MQTT client.

mqtt tutorial config

Before continuing you have to create a free account using this link. Log into the application and click on the Device to create a new device. 

We used RGB Controller as the device name. A device acts as a data container where we can group variables that hold the data exchanged. Once the device is ready, we can start adding a new variable:

mqtt add variable

We create three variables called:

  • redColor
  • greenColor
  • blueColor

Now it is time to create the dashboard that we will use to control these variable values. Go to Dashboard and click on add. Give a name to your dashboard and start adding controls. In this project, we add sliders so click on add new widget and then on slider:

iot platform tutorial

Select the device (aka the datasource) previously configured:

configuring Ubidots to create an Arduino MQTT client

Now, you have to select the RGB Controller and you will see three variables:

Then, pick one of them and repeat the process for all three variables. In the end, you will have a dashboard that looks like:

RGB color components to control an RGB LEDs strip using Arduino MQTT client

How to build an Arduino MQTT client

Once the IoT platform is configured we can focus our attention on the Arduino MQTT client. This client receives the data from the platform and manages the RGB Led matrix. To create the client, we use the following libraries:

Firstly, it is necessary to initialize the Neomatrix providing a set of information like the matrix width and height and the pin number. In our case, the RGB Led matrix is connected using MKR1000 PIN 5.


Notice the first three parameters represent the number of LEDs in a row and in a column. Using matrix instance,  we can color the matrix.

Then, let us implement the Arduino MQTT client. The Arduino MKR1000 uses a WiFi connection. Consequently, it is necessary to configure it:

WiFiClient net; MQTTClient mqttClient; void setup() { Serial.begin(115000); Serial.println("Configuring the net..."); WiFi.begin("your WiFi SID", "your WiFi password"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(1000); } Serial.println("Begin..."); mqttClient.begin("mqtt://", net); matrix.begin(); connect(); }
Code language: JavaScript (javascript)

In the setup() method, we simply initialize the WiFi connection and the mqttClient. To initialize the Arduino MQTT client we have to set the MQTT server (Ubidots server address) and the net.
The connect() method handles the connection and the MQTT topic subscription. According to the Ubidots docs, each variable defined in the datasource (aka device) has it is own topic:


Notice that the lv, at the end of the topic, stands for last value. In other words, we are interested only in the last variable value to control the RGB LED matrix. In this project, the application has to subscribe to the following topics:

/v1.6/devices/RGBController/redColor/lv /v1.6/devices/RGBController/greenColor/lv /v1.6/devices/RGBController/blueColor/lv

So the connect() method is:

void connect() { Serial.println("Connecting to MQTT...."); if (mqttClient.connect("YOUR_UNIQUE_ID", "YOUR_UBIDOTS_TOKEN", "")) { Serial.println("MQTT client connected"); int result = mqttClient. subscribe("/v1.6/devices/RGBController/redColor/lv"); Serial.println(result); Serial.println("Subscribed red topic."); int result1 = mqttClient. subscribe("/v1.6/devices/RGBController/greenColor/lv"); Serial.println(result1); Serial.println("Subscribed green topic."); int result2 = mqttClient. subscribe("/v1.6/devices/RGBController/blueColor/lv"); Serial.println(result1); Serial.println("Subscribed blue topic."); } else { Serial.println("######### Client not connected ########"); } }
Code language: JavaScript (javascript)

Recommended: ESP8266 MQTT Client: Publish and Subscribe with Node-RED Dashboard

Handle MQTT messages using Arduino

After the Arduino MQTT client connects to the MQTT server and it subscribes to the variable topics, we have to implement the method that listens to the values coming from the MQTT server. To this purpose, we have to implement a method named:

messageReceived(String topic, String payload, char * bytes, unsigned int length)

In this method the application has to do the following tasks:

  • identify the topic that holds the message
  • convert the message to the color component value
  • control the RGB LED matrix setting the new color

This is the method implementation:

void messageReceived(String topic, String payload, char * bytes, unsigned int length) { if (topic.indexOf("redColor") >= 0) { red = atoi(bytes); } else if (topic.indexOf("greenColor") >= 0) { green = atoi(bytes); } else if (topic.indexOf("blueColor") >= 0) { blue = atoi(bytes); } matrix.fillScreen(matrix.Color(red,green,blue));; }
Code language: JavaScript (javascript)

That’s all. In the last two lines of the method, we use the instance of matrix to fill the RGB LED matrix with the color identified by red, green, blue component and we show it.

Now you can try to use the three sliders in the Ubidots IoT console to change the RGB LED matrix color.

This project can be further developed. You can use an Android MQTT client app that you can download from google play store and control the RGB LEDs from your smartphone.

If you want to explore how to use MQTT in a different scenario you can read my other tutorial about how to use Cayenne IoT ESP8266 + MQTT.


At the end of this post, hopefully, you have gained the knowledge about IoT MQTT protocol and how to use it to control remote peripheral. Moreover, we have discovered how to build an Arduino MQTT client that connects to Ubidots MQTT broker to exchange data.

Tags: , ,
  • Add Your Comment