Problem with MQTT connectivity

Hello Everyone, I'd like to start by saying I'm still a novice when it comes to programming and MQTT, so i may be overlooking something totally obvious. With that out of the way, here's my problem:
I'm trying to get my Arduino MKR 1010 WiFi to receive messages as a subscriber from an MQTT broker (mosquitto) i have installed on a mini pc. Now, the code for connecting to internet works just fine, and the MQTT broker works as well, since i was able to test it out. I have a config file that's correctly set up and there is no communication problem between the mini pc the broker is on and the pc the arduino software is ran on (tested this out too). What has me banging my head against the wall is that i was able to create a connection to a public mqtt server (test.mosquitto.org), but when i tried to connect to the mqtt broker on my pc the code did not work.
I'll post the code inthe hope that more expert eyes can discern what's wrong with it. Thanks in advance!

#include <WiFiNINA.h>
#include "Clelia_secrets.h"
#include <PubSubClient.h>
#include <ArduinoMqttClient.h>

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

WiFiClient wificlient;
PubSubClient client(wificlient);
int status = WL_IDLE_STATUS;
IPAddress server (127, 0, 0, 1); 
MqttClient mqttC(wificlient);
int port = 1883;
const char broker[]= "192.168.20.39";


void setup() {
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  while (!Serial){
      ;
    }
  Serial.println ("OK");
  delay (2500);
  Serial.println ("Attempting to connect ");
  
  while (WiFi.begin(ssid,pass) != WL_CONNECTED) {
    Serial.println (".");
    delay (2500);
  }
  Serial.println("you're connected to the network");
  delay (2500);
  client.setCallback(callback);
  Serial.println("connecting to MQTT broker");

  client.setServer(server, 1883);
  client.setCallback(callback);
  if (client.connect("LeaSub")){ 
    Serial.println("mqtt connected");
    client.subscribe("lea");
  }else {
    Serial.println("mqtt failed");
    Serial.print("failed, rc = ");
    Serial.println(client.state()); 
  }
}

void callback(char*topic, byte* payload, unsigned int length){
  String msg;
  for (int i=0;i< length; i++){
    msg += (char)payload [i];
  }

  if (strcmp(topic, "/lea")==0) {
    if(msg=="on"){
  digitalWrite(13,HIGH);
  Serial.println("on");
} else if (msg== "off"){
    digitalWrite(13,LOW);
    Serial.println("off");
  }
}
}

void loop() {
  client.loop();
}

Following is the output from the serial monitor

Screenshot 2024-05-09 142527

*Edited for code and clarity

Do the logs in the broker show any connection attempts?

Never occured to me i could check, i'll do right away and let you know. Thanks for the tip!

Firstly, I noticed that you have declared two MQTT client objects: PubSubClient client(wificlient); and MqttClient mqttC(wificlient);. However, you seem to be using only the PubSubClient object in your code. The MqttClient object is not used and can be removed to avoid confusion.

The main issue seems to be with the IPAddress server (127, 0, 0, 1); line. The IP address 127.0.0.1 refers to localhost, which is the address of the local machine. When running on the Arduino, localhost will refer to the Arduino itself, not your mini PC where the broker is running. You should replace this with the IP address of the MQTT broker running on your mini PC, which seems to be 192.168.20.39 as per your broker variable.

Here's the corrected part of your code:

// ... other code ...

IPAddress server(192, 168, 20, 39); // IP of the MQTT broker

// ... setup() ...

void setup() {
  // ... other setup code ...
  
  Serial.println("connecting to MQTT broker");
  client.setServer(server, port); // Use the correct IP and port for the MQTT broker
  client.setCallback(callback);
  
  if (client.connect("LeaSub")) {
    Serial.println("mqtt connected");
    client.subscribe("lea");
  } else {
    Serial.println("mqtt failed");
    Serial.print("failed, rc = ");
    Serial.println(client.state());
  }
}

// ... rest of your code ...

Make sure to replace the server IP address with the actual IP address of your MQTT broker. Also, ensure that the port variable is set to the correct port that your MQTT broker is listening on.

Additionally, you have a callback function that compares the topic with "/lea". If your topic name does not start with a forward slash, you should remove it from the comparison in the callback function.

Thanks a lot for the info, i'll try all the different solutions proposed by you and others and hopefully solve the issue!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.