Two ESP8266 communicate with each other via MQTT protocol
Posted: Thu Mar 16, 2017 2:07 am
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)
subscriber side (ESP02)
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.
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.