[SOLVED] Mqtt connection lost with mosquitto

my mosquitto is running fine.my handler code is–
public class FirstHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(FirstHandler.class);
private ScheduledFuture<?> executionJob;
protected FirstConfig configuration;
private static final String MQTT_URI = "tcp://127.0.0.1:1883";
private MqttClient client;
public String geet;
private String clientId = "openhab2";
MemoryPersistence persistence = new MemoryPersistence();

public FirstHandler(final Thing thing) {
    super(thing);
}


public void updateSynopChannels() {

    getThing().getChannels().forEach(channel -> {
        String channelId = channel.getUID().getId();
        updateState(channelId, getChannelState(channelId));
    });
}

public StringType getChannelState(String channelId) {

    if (channelId.equals(TEMPERATURE)) {

        try {
            client = new MqttClient(MQTT_URI, clientId, persistence);
            client.connect();

            client.subscribe("good");
        } catch (MqttException e) {
            e.printStackTrace();
        }

        client.setCallback(new MqttCallback() {

            @Override
            public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
                logger.info("FirstHandler::messageArrived\n");
                // TODO Auto-generated method stub

                geet = arg1.toString();
            }

            @Override
            public void connectionLost(Throwable throwable) {
                logger.info("FirstHandler::connectionLost\n");
                logger.info("FirstHandler::connectionLost\n");

            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
                logger.info("FirstHandler::deliveryComplete\n");
            }
        });
    }

    return new StringType(geet);

}

@Override
public void initialize() {

    configuration = getConfigAs(FirstConfig.class);
    logger.info("Scheduling Synop update thread to run every {} minute for Station '{}'",
            configuration.refreshInterval);

    executionJob = scheduler.scheduleWithFixedDelay(() -> {
        updateSynopChannels();
    }, 1, configuration.refreshInterval, TimeUnit.SECONDS);
    updateStatus(ThingStatus.ONLINE);

  
}

}

when i run this binding then in log file i see this–

[ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to broker was lost
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:146)[253:org.eclipse.paho.client.mqttv3:1.0.2]
at java.lang.Thread.run(Thread.java:748)[:1.8.0_131]
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:267)[:1.8.0_131]
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:65)[253:org.eclipse.paho.client.mqttv3:1.0.2]
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:107)[253:org.eclipse.paho.client.mqttv3:1.0.2]
… 1 more
16:52:34.629 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to ‘mosquitto’ was lost: Connection lost : ReasonCode 32109 : Cause : null

kindly help me…where i am wrong…why it is not connecting …m stuck and m trying since last 5 days…:frowning:

Did you ever find a solution to this?

I have the same problem - MQTT is working but I get a lot of these messages in my openhab.log file.
I think it is causing OpenHAB to miss some MQTT messages, (probably while the connection was lost and before re-connection), so I really need to figure out how to fix it.

post the contents of the following 2 files:

/var/lib/openhab2/config/org/openhab/mqtt.config
/etc/openhab2/services/mqtt.cfg

Hi @Dim,

I have read a lot of your posts in other threads while trying to troubleshoot this and I assume you are going to be looking for discrepancies between the two files since changes to the .cfg do not necessarily update the .config.

mqtt.cfg:

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=OpenHAB2MQTT
mosquitto.retain=true
mosquitto.async=false

find.url=tcp://ml.internalpositioning.com:1883
find.clientId=OpenHAB
find.user=xxxx
find.pwd=xxxx

mqtt.config

find.clientId="OpenHAB"
find.pwd="xxxx"
find.url="tcp://ml.internalpositioning.com:1883"
find.user="xxxx"
mosquitto.async="false"
mosquitto.clientId="OpenHAB2MQTT"
mosquitto.retain="true"
mosquitto.url="tcp://localhost:1883"
service.pid="org.openhab.mqtt"

As far as I could see there are no spurious entries in the .config file that would be causing a problem.

I am only having trouble with the connection to the remote server ml.internalpositioning.com the connection to my local MQTT broker never reports an issue in the log.

I have been running with a different MQTT viewer client on a different machine, (using a different ClientID), and that client never has any connection issues so I don’t think it is a network problem or an issue with the remote server.

i can only think of a network timeout to the remote broker…
the viewer client that you are using maybe also gets disconnected but has an auto reconnect feature?
btw, I haven’t found a timeout or a reconnect config option for mqtt.cfg so far :slight_smile: (there is only the keepAlive)

you could try to use find.keepAlive=60 to check if this helps

OH actually reconeccts itself after the connection was lost:

2018-01-06 14:09:24.564 [ERROR] [t.mqtt.internal.MqttBrokerConnection] - MQTT connection to 'find' was lost: Connection lost : ReasonCode 32109 : Cause : null
2018-01-06 14:09:24.564 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting connection helper to periodically try restore connection to broker 'find'

So subsequent messages are captured but the disconnection happens so frequently, about every 20s, that OH misses messages that are published while the connection is down.

I have tried monitoring with MQTT.fx and also with the command line mosquitto_sub both of which appear to be rock solid and never miss a message. This is why I was blaming OH rather than the network or the remote server :slight_smile:
I agree that it is possible that these other clients are getting disconnected and auto-reconnect but if so they are doing so silently and reconnecting much faster than OH so as to not miss any messages.

I have also tried running these other clients on the OH server just to eliminate that variable and they do not miss any messages their either.

Tried find.keepAlive=10 since i am losing the connection more frequently than every minute so went with (I assume) 10s.
This doesn’t seem to have made a difference unfortunately :frowning:

Do I need to restart OH for changes to the .cfg file to take effect?

1 Like

from what I remember: no

I tried a restart anyway and… it’s working!
No more disconnects using find.keepAlive=10 worked.

Thanks @Dim :smiley:

1 Like