Mqtt basic example in pubsubclient doesn't print message with callback function

I'm having problem with that pubsubclient example. I have arduino uno and ethernet shield. I have mosquitto running in my backroung with port 1883. So I am having problem with callback function. It doesn't print anything. When I compile that script and open serial monitor, it shows me:
Attempting MQTT connection.....connected...so that works! But then it doesn't show anything more. It wont print Message arrived from callback function. What is wrong? Please help me, I need to understand mqtt for my degree. Thanks in advance!

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress server(192, 168, 1, 71);

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++) {
Serial.print((char)payload*);*

  • }*
  • Serial.println();*
    }
    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("arduinoClient")) {*
  • Serial.println("connected");*
  • // Once connected, publish an announcement...*
  • client.publish("outTopic","hello world");*
  • // ... and resubscribe*
  • client.subscribe("inTopic");*
  • } 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(server, 1883);*
  • client.setCallback(callback);*
  • Ethernet.begin(mac, ip);*
  • // Allow the hardware to sort itself out*
  • delay(1500);*
    }
    void loop()
    {
  • if (!client.connected()) {*
  • reconnect();*
  • }*
  • client.loop();*
    }[/quote]

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

I have exactly the same issue. I posted it on Controlling a relay from mqtt -> no incoming messages - Programming Questions - Arduino Forum

Have you ever found a solution for this?