IoT Protocol : Sample MQTT Client for Arduino


Quick Reference for connecting Arduino ESP8266 as MQTT Client for an internet of Things Solution:


In first place is needed to understand the basic architecture of MQ Telemetry Transport (MQTT) . As shown in the figure, the MQTT protocol has three main nodes: Broker, Publisher and Subscriber. The communication is based on a source (publisher) that publish  some “data” enclosed in a specific “topic” to the server (Broker); hence, the broker can allocate multiple topics, and simultaneously can send the same “data” to the Subscriber(s) of such topic.   

MQTT Broker :Due the MQTT protocol is designed on top of the TCP/IP protocol, it is vital to have network connectivity;therefore, the broker could be public (list of public brokers), or could be configured in a LAN ( Install MQTT broker in Raspberry Pi). The main idea is to have a IP address or Domain name for point the clients.

Arduino Brokerclient.begin("", net);

MQTT Client :Now it is possible to understand that the clients of the Broker will be the subscriber and the publisher, that will use the broker URL as common point for exchange data.

Arduino Subscriberclient.subscribe("/temperatureSensor");

Arduino Publisher:  client.publish("/temperatureSensor", "-5 Centigrade");

MQTT Topic/Data: The topic basically is an identifier; thus, some characters (String),ca represent some variables in your design, for instance the topic “/temperatureSensor”. The System could be scalable, for instance it seems is possible to have between 100k to 500k subscribers of a single topic (haven’t try), as well it is possible to publish to the same topic from a different sources, at this point id good idea to revise proper documentation for  conventions and quality of service. As well, the protocol could be run more securely using this guidelines.

Note: This document present a brief information of MQTT protocol, for deep understanding of MQTT please refer to:

Regarding Arduino connection, the IDE has some templates built-in based on this libraries.   As follow there is a sample of MQTT using ESP8266.

Node MCU ESP8266 – MQTT Code Sample:

#include MQTTClient.h
#include "SoftwareSerial.h"
#include ESP8266WiFi.h
const char* ssid = "yourwifi id";
const char* password = "wifi password ";
WiFiClient net;
MQTTClient client;
SoftwareSerial serial_connection(12, 13);

void setup()
client.begin("", net);
connect() ;

void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {

while (!client.connect("arduino", "try", "try")) {
void loop()
//Serial.print("\nTesting Publisher every 5 sec");
//client.publish("/temperatureSensor", ("-5"));
void ledAction(int x) {
switch (x) {
case 0:
Serial.print("\nSerial control OFF");
digitalWrite(LED_BUILTIN, LOW);
case 1:
Serial.print("\nSerial control ON");
digitalWrite(LED_BUILTIN, HIGH);

void messageReceived(String topic, String payload,
char * bytes, unsigned int length) {
Serial.print("incoming: ");
Serial.print(" - ");
int x= payload.toInt();

Mauricio Silva

Leave a Reply