Home > Software engineering >  Problem with MQTT server: Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Problem with MQTT server: Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

Time:12-17

I'am hosting MQTT server on rPi, server works fine and i'am able to connect to it using different machine (PC-ubuntu), but arduino(wemos d1 mini) has a problem with that. I have already set allow_anonymous to true in config file, and run sudo ufw allow 1883. Nothing seems to work for me :c

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
#include "arduino_secrets.h"
// Connect to the WiFi
const char* ssid = SECRET_SSID;                          
const char* password = SECRET_PASS;               
//const char* mqtt_server = "192.168.0.158"; //at first was using this 


IPAddress ip(192, 168, 1, 100); //then switched to this, but with no effect 
IPAddress server(192, 168, 0, 158);
IPAddress mqtt_server = {192, 168, 0, 158};  
 
WiFiClient espClient;
PubSubClient client(espClient);
 
const byte ledPin = D4; // digital pin 4 on a weMos D1 mini is next to ground so easy to stick a LED in.
 
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  ) {
  char receivedChar = (char)payload[i];
  Serial.print(receivedChar);
  if (receivedChar == '1')
  digitalWrite(ledPin, HIGH);
  if (receivedChar == '0')
   digitalWrite(ledPin, LOW);
  }
  Serial.println();
}
 void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect
 if (client.connect("DUPADUPA")) {
  delay(2000);
  Serial.println("connected");
  client.subscribe("real_unique_topic");
 } 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()
{
 Serial.begin(9600);
 
 client.setServer(mqtt_server, 1883);
 client.setCallback(callback);
 
 pinMode(ledPin, OUTPUT);
 digitalWrite(ledPin, HIGH);
 delay(5000);
 digitalWrite(ledPin, LOW);
}
 
void loop()
{
 if (!client.connected()) {
  reconnect();
 }
 client.loop();
}

Also checked another lib: <ArduinoMqttClient.h>, but the same error appears.

PS: Also tried code from here: https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device - works for "test.mosquitto.org", but not with rPi ip

CodePudding user response:

OK ip v6 not working with arduino XD use ip v4 - but only for this code https://docs.arduino.cc/tutorials/uno-wifi-rev2/uno-wifi-r2-mqtt-device-to-device

CodePudding user response:

This code works for me

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>

///////////////////////////////////////////////////////////////////////// init
const char* ssid = "***** ***";
const char* password = "***** ***";
const char* mqtt_server = "XXX.XXX.X.XXX";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
Adafruit_BME280 bme; 
const int ledPin = 4;


///////////////////////////////////////////////////////////////////////// setup
void setup() {
  Serial.begin(250000);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  pinMode(ledPin, OUTPUT);
}


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

  WiFi.begin(ssid, password);

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

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


///////////////////////////////////////////////////////////////////////// callback
void callback(char* topic, byte* message, unsigned int length) {
  String messageTemp;
   for (int i = 0; i < length; i  ) {
    Serial.print((char)message[i]);
    messageTemp  = (int)message[i];
  }
  Serial.println(messageTemp);

}



///////////////////////////////////////////////////////////////////////// reconnect
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    Serial.print(mqtt_server);
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("real_unique_topic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

///////////////////////////////////////////////////////////////////////// loop
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}
  • Related