Unable to run MQTT Basic

Used Nick O' Leary's MQTT library but when running the example through the serial monitor I see nothing appear. How would I troubleshoot? I even launched a MQTT client to test and found no messages generated through MQTT.

/*
 Basic MQTT example 
 
  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/

#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 };
byte server[] = { 198, 41, 30, 241 };
byte ip[]     = { 172, 16, 0, 100 };

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{
  Ethernet.begin(mac, ip);
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  }
}

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

I'm using Arduino Uno and the Ethernet Shield.
Thanks!

Hi sudarnar

You are trying to connect to the network using a static IP address which is the same as in the library example program:

byte ip[]     = { 172, 16, 0, 100 };

I'm guessing that you either need to change this to one that is valid on your network, or use DHCP to receive an allocated address. You would do this by changing the begin statement:

Ethernet.begin(mac);

This way of using begin also returns true on success and false on failure, so you could check that this step in the program is working ok.

Regards

Ray

Also, if your ethernet shield has a sticker on it with a mac address, you need to change the value in the program to match it.

Thanks for the reply. Since there's no indication of the IP Address on the shield, I ran the DhcpAddressPrinter Example to identify and it came back as "My IP Address is 192.168.102.101" which i've included in my code below. I also changed the code to do the check, yet I've still unable to run the mqtt basic example - nothing prints out in the serial monitor once I've uploaded the sketch. Any ideas?

Thanks!

/*
 Basic MQTT example 
 
  - connects to an MQTT server
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic"
*/

#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 };
byte server[] = { 198, 41, 30, 241 };
byte ip[]     = { 192, 168, 102, 101 };

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void setup()
{if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
}
  if (client.connect("arduinoClient")) {
    client.publish("outTopic","hello world");
    client.subscribe("inTopic");
  
  }
}

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

Just spotted that you are missing Serial.begin()

Thanks! Included Serial.begin(9600); and tried to run - same result. Nothing shows up on the serial monitor. Could there a potential compatibility issue with IDE version or something along those lines. Thanks!

Thinking about it, I would not expect anything to show on serial monitor if the program works OK. You publish one message and subscribe to a topic. But the program has nothing in the callback function to process or print any received messages.

With the current program, you could use a separate client program like MQTT.fx running on a PC to check if your published message gets republished by the broker.

To do something with received messages, you will need to add to the current program. There is info on the callback function here: http://knolleary.net/arduino-client-for-mqtt/api/#callback

Found this thread which may be useful: Strings, bytes, char arrays MQTT arghgh - Programming Questions - Arduino Forum