0
votes

I've got C++ code using the Paho MQTTPacket Embedded C++ library to connect to an MQTT broker. When that broker is io.adafruit.com, it works perfectly fine. But when it's my own Mosquitto instance running on my Raspberry Pi, the connection fails. I've narrowed it down to this line in MQTTClient.h, in the MQTT::Client::connect method:

// this will be a blocking call, wait for the connack
if (waitfor(CONNACK, connect_timer) == CONNACK)

The app hangs here for about 30 seconds, and then gets a result other than CONNACK (specifically 0 rather than 2).

I have tried both protocol version 3 (i.e. 3.1) and 4 (i.e. 3.1.1); same result.

My Mosquitto instance has no authentication or passwords set up. I've tried turning on debug messages in the Mosquitto log, but they're not showing anything useful. I'm at a loss. Why might I be unable to connect to Mosquitto from my C++ Paho code?

EDIT: Here's the client code... again, this works fine with Adafruit, but when I point it to my Mosquitto at localhost, it hangs as described. (I've elided the username and password -- I am sending them, but I really don't think those are the issue, since with mosquitto_pub or mosquitto_sub on the command line, I can connect regardless of this, since mosquitto is configured to allow anonymous connections.)

const char* host = "127.0.0.1";
int port = 1883;
const char* clientId = "ZoomBridge";
const char* username = "...";
const char* password = "...";
MQTT::QoS subsqos = MQTT::QOS2;

ipstack = new IPStack();
client = new MQTT::Client<IPStack, Countdown, 30000>(*ipstack);

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.willFlag = 1;
data.MQTTVersion = 3;
data.clientID.cstring = (char*)clientId;
data.username.cstring = (char*)username;
data.password.cstring = (char*)password;

data.keepAliveInterval = 20;
data.cleansession = 1;

int rc = ipstack->connect(host, port);
if (rc != MQTT::SUCCESS) {
    cout << "Failed [1] (result " << rc << ")" << endl;
    return rc;
}

rc = client->connect(data);
if (rc != MQTT::SUCCESS) {
    cout << "Failed [2] (result " << rc << ")" << endl;
    ipstack->disconnect();
    return rc;
}
1
What version of mosquitto are you using?hardillb
mosquitto version 1.4.10 (build date Mon, 29 May 2017 13:43:29 +0100), downloaded and installed last week. Everything on this Pi is fresh, including the OS, within the last week.Joe Strout
ok, Just checking as the default raspbian bundled version is very old iirc (latest is 1.4.14, skip 1.4.13 as it has a bug)hardillb
You might want to add your code as well just so we can see if there is anything odd about it.hardillb
Here's a possible clue: when I set data.willFlag = 0 instead of 1 in my options, then it appears to work. All the sample code uses willFlag = 1, and I can't find any documentation about what this means. What am I changing by flipping this flag?Joe Strout

1 Answers

2
votes

As hashed out in the comments.

It looks like you are setting the flag to indicate you want to set a Last Will and Testament for the client (data.willFlag = 1;) but then not passing any topic or payload for the LWT.

If you don't need the LWT then set the flag to 0 (or remove the line settings flag) as it should default to disabled.

Also worth pointing out to clarity, this is all with the Paho Embedded C++ MQTTPacket client not the full blown Paho C++ client.