-1

Please help me, i should finish this project as soon I want to make project monitoring using ESP8266, MQTT, and Node-RED dashboard but I have an issue that my serial monitors showing cannot connected to MQTT "Failed to connect to MQTT broker, rc=-2" I try to test my mqtt in CMD using Sub and Pub, i send some text and it works well. but in my project it's failed. here is my code:

#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Replace with your Wi-Fi credentials
const char* ssid = "mywifi";
const char* password = "password";

// Replace with your MQTT broker IP address
const char* mqttServer = "127.0.0.1";
const int mqttPort = 1883;

// Replace with your MQTT topics
const char* mqttTopicTemp = "sensors/temperature";
const char* mqttTopicOxygen = "sensors/oxygen";

WiFiClient espClient;
PubSubClient client(espClient);

Adafruit_MLX90614 mlx = Adafruit_MLX90614();
PulseOximeter pox;

void onHeartRateBeatDetected()
{
  float hr = pox.getHeartRate();
  Serial.print("Heart rate:");
  Serial.println(hr);
}

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

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  
  Serial.println("Connected to WiFi");
  
  // Connect to MQTT broker
  client.setServer(mqttServer, mqttPort);
  
  while (!client.connected()) {
    if (client.connect("ESP8266Client")) {
      Serial.println("Connected to MQTT broker");
    }
    else {
      Serial.print("Failed to connect to MQTT broker, rc=");
      Serial.println(client.state());
      delay(2000);
    }
  }

  // Initialize sensors
  mlx.begin();
  pox.begin();

  // Set up a callback for pulse detection
  pox.setOnBeatDetectedCallback(onHeartRateBeatDetected);
}

void loop() {
  // Read temperature from MLX90614 sensor
  float temperature = mlx.readObjectTempC();
  
  // Convert temperature to string
  String tempPayload = String(temperature);
  
  // Publish temperature to MQTT topic
  client.publish(mqttTopicTemp, tempPayload.c_str());

  // Update pulse oximeter
  pox.update();

  // Read oxygen level from MAX30100 sensor
  if (pox.begin()) {
    float oxygen = pox.getSpO2();
  
    // Convert oxygen level to string
    String oxygenPayload = String(oxygen);
  
    // Publish oxygen level to MQTT topic
    client.publish(mqttTopicOxygen, oxygenPayload.c_str());
  }

  delay(1000);  // Adjust the delay based on your needs
}

I try to test my mqtt in CMD using Sub and Pub, i send some text and it works well. but in my project it's failed.

hardillb
  • 54,545
  • 11
  • 67
  • 105

1 Answers1

1
const char* mqttServer = "127.0.0.1";

127.0.0.1 always points to the device the code is running on, in this case that would mean that the code running on the esp8266 is trying to connect to a boker running on the esp8622.

You need to change this to the IP address of the of the machine the MQTT broker is running on.

(You assuming you are using Mosquitto as the broker, you will also probably need to configure Mosquitto to accept connections from something other than localhost see: Mosquitto: Starting in local only mode)

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Thank you for your response, i did it already but i still get the same problem these are all that i tried to fix this: 1. edit the mosquitto.conf to listener 1883, and allow_anonymous true 2. add firewall settings on port 1883, and file and printing at IPv4 3. try different mqttserver IP in the code using 192.168.18.4 which is my IP (i get my IP on ipconfig command 4. try to make communication in terminal using MQTT pub and sub (it works) but i kinda sus in some configurations when i type mosquitto -v in terminal, it shows error some port is reused. is that signing what the problm is? – AHMAD AUFA A A Jun 11 '23 at 12:44
  • [Edit](https://stackoverflow.com/posts/76438749/edit) the question to include all the relevant details. And no, trying to run 2 copies of mosquitto at the same time should show an error because the ports are already in use. The answer above is the only conclusion that can be drawn from the information provided. Best guess, you didn't restart the service after editing the config file. – hardillb Jun 11 '23 at 13:41