Error starting broker connection

Hello everyone,

I’m having some troubles, trying to use the MQTT binding.
I’m using an raspberry pi3 where I have installed openhab2 and mosquitto. I also installed the MQTT binding in the paper UI.

I’m trying to configure the mqtt.cfg file via /etc/openhab2/services/mqtt.cfg and only changed the line .url=tcp://localhost:1883 to this:

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
broker.url=tcp://localhost:1883

But when I want to save the file, I already get an error in the events.log :

[ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
MqttException (0) - java.net.NoRouteToHostException: No route to host (Host unreachable)
	at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:38)[208:org.openhab.io.transport.mqtt:1.10.0]
	at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:664)[208:org.openhab.io.transport.mqtt:1.10.0]
	at java.lang.Thread.run(Thread.java:745)[:1.8.0_121]
Caused by: java.net.NoRouteToHostException: No route to host (Host unreachable)
	at java.net.PlainSocketImpl.socketConnect(Native Method)[:1.8.0_121]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)[:1.8.0_121]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)[:1.8.0_121]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)[:1.8.0_121]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)[:1.8.0_121]
	at java.net.Socket.connect(Socket.java:589)[:1.8.0_121]
	at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)[208:org.openhab.io.transport.mqtt:1.10.0]
	at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)[208:org.openhab.io.transport.mqtt:1.10.0]
	... 1 more

When I use mqtt.fx I can successfully subscribe and publish on my raspberry’s IP-address.

Also, I noticed from the mosquitto.log file (/var/log/mosquitto/mosquitto.log) that when my ESP8266-12S wifi module is trying to connect, it is also disconnecting continuously because of an socket error.

1499945603: Socket error on client Device1, disconnecting.
1499945603: New connection from 192.168.0.170 on port 1883.
1499945603: New client connected from 192.168.0.170 as Device1 (c1$
1499945693: Client Device4 has exceeded timeout, disconnecting.
1499945693: Socket error on client Device1, disconnecting.
1499945766: New connection from 192.168.0.170 on port 1883.
1499945766: New client connected from 192.168.0.170 as Device1 (c1$
1499945855: Client Device1 has exceeded timeout, disconnecting.
1499945855: Socket error on client Device1, disconnecting.

Does anyone know how I can fix this error? Or what is causing it?

I’m btw new here and an beginner at openhab, so already sorry if I ask some stupid questions or making a lot of mistakes :wink:

Thanks in advance!

check please the contents of the following file:

/var/lib/openhab2/config/org/openhab/mqtt.config

if they correspond to the settings that you have in /etc/openhab2/services/mqtt.cfg

If not, shutdown OH2, delete the mqtt.config (not the mqtt.cfg) and then restart OH2

Now, for the other issue:

can you post the contents of your /etc/mosquitto/mosquitto.conf

Hi @Dim, thanks for the fast response!

In the file:

/var/lib/openhab2/config/org/openhab/mqtt.config

I can only see the following lines:

broker.url="tcp://localhost:1883"
service.pid="org.openhab.mqtt"

So I think that’s ok right? And in this file:

/etc/mosquitto/mosquitto.conf

I found this:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

configs look good…

I don’t understand why this error is coming up:

[ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
MqttException (0) - java.net.NoRouteToHostException: No route to host (Host unreachable)

can you try something (just to see if this will help):
change in your /etc/openhab2/services/mqtt.cfg the line to:

broker.url=tcp://127.0.0.1:1883

and add the following line in /etc/mosquitto/mosquitto.conf (at the end):

port 1883

Although, the Mosquitto MQTT Broker is already listening on the default port 1883…

Question: Do you have the MQTT Action addon installed? (if yes, uninstall it and try again)

How do you connect to the LAN (using Wireless or Wired?) Which linux distribution are you using? (raspbian, openHABian, other?)

Also, try to increase the log level within the openHAB console (ssh openhab@localhost -p 8101 with password: habopen):

log:set TRACE org.openhab.binding.mqtt
log:set TRACE org.openhab.io.transport.mqtt

(put it back to WARN after collecting some logs :slight_smile:)

When I changed the settings like you said, the openhab.log shows this:

2017-07-13 23:30:14.531 [TRACE] [penhab.io.transport.mqtt.MqttService] - Processing property: broker.url = tcp://127.0.0.1:1883
2017-07-13 23:30:14.534 [TRACE] [penhab.io.transport.mqtt.MqttService] - Processing property: mqtt.url = tcp://localhost:1883
2017-07-13 23:30:14.536 [TRACE] [penhab.io.transport.mqtt.MqttService] - Processing property: r.url = tcp://localhost:1883
2017-07-13 23:30:14.537 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2017-07-13 23:30:14.540 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'r'
2017-07-13 23:30:14.542 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mqtt'
2017-07-13 23:30:14.544 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'broker'

There are no errors showing up. But earlier today, I changed the word ‘broker’ in to other words like ‘mqtt’ and (by accident) ‘r’. This also helped and I haven’t seen errors on the openhab.log since then. But now I changed it back to the word ‘broker’ and these are still showing up in the logs and starting an connection… Is this normal?

Question: Do you have the MQTT Action addon installed? (if yes, uninstall it and try again)

No, I have no actions installed.

How do you connect to the LAN (using Wireless or Wired?)  Which linux distribution are you using? (raspbian, openHABian, other?)

My Rapsberry Pi is connected to a LAN port on my router with an ethernet cable and I’m using openhabian.

I see that I only still have errors on the mosquitto.log
I tried to use other programs on the ESP8266 module, but I still got the same issues when the module is powered:

1499982270: Client Client Name has exceeded timeout, disconnecting.
1499982270: Socket error on client Client Name, disconnecting.
1499982280: New connection from 192.168.0.170 on port 1883.
1499982280: New client connected from 192.168.0.170 as Client Name (c1, k15).
1499982301: Client Client Name has exceeded timeout, disconnecting.
1499982301: Socket error on client Client Name, disconnecting.
1499982311: New connection from 192.168.0.170 on port 1883.
1499982311: New client connected from 192.168.0.170 as Client Name (c1, k15).
1499982332: Client Client Name has exceeded timeout, disconnecting.
1499982332: Socket error on client Client Name, disconnecting.
1499982342: New connection from 192.168.0.170 on port 1883.

The IP-address of the client is 192.168.0.170 as you can see and is connecting and disconnecting all the time…

Now, 3 broker connections have been configured in your system :slight_smile:
(check /var/lib/openhab2/config/org/openhab/mqtt.config)

Stop OH2, delete file /var/lib/openhab2/config/org/openhab/mqtt.config, then Start OH2 (keep your /etc/openhab2/services/mqtt.cfg file properly configured with 1 correct broker config)

This should take care of the stale entries.

For the disconnects of the ESP… I don’t know what could be wrong… maybe add a keepalive in the ESP code? (change also the Client Name to something else…maybe without a space)

After I removed the mqtt.config the right broker is starting a connection, without showing errors.
I don’t know why the errors are gone but that’s already a part of the problem that’s solved!

I’m sorry, but what do you mean with a keepalive code? (I’m not a really experienced programmer :slight_smile: )
I’m btw not able to send any commands to the ESP nor receiving. I tried to use the mqtt.fx to send some basic commands to the ESP but it didn’t do anything. So I’m really stuck at this point, because I don’t know what to do or where to look…

The ESP is not holding the connection open to the MQTT Broker for some reason…
It could be any number of reasons behind this problem…
networking, broker, client code, etc

can you post your ESP sketch/code to check it?
Are you using a framework for the ESP? (like ESPEasy etc)

It seems that openHAB is now properly connected to the MQTT Borker. Just for a double check: Can you see with mqtt.fx if openHAB is publishing to the topic? (have you configured a MQTT bound item like a switch to publish info?)

I’m using an arduino to program the ESP.
The code of the ESP:

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

/* WIFI Settings */
// Name of wifi network
const char* ssid = "ssid";

// Password to wifi network
const char* password = "password"; 

/* Web Updater Settings */
// Host Name of Device
const char* host = "Device1";

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = "/firmware";

// Username to access the web update page
const char* update_username = "admin";

// Password to access the web update page
const char* update_password = "password";

/* MQTT Settings */
// Topic which listens for commands
char* subscribeTopic = "Relay"; 

//MQTT Server IP Address
const char* server = "192.168.0.142";


//Unique device ID 
const char* mqttDeviceID = "Device1"; 


int channel1 = 14;
int channel2 = 12;
int channel3 = 13;

//webserver 
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

//MQTT
WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

//Connect to WiFI and MQTT
void connect();

//Setup pins, wifi, webserver and MQTT
void setup() 
{
  // set pin modes
  pinMode(channel1, OUTPUT);
  digitalWrite(channel1, LOW);

  WiFi.mode(WIFI_STA);


  WiFi.begin(ssid, password);
  client.begin(server, net);

  connect();

  MDNS.begin(host);

  httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  httpServer.begin();

  MDNS.addService("http", "tcp", 80);
}

//Connect to wifi and MQTT
void connect() 
{
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
  }

  while (!client.connect(mqttDeviceID)) 
  {
    delay(1000);
  }

  client.subscribe(subscribeTopic);
}

void loop() 
{
  // MQTT Loop
  client.loop();
  delay(10);

  // Make sure device is connected
  if(!client.connected()) 
  {
    connect();
  }

  httpServer.handleClient();

}

// Change the state of a relay based on the MQTT Message
void messageReceived(String topic, String payload, char * bytes, unsigned int length) 
{
    String msgString = payload;

  if (msgString == "Z1ON")
  {
    digitalWrite(channel1, HIGH);
    delay(250);
  }
  else if (msgString == "Z1OFF")
  {
    digitalWrite(channel1, LOW);
    delay(250);
  }
}

(I’m using the code from the website: https://www.mksmarthouse.com/sprinkler-system but simplified the code for controlling just one relay.)

I tested the mqtt binding with two switches, one for outbound and the other for inbound.
One switch is named “mySwitch” with topic light which was able to publish, because I could see the commands on mqtt.fx and in the openhab.log.
The other switch named “doorbell” with topic doorbell (for testing inbound messages) was able to get ‘ON’ with mqtt.fx when I published the command ‘ON’.

This gave the following log:

2017-07-14 13:05:53.243 [ItemCommandEvent          ] - Item 'mySwitch' received command ON
==> /var/log/openhab2/openhab.log <==
2017-07-14 13:05:53.246 [DEBUG] [inding.mqtt.internal.MqttItemBinding] - Publishing command ON to light
2017-07-14 13:05:53.250 [DEBUG] [t.mqtt.internal.MqttBrokerConnection] - Publishing message 139 to topic 'light'
2017-07-14 13:05:53.250 [TRACE] [t.mqtt.internal.MqttBrokerConnection] - Message with id 139 delivered.
==> /var/log/openhab2/events.log <==
2017-07-14 13:05:53.260 [ItemStateChangedEvent     ] - mySwitch changed from OFF to ON
2017-07-14 13:05:56.001 [ItemCommandEvent          ] - Item 'mySwitch' received command OFF
==> /var/log/openhab2/openhab.log <==
2017-07-14 13:05:56.004 [DEBUG] [inding.mqtt.internal.MqttItemBinding] - Publishing command OFF to light
2017-07-14 13:05:56.008 [TRACE] [t.mqtt.internal.MqttBrokerConnection] - Message with id 140 delivered.
2017-07-14 13:05:56.008 [DEBUG] [t.mqtt.internal.MqttBrokerConnection] - Publishing message 140 to topic 'light'
==> /var/log/openhab2/events.log <==
2017-07-14 13:05:56.022 [ItemStateChangedEvent     ] - mySwitch changed from ON to OFF
==> /var/log/openhab2/openhab.log <==
2017-07-14 13:06:01.149 [TRACE] [t.mqtt.internal.MqttBrokerConnection] - Received message on topic 'doorbell' : ON
==> /var/log/openhab2/events.log <==
2017-07-14 13:06:01.156 [ItemCommandEvent          ] - Item 'doorbell' received command ON
2017-07-14 13:06:01.172 [ItemStateChangedEvent     ] - doorbell changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
2017-07-14 13:06:03.638 [TRACE] [t.mqtt.internal.MqttBrokerConnection] - Received message on topic 'doorbell' : OFF
==> /var/log/openhab2/events.log <==
2017-07-14 13:06:03.645 [ItemCommandEvent          ] - Item 'doorbell' received command OFF
2017-07-14 13:06:03.661 [ItemStateChangedEvent     ] - doorbell changed from ON to OFF

Hey guys , Did you find a solution for this problem .
I have the same issue and struggle too get the service running.
Have tried different settings in .cfg file and config, even installed on a different machine and get the same error.
Can you guys help me out and give me some hints where to look.

2018-02-17 11:50:37.435 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'globalcache.things’
2018-02-17 11:50:40.517 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at http://192.168.1.150:8080
2018-02-17 11:50:40.963 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at https://192.168.1.150:8443
2018-02-17 11:50:41.128 [INFO ] [basic.internal.servlet.WebAppServlet] - Started Basic UI at /basicui/app
2018-02-17 11:50:41.159 [INFO ] [arthome.ui.paper.internal.PaperUIApp] - Started Paper UI at /paperui
2018-02-17 11:50:41.415 [INFO ] [panel.internal.HABPanelDashboardTile] - Started HABPanel at /habpanel
2018-02-17 11:50:41.906 [INFO ] [e.internal.GlobalCacheHandlerFactory] - GlobalCache binding v2.1.0
2018-02-17 11:50:42.249 [INFO ] [b.core.service.AbstractActiveService] - Modbus Polling Service has been started
2018-02-17 11:50:42.277 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2018-02-17 11:50:42.278 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'broker’
2018-02-17 11:50:44.745 [ERROR] [penhab.io.transport.mqtt.MqttService] - Error starting broker connection
Unable to connect to server (32103) - java.net.ConnectException: Connection refused: connect
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)[200:org.openhab.io.transport.mqtt:1.10.0]
at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:650)[200:org.openhab.io.transport.mqtt:1.10.0]
at java.lang.Thread.run(Unknown Source)[:1.8.0_151]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)[:1.8.0_151]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)[:1.8.0_151]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)[:1.8.0_151]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)[:1.8.0_151]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)[:1.8.0_151]
at java.net.PlainSocketImpl.connect(Unknown Source)[:1.8.0_151]
at java.net.SocksSocketImpl.connect(Unknown Source)[:1.8.0_151]
at java.net.Socket.connect(Unknown Source)[:1.8.0_151]
at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)[200:org.openhab.io.transport.mqtt:1.10.0]
… 2 more