Mqtt“软件导致连接中止”错误

时间:2019-06-19 09:49:41

标签: c++ raspberry-pi mqtt nodemcu esp32

我正在尝试使用mqtt将数据从nodemcu(esp32)发送到树莓派。连接Wifi网络后,会发生此错误。错误:正在尝试进行MQTT连接... [E] [WiFiClient.cpp:365] write():在fd 55上失败,错误号:113,“软件导致连接中止” 失败,rc = -4请在5秒钟后重试

Nodemcu无法连接到在树莓派上运行的mosquitto mqtt代理。我尝试检查1883端口是否已打开,或者rpi mqtt broker上是否存在任何问题。没有发现任何问题。尝试了多个库。但是结果仍然相同。 这是Iam使用的代码:

#include "EspMQTTClient.h"

EspMQTTClient client(
  "XXXXX",
  "XXXXX",
  "XXXXXX",  // MQTT Broker server ip
     // Can be omitted if not needed
     // Can be omitted if not needed
  "TestClient",     // Client name that uniquely identify your device
  1883              // The MQTT port, default to 1883. this line can be omitted
);

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

  // Optionnal functionnalities of EspMQTTClient : 
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overrited with enableHTTPWebUpdater("user", "password").
  client.enableLastWillMessage("TestClient/lastwill", "I am going offline");  // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  // Subscribe to "mytopic/test" and display received message to Serial
  client.subscribe("refrigerator/temp", [](const String & payload) {
    Serial.println(payload);
  });

  // Publish a message to "mytopic/test"
  client.publish("refrigerator/temp", "Temp=34"); // You can activate the retain flag by setting the third parameter to true

  // Execute delayed instructions
  client.executeDelayed(5 * 1000, []() {
    client.publish("refrigerator/temp", "Temp=23");
  });
}

void loop()
{
  client.loop();
}

0 个答案:

没有答案