electroSome

ESP8266 as MQTT Client – Arduino IoT

ESP8266 as MQTT Client - Arduino IoT Tutorial

ESP8266 as MQTT Client - Arduino IoT Tutorial

IoT or Internet of Things is an emerging technology which enables us to create a network of devices, sensors, actuators to connect and exchange data. MQTT (Message Queuing Telemetry Transport) is one of the commonly used protocol in this field. It consists of MQTT Broker and MQTT Clients. Where MQTT Broker is the server through which all MQTT Clients are communicating each other. CloudMQTT provides MQTT Broker as a service, which we are using in this tutorial.

In this project we are controlling an LED connected to ESP8266 from a mobile app using MQTT protocol. Here we are using free plan of CloudMQTT  for this or you can use any other MQTT broker of your choice.

Components Required

Hardware

Circuit Diagram

ESP8266 as MQTT Client – Circuit Diagram

Software

Creating Account in CloudMQTT – MQTT Broker

www.cloudmqtt.com – credentials

Programming ESP8266 – MQTT Client

Arduino Code

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>

const char *ssid = "SERVER NAME"; 
const char *password = "SERVER PASSWORD"; 
const char *mqtt_server = "m11.cloudmqtt.com"; 
const char *device_id = "esp8266";

WiFiClient espClient;
PubSubClient client(espClient);

const byte ledPin5 = 16;
char message_buff[100];

void callback(char *led_control, byte *payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(led_control);
  Serial.println("] ");
  int i;
  for (i = 0; i & length; i++)
  {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';

  String msgString = String(message_buff);
  Serial.println(msgString);
  if (strcmp(led_control, "esp8266/led_control") == 0)
  { 
    if (msgString == "1")
    {
      digitalWrite(ledPin5, LOW); // PIN HIGH will switch OFF the relay
    }
    if (msgString == "0")
    {
      digitalWrite(ledPin5, HIGH); // PIN LOW will switch ON the relay
    }
  }
}

void reconnect()
{
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    if (client.connect(device_id, "cloud_username", "cloud_password"))
    { 
    Serial.println("connected");
    client.subscribe("esp8266/led_control"); // write your unique ID here
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void setup()
{
  Serial.begin(115200);

  client.setServer(mqtt_server, 1883); // change port number as mentioned in your cloudmqtt console
  client.setCallback(callback);

  pinMode(ledPin5, OUTPUT);
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
}

Code Explanation

We are including ESP8266 WiFi library which provides ESP8266 specific WiFi routines and we are calling it to connect to the network. PubSubClient library allows a client to publish/subscribe messaging with a MQTT support server.

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>

Get and enter the “ssid” and “password” i.e.,  your WiFi name and password.

const char *ssid = "SERVER NAME";
const char *password = "SERVER PASSWORD";

Get and enter the “MQTT Server” name and uniqueID. Assign the MQTT as a client to ESP8266.

const char *mqtt_server = "m11.cloudmqtt.com";
const char *device_id = "esp8266";

WiFiClient espClient;
PubSubClient client(espClient);

Select a digital pin D5 of NodeMCU to perform LED control operation and initialize an array message_buff with the type char.

const byte ledPin5 = 14;
char message_buff[100];

We are writing a function to read the characters from serial monitor and controlling the LED ON and OFF based on the instructions which is passing from MQTT Dashboard Play Store application.

void callback(char *led_control, byte *payload, unsigned int length)
{
  Serial.print("Message arrived [");
  Serial.print(led_control);
  Serial.println("] ");
  int i;
  for (i = 0; i & length; i++)
  {
    message_buff[i] = payload[i];
  }
  message_buff[i] = '\0';

  String msgString = String(message_buff);
  Serial.println(msgString);
  if (strcmp(led_control, "esp8266/led_control") == 0)
  { 
    if (msgString == "1")
    {
      digitalWrite(ledPin5, LOW); 
    }
    if (msgString == "0")
    {
      digitalWrite(ledPin5, HIGH); 
    }
  }
}

Actual connection to MQTT server is initialized by calling the below instructions. We have to enter our cloudmqtt user name, password and uniqueID in respective place. If client connected, then we can control operation of the NodeMCU connected LED via MQTT Dash app.

void reconnect()
{ 
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection..."); 
    if (client.connect(device_id, "CLOUD USER NAME", "CLOUD PASSWORD"))
    { 
      Serial.println("connected"); 
      client.subscribe("esp8266/led_control"); 
    }

If client not connected then it will try to connect again and again for every 5 seconds.

else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds"); 
      delay(5000);
    }
  }
}

Put your setup or configuration code in the setup function, it will only run once during the startup.
Here in the setup function, it will connect to WiFi Network and also initialize serial communication for debugging and logging with a baud rate of 115200. Please change port number as mentioned in your cloudmqtt console. Setting mode of operation as output for D5 pin of NodeMCU.

void setup()
{
  Serial.begin(115200);
  client.setServer(mqtt_server, 1883);  
  client.setCallback(callback);

  pinMode(ledPin5, OUTPUT);
}

Put your main code in void loop() function to run repeatedly. This continuously waits and checks for a client to connect.

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }
  client.loop();
}

Setting up Mobile App – MQTT Client

MQTT Dash App Configuration Steps

Working

In this project we have 3 parts, MQTT Dash (mobile app – MQTT Client), CloudMQTT (MQTT Broker as a service) and ESP8266 Circuit (led control circuit – MQTT Client). Our mobile app is sending commands to ESP8266 circuit via MQTT broker. MQTT is a publish and subscribe protocol.

 

Practical Implementation

ESP8266 as MQTT Client – Practical Implementation

Video