9 years ago.

How to resolve connection issues?

I'm trying to add MQTT into a home sensor project. It seems to be stalling on the ipstack.connect call, which is returning a -1.

Is there any way to work out what is causing the issue? From looking at the code it could be a number of different issues.

On the network side the mBed gets an IP over DHCP (172.16.0.186) and I am seeing a TCP three way handshake in a packet capture on the server, but nothing after that.

On the console I see both the "connecting to" and "rc from" messages.

MQTT specific code

#include "mbed.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"

Serial pc(USBTX, USBRX); // tx, rx

int main() {
    //MQTT main init
    MQTTEthernet ipstack = MQTTEthernet();
    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
    char* hostname = "172.16.0.1";
    int port = 1883;
    
    myled4 = 1;
    
    pc.printf("Connecting to %s:%d\n", hostname, port);
    int rc = ipstack.connect(hostname, port);
    if (rc != 0)
        pc.printf("rc from TCP connect is %d\n", rc);

    myled3 = 1;
    
    //End MQTT main init

Question relating to:

An API for using MQTT over multiple transports MQTT, MQTTClient

There is not enough information given for me, but to have as a guess, there is something wrong with hostname and/or port. You need to share more of your code to give it more context.

posted by David Fletcher 06 Apr 2015

Ok, I've updated the code and added some more network detail. The server at 172.16.0.1 is getting a connection on TCP port 1883, but there's just a TCP handshake and nothing else.

posted by Matthew Green 06 Apr 2015

So, you'll see in MQTTSocket.h that the connect method is returning the value given by TCPSocketConnection.connect(). So you should look at the doc for that class to try to work out what is going on.

posted by Ian Craggs 09 Apr 2015

Ok, it seems I had two issues. Firstly my code was failing shortly after this, so it wasn't actually the connect that was always the problem. Secondly there seems to be some kind of locking where if I power cycle my mBed it stops working (and the connection returns a -1), however, if I leave the mBed for a short while (maybe a couple of minutes) then it's fine again. I'm wondering if this has something to do with the mBed not being able to start a new TCP session if the old one is still running (as far as the server is concerned)

I've been digging into this a bit more, and the mBed is always connecting using TCP source port 49153, this is causing problems because the server already has a session open on that socket, so it ignores corresponding TCP SYN packets. This is the first time I've used Ethernet in any of my mBed code, so I'm not sure where the issue lies, but I've not yet been able to find any mention of that port number in the code...

posted by Matthew Green 09 Apr 2015

I'm having the same problem using the HelloMQTT example on the K64F board. I've tried several brokers and the behavior is exactly the same. The code works when it runs the first time, but fails after a reset. If I leave the board powered for a few minutes and reset again, it works again just once. If I flash the board again, it works once. This is a very strange bug. Does anybody have any suggestions ?

posted by Stelian Saracut 06 Jan 2017
Be the first to answer this question.