简体   繁体   English

Mqtt_server 未连接到端口 8884

[英]Mqtt_server not connecting on port 8884

there is a problem I am facing that when I tried to connect server on port 8884 it gives me error Attempting MQTT connection…failed, rc = -4 try again in 5 seconds but the connection is successfully built on port 8883 I don't understand the reason behind this kindly help me to to over come this problem.我遇到一个问题,当我尝试在端口 8884 上连接服务器时,出现错误 Attempting MQTT connection…failed, rc = -4 try again in 5 seconds 但连接已成功建立在端口 8883 上 我不明白这背后的原因请帮助我解决这个问题。 I am totally new with mqtt protocol and try to figure out how to make web based system to automate farm using esp32 or esp8266,HiveMQ server and Django for backend development also give me some suggestions how to do complete this task我完全不熟悉 mqtt 协议,并尝试弄清楚如何使用 esp32 或 esp8266、HiveMQ 服务器和 Django 使基于 web 的系统自动化农场用于后端开发,也给我一些如何完成此任务的建议

The code I am using provided by the HiveMQ community我使用的代码由 HiveMQ 社区提供

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <time.h>
#include <TZ.h>
#include <FS.h>
#include <LittleFS.h>
#include <CertStoreBearSSL.h>

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

// A single, global CertStore which can be used by all connections.
// Needs to stay live the entire time any of the WiFiClientBearSSLs
// are present.
BearSSL::CertStore certStore;

WiFiClientSecure espClient;
PubSubClient * client;
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (500)
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 setDateTime() {
  // You can use your own timezone, but the exact time is not used at all.
  // Only the date is needed for validating the certificates.
  configTime(TZ_Europe_Berlin, "pool.ntp.org", "time.nist.gov");

  Serial.print("Waiting for NTP time sync: ");
  time_t now = time(nullptr);
  while (now < 8 * 3600 * 2) {
    delay(100);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println();

  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.printf("%s %s", tzname[0], asctime(&timeinfo));
}


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 the first character is present
  if ((char)payload[0] != NULL) {
    digitalWrite(LED_BUILTIN, 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)
    delay(500);
    digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
  } else {
    digitalWrite(LED_BUILTIN, 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…");
    String clientId = "ESP8266Client - MyClient";
    // Attempt to connect
    // Insert your password
    if (client->connect(clientId.c_str(), "Hassan", "******")) {
      Serial.println("connected");
      // Once connected, publish an announcement…
      client->publish("testTopic", "hello world");
      // … and resubscribe
      client->subscribe("testTopic");
    } 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() {
  delay(500);
  // When opening the Serial Monitor, select 9600 Baud
  Serial.begin(9600);
  delay(500);

  LittleFS.begin();
  setup_wifi();
  setDateTime();

  pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output

  // you can use the insecure mode, when you want to avoid the certificates
  //espclient->setInsecure();

  int numCerts = certStore.initCertStore(LittleFS, PSTR("/certs.idx"), PSTR("/certs.ar"));
  Serial.printf("Number of CA certs read: %d\n", numCerts);
  if (numCerts == 0) {
    Serial.printf("No certs found. Did you run certs-from-mozilla.py and upload the LittleFS directory before running?\n");
    return; // Can't connect to anything w/o certs!
  }

  BearSSL::WiFiClientSecure *bear = new BearSSL::WiFiClientSecure();
  // Integrate the cert store with this connection
  bear->setCertStore(&certStore);

  client = new PubSubClient(*bear);

  client->setServer(mqtt_server, 8884);
  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("testTopic", msg);
  }
}

在此处输入图像描述

As mentioned in the comments.如评论中所述。

The PubSubClient does NOT support MQTT over Websockets. PubSubClient 不支持基于 Websockets 的 MQTT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接到MQTT服务器时的多个节点 - Multiple nodes when connecting to the MQTT Server MQTT无法连接? - MQTT not connecting? 在隔离网络中将MQTT客户端连接到Android中的MQTT服务器时出现问题 - Trouble connecting MQTT client to MQTT server in Android in isolated network 为 MQTT 连接 AWS 服务器时出现异常 - Exception while connecting AWS server for MQTT 更改PHP服务器上的默认Mosquitto MQTT端口 - Changing default Mosquitto MQTT Port on PHP server php,mqtt:stream_socket_client():无法连接到<server><port> - php,mqtt: stream_socket_client(): unable to connect to <server><port> 在端口 443 上使用 ALPN 将 M2MQTT 客户端库连接到 AWS IoT 时出现问题 - Having problems connecting with M2MQTT client library to AWS IoT using ALPN on port 443 使用 ActionListener(回调)连接到 MQTT 服务器时出现错误异常 - Wrong Exception while connecting to MQTT server with an ActionListener (callback) C# Cumulocity SDK 抛出“与 MQTT 服务器连接失败 (ConnectionRefusedNotAuthorized)” - C# Cumulocity SDK throws "Connecting with MQTT server failed (ConnectionRefusedNotAuthorized)" 使用端口443上的erlang mqtt websocket客户端连接ibm bluemix watson IoT - Connecting ibm bluemix watson IoT using erlang mqtt websocket client on port 443
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM