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

*Edited for code and clarity