Pubsubclient on Arduino Mega 2560 - Not receiving any topic

Dear All,

I would like to kindly ask for your help.
I read lot of forum topics and I do not find a proper solution for my problem, however, I saw that several people had this issue as well. Apologies, if this is obvious to solve, I really tried to find the problem myself.

I have an Arduino Mega 2560 module with W5100 ethernet shield, I would like to use the device to send/receive MQTT messages to Mosquito.
Basically, my code is working, I’m able to send the messages on the right topics, I received back “1” on the serial monitor for my subscription of the topic, but it seems the library does not call the “callback” function ever. Nothing happened when I send any info on the related topic from Node-Red via Mosquito.

I can see that the package has been sent due I receive it back from Mosquito on Node-Red monitoring section, I can see that the device sends the temperature/pressure data on the right topic and it also sends the LED status back once I push my button and the LED is turning on/off.

I tried this 100 ms (and 1000ms) delay after the client.loop() command, nothing has happened, I can write anything to “callback” section, it will be not executed, however I can see that the RX led is blinking on the shield once I send anything on the topic where I subscribed, so I think it is sent…

Much appreciated if you have any idea!

My code is:

#include <Adafruit_Sensor.h>
#include <ArduinoJson.h>
#include <Adafruit_BMP085.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <Wire.h>

Adafruit_BMP085 bmp;

#define DATA_TOPIC "testboard/data"
#define LED_TOPIC "testboard/ledstatus"
#define SWITCH_TOPIC "testboard/switchstatus"

// Update these with values suitable for your network.
byte mac[]    = {  0x3B, 0xC8, 0x9A, 0x28, 0xC0, 0x80 };
IPAddress ip(192, 168, 0, 40);
IPAddress server(192, 168, 0, 10);

const int LED1_Pin = 22;
const int Switch1_Pin = 23;

long lastMQTTMsg = 0;

int LED1_State = 0;
int prevLED1_State = 0;
int LED1_Status = LOW;
int Button1_State = 0;
int prevButton1_State = LOW;

long ActualTime = 0;
long ReleaseTime = 200;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  String MQTTmsgTemp;
  
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
    MQTTmsgTemp += (char)payload[i];
  }
  
  Serial.println();

  if (String(topic) == "LED_TOPIC") {
      Serial.print("Changing output to ");
      if(MQTTmsgTemp == "ON" && prevLED1_State != 1){
          Serial.println("ON");
          LED1_Status = HIGH;
          digitalWrite(LED1_Pin, LED1_Status);
          LED1_State = 1;
          prevLED1_State = 1;
      }
      else if(MQTTmsgTemp == "OFF" && prevLED1_State != 0){
          Serial.println("OFF");
          LED1_Status = LOW;
          digitalWrite(LED1_Pin, LED1_Status);
          LED1_State = 0;
          prevLED1_State = 0;
      }
   }
}

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoTestPanel")) {
      Serial.println("connected");
      // Subscribe
      client.subscribe("LED_TOPIC");
      Serial.println(client.subscribe("LED_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(57600);

  client.setServer(server, 1883);
  client.setCallback(callback);

  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out
  delay(1500);
  
  pinMode(LED1_Pin, OUTPUT);
  pinMode(Switch1_Pin, INPUT);

  if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  while (1) {}
  }
  
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  delay(100);
  
    long now = millis();
    
    if (now - lastMQTTMsg > 5000) {
    lastMQTTMsg = now;

    Serial.print("Temperature = ");
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
    
    Serial.print("Pressure = ");
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");   
    
    char data[90];
    char tempbuffer[8];
    String temp = dtostrf(bmp.readTemperature(),  6, 2, tempbuffer);
    String pressure = dtostrf(bmp.readPressure(), 6, 2, tempbuffer);
    String json = "{\"temperature\":" + String(temp) + ",\"pressure\":" + String(pressure) + "}";
    json.toCharArray(data, (json.length() + 1));
    client.publish(DATA_TOPIC, data, false);
    
    Serial.println();
    }
    
    Button1_State = digitalRead(Switch1_Pin);
    if (Button1_State == HIGH && prevButton1_State == LOW && millis() - ActualTime > ReleaseTime) {
      if(LED1_Status == HIGH) {
        LED1_Status = LOW;
        LED1_State = 0;
      } else {
        LED1_Status = HIGH;
        LED1_State = 1;
      }
      ActualTime = millis();
    }
    
    digitalWrite(LED1_Pin, LED1_Status);
    prevButton1_State = Button1_State;

    if (LED1_State == 0 && prevLED1_State != 0) {
       client.publish(SWITCH_TOPIC, "OFF");
       Serial.print("Led State changed to OFF") ;
       Serial.println();
       prevLED1_State = 0;
      } 
      else if (LED1_State == 1 && prevLED1_State !=1 ) {
        client.publish(SWITCH_TOPIC, "ON");
        Serial.print("Led State changed to ON") ;
        Serial.println();
        prevLED1_State = 1;
      } 
}