Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino ESP8266无法连接到mosquitto代理&引用;尝试MQTT连接…失败,rc=-2请在5秒内重试”;_Arduino_Mqtt_Esp8266_Mosquitto_Arduino Esp8266 - Fatal编程技术网

Arduino ESP8266无法连接到mosquitto代理&引用;尝试MQTT连接…失败,rc=-2请在5秒内重试”;

Arduino ESP8266无法连接到mosquitto代理&引用;尝试MQTT连接…失败,rc=-2请在5秒内重试”;,arduino,mqtt,esp8266,mosquitto,arduino-esp8266,Arduino,Mqtt,Esp8266,Mosquitto,Arduino Esp8266,我正在运行一个mosquitto代理和一个node red服务器,node red能够连接到mosquitto。ESP8266能够连接到WiFi,但不能连接到代理。串行监视器中显示“尝试MQTT连接…失败,rc=-2 5秒后重试”消息。我也在使用MQTT资源管理器,我不知道它是否与此有关 #include <ESP8266WiFi.h> #include <PubSubClient.h> // Update these with values suitable for

我正在运行一个mosquitto代理和一个node red服务器,node red能够连接到mosquitto。ESP8266能够连接到WiFi,但不能连接到代理。串行监视器中显示“尝试MQTT连接…失败,rc=-2 5秒后重试”消息。我也在使用MQTT资源管理器,我不知道它是否与此有关

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

// Update these with values suitable for your network.

const char* ssid = "Milagros";
const char* password = "18095381";
const char* mqtt_server = "192.168.1.133";

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

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  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();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is active low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("data", "hello world");
      // ... and resubscribe
   
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

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

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("data", msg);
  }
}```
#包括
#包括
//使用适合您的网络的值更新这些值。
const char*ssid=“Milagros”;
const char*password=“18095381”;
const char*mqtt_server=“192.168.1.133”;
无线客户端;
PubSubClient客户(espClient);
无符号长lastMsg=0;
#定义消息缓冲区大小(50)
char msg[msg_BUFFER_SIZE];
int值=0;
无效设置\u wifi(){
延迟(10);
//我们从连接WiFi网络开始
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.模式(WiFi_STA);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
随机种子(micros());
Serial.println(“”);
Serial.println(“WiFi连接”);
Serial.println(“IP地址:”);
Serial.println(WiFi.localIP());
}
无效回调(字符*主题,字节*有效负载,无符号整数长度){
串行打印(“消息到达[”);
连续打印(主题);
序列号。打印(“]”);
for(int i=0;i2000){
lastMsg=现在;
++价值观;
snprintf(msg,msg缓冲区大小,“hello world#%ld”,值);
Serial.print(“发布消息:”);
Serial.println(msg);
发布(“数据”,msg);
}
}```

您运行的是哪个版本的mosquitto,您是否修改了
mosquitto.conf
以允许从远程机器进行连接?您的代码看起来不错。rc=2表示无法与MQTT代理建立连接。MOSQUITO服务器运行的MQTT协议版本是什么?您的服务器需要用户名和密码吗?您的mosquitto代理是否与您的ESP8266在同一网络上运行(在同一192.168.1.xxx子网上)?这是我没有创建的mosquitto conf文件。