Reputation: 217
I am working on a small project for demonstration purposes.
I am using an ESP8266
to publish data to the IoT Extension using the MQTT
functionality as described here. I am publishing to the s/us topic and it works fine.
Now I want another ESP8266
to subscribe to the same channel.
Is this possible? If yes, then what is the correct way to do this? I already tried subscribing to the s/us topic (using the arduino pubsubclient lib
) but this is not working and I am not able to find any information on this.
Here is the requested Code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "ssid";
const char* password = "Password";
const char* mqtt_Server = "mciotextension.eu-central.mindsphere.io";
const int mqtt_Port = 1883;
const char* mqtt_user = "User";
const char* mqtt_password = "Password";
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void reconnect() {
// Loop until reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "0da4cfbb-4ea2-488d-8a89-739ec04acf1e";
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
} else {
Serial.print("failed, rc= ");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to WiFi network");
client.setServer(mqtt_Server, mqtt_Port);
reconnect();
client.setCallback(callback);
client.subscribe("s/us"); //I also tryed s/ds
}
void loop() {
client.loop();
delay(1000);
reconnect();
}
Upvotes: 0
Views: 578
Reputation: 4946
Now I want another ESP8266 to subscribe to the same channel.
Is this possible?
Yes- you can confirm that you have configured the publishing client and broker properly by connecting a standalone MQTT client running on your PC. Then have the MQTT client subscribe to the channel.
Your subscribing code has at least one potential issue. It only subscribes to the topic on the very first connection to the broker. The default PubSubClient connect will use a Clean Session - any previous subscriptions are not persisted when the client disconnects and reconnects.
So if the subscriber loses its initial connection to the broker then it will not be subscribed to the topic on subsequent connections.
You can fix this by moving the subscribe code into the reconnect function as shown in the snippet below.
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe("s/us");
} else {
I would also recommend that you invoke setCallback()
prior to the first connect in setup. e.g. in setup()
client.setServer(mqtt_Server, mqtt_Port);
client.setCallback(callback);
reconnect();
Upvotes: 0