简体   繁体   中英

Issues connecting to HiveMQ MQTT broker via ESP8266

I am trying to connect to a HiveMQ MQTT broker with an ESP8266 but it keeps failing to connect. I am not super well versed in MQTT protocol so I'm not really sure if I've configured everything right but here's how I have it setup:

HiveMQ broker is setup to run at 127.0.0.1 and listen at 1883 (seems fairly standard from what I've read). I can connect to the broker with a desktop client (MQTT Explorer) and verify that the client is connected via the broker's control center.

How the desktop client is configured to connect.

When using the desktop client, I can connect by using 127.0.0.1 as the server and 1883 as the port no problem. However, when I put the same information in the code for the ESP it fails to connect. Here is the code showing how the variables are setup, the ESP PubSubClient setup, and connecting to the broker.

#ifdef USE_MQTT
const char* mqttClientId = "Lovebox-01";
const char* mqttServer = "127.0.0.1";
const int mqttPort = 1883;
const char* mqttUser = "";
const char* mqttPassword = "";
const char* mqttPublishTopic = "lovebox/result";
const char* mqttSubscribeTopic = "lovebox/messages";
#ifdef USE_MQTT
#include <PubSubClient.h>
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
char* mqttMessage;
bool newMqttMessageAvailable;
#else
const int fetchIntervalMillis = fetchIntervalSeconds * 1000;
#endif
#ifdef USE_MQTT
void mqttConnect() {
  mqttClient.setServer(mqttServer, mqttPort);
  mqttClient.setCallback(mqttCallback);

  while (!mqttClient.connected()) {
    Serial.println("Connecting to MQTT...");
    if (mqttClient.connect(mqttClientId, mqttUser, mqttPassword)) {
      Serial.println("connected");
    } else {
      Serial.print("failed with state ");
      Serial.println(mqttClient.state());
      delay(2000);
    }
  }
  mqttClient.publish(mqttPublishTopic, "Hello from Lovebox");
  mqttClient.subscribe(mqttSubscribeTopic);
}

Again like I said, I'm not very familiar with MQTT but based off the troubleshooting I've done, I think I've got it setup correctly. I'm just not sure what to put as my mqttServer because clearly the localhost address is not working.

My best guess is that there is either an issue with the way the broker is setup and its not compatible with the ESP or that I'm missing something fundamental with the way it needs to be setup.

The only thing I've seen people mention online about this is how they've been able to get it to work by using their actual IP address but I'm not sure which IP address to use and if that would be a long term fix for the issue.

The only thing I've seen people mention online about this is how they've been able to get it to work by using their actual IP address but I'm not sure which IP address to use and if that would be a long term fix for the issue.

Using the correct IP address or name is the only fix for this issue.

127.0.0.1 means localhost - the same host that the request is coming from. It and the name localhost are never used to refer to a different computer or device than the one your code is running on. So in this case, 127.0.0.1 would indicate that you're trying to connect to an MQTT broker on the ESP8266 itself, which is obviously not the case.

You need to figure out the actual IP address or name of the MQTT broker and use that instead of 127.0.0.1 . How you find the IP address depends on the operating system the broker is running on. Check its documentation for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM