将两个ESP8266连接到一个MQTT代理导致挂起

时间:2016-11-19 19:29:52

标签: raspberry-pi publish-subscribe mqtt esp8266 arduino-esp8266

我有2个ESP8266 PubSubClients连接到Raspberry PI3上安装的MQTT代理。我能够连接它们并进行ON / OFF操作,没关系!但是当我要使用它们来操作它时,它会挂起并卡在重新连接循环中,当我关闭其中一个时,它的工作正常。

这是我在ESP8266上的代码:

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

// Update these with values suitable for your network.
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "192.168.1.10";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  //pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  //Serial.begin(115200);
  Serial.begin(9600);
  pinMode(0, OUTPUT);
  digitalWrite(0, HIGH);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  //1 ( pin 0 )
  if (strcmp(topic,"/home/1/ard1/p1/com")==0) {
    if (payload[0] == '0'){
      digitalWrite(0, HIGH); 
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("/home/1/ard1/p1/state","0");
    }
    else if (payload[0] == '1')
    {
      digitalWrite(0, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("/home/1/ard1/p1/state","1");
    }
  }


  //2 ( pin 2 )
  if (strcmp(topic,"/home/1/ard1/p2/com")==0) {
    if (payload[0] == '0'){
      digitalWrite(2, HIGH); 
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("/home/1/ard1/p2/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(2, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("/home/1/ard1/p2/state","1");
    }
  }

  //3 ( pin 4 )
  if (strcmp(topic,"/home/1/ard1/p3/com")==0) {
    if (payload[0] == '0'){
      digitalWrite(4, HIGH); 
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("/home/1/ard1/p3/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(4, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("/home/1/ard1/p3/state","1");
    }
  }

  //4 ( pin 5 )
  if (strcmp(topic,"/home/1/ard1/p4/com")==0) {
    if (payload[0] == '0'){
      digitalWrite(5, HIGH); 
      Serial.print("Turning Light ON");
      delay(100);
      client.publish("/home/1/ard1/p4/state","0");
    }
  else if (payload[0] == '1')
    {
      digitalWrite(5, LOW);
      Serial.print("Turning Light OFF");
      delay(100);
      client.publish("/home/1/ard1/p4/state","1");
    }
  }



}
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      client.subscribe("/home/1/ard1/#");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, 75, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("outTopic", msg);
  }
}

我为其中一个更改了以下行,但结果是相同的。

WiFiClient espClient;
PubSubClient client(espClient);

为:

WiFiClient espClient1;
PubSubClient client(espClient1);

非常感谢。

1 个答案:

答案 0 :(得分:4)

你的问题就在这一行:

if (client.connect("ESP8266Client")) {

每个客户端都需要一个唯一的客户端ID,将其硬编码到ESP8266Client意味着当第二个客户端连接代理时,将关闭第一个客户端,然后尝试重新连接,这将使第二个关闭。这最终会陷入困境。