-->
Page 1 of 1

Two ESP8266 communicate with each other via MQTT protocol

PostPosted: Thu Mar 16, 2017 2:07 am
by Priya Garg
Hello Everyone,
I am new to working with MQTT protocol. I am reading some stuff to get familiar with MQTT and doing some practical implementations. I have made a basic application (PC communication with esp8266 to control the LED on ESp8266 with HiveMQ broker).
Now I'm trying to communicate two esp8266 together with HiveMQ broker. In that ESP(01) will publish the message (on) with Topic (ESP8266/LED_status) to broker and ESP(02) will subscribe to the topic and when it finds the message(on), it lits up the LED on GPIO2 of ESP(02).

Publisher side- ESP(01)
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "qwertyuyt";
const char* password = "asdfnls";
const char* mqtt_server = "broker.mqttdashboard.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server,1883);
  reconnect();
}

 void setup_wifi()
 {
  delay(10);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      }
 }

 void reconnect()
 {
  while (!client.connected())
  {
    if (client.connect("ESP8266Client"))
    {
      client.publish("ESP8266/connection_status", "on");
      }
     else
     {
      delay(5000);
      }
    }
  }

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


subscriber side (ESP02)
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "qwertyuyt";
const char* password = "asdfnls";
const char* mqtt_server = "broker.mqttdashboard.com";

WiFiClient espClient1;
PubSubClient client(espClient1);

void setup()
{
  pinMode(2, OUTPUT);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  reconnect();
  }

void callback(char* topic, byte* payload, unsigned int length)
{
  if((char)payload[0] == 'o' && (char)payload[1] == 'n') //on
  digitalWrite(2, HIGH);
 }

void setup_wifi()
{
  delay(10);
  WiFi.begin(ssid, password);
   while (WiFi.status() != WL_CONNECTED)
   {
    delay(500);
    }
  }

 void reconnect()
 {
  while(!client.connected())
  {
    if (client.connect("ESP8266Client1"))
    {
      client.subscribe("ESP8266/connection_status");
      }
      else
      {
        delay(5000);
        }
    }
  }

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


When I am uploading the code on both ESPs, LED is not glowing. I am doing something wrong on subscriber side. Please guide me. Any help will be appreciated for me.

Re: Two ESP8266 communicate with each other via MQTT protoco

PostPosted: Thu Mar 16, 2017 12:31 pm
by martinayotte
How did you connect your LED on GPIO2 ?

(Funnily, I've posted about 4 times the following answer in past 2 days, and maybe 20 times over years)

There are 2 ways to connect an LED on a GPIO, in sink mode or in source mode.
For normal GPIO, both modes can be used, but you need to be aware that for sink mode, the GPIO need to be bring to LOW or zero to make the LED turned ON.
But since GPIO0 and GPIO2 needs to be HIGH at boot time to enter in execute mode, the source mode can not be used since it is acting as a pull-down, preventing ESP to go in execute mode.
So, for those 2 GPIOs, you don't have choice to connect the LED in sink mode and reverse all digitalWrite() states.
Image