MQTT connection to broker was lost when sketch arduino is connected

Hi
I have one problem when i connect the sketch to network

my topology is the next

arduino 1+ ethenet sheld : 192.168.1.75
raspberry : 192.168.1.67

mosquitto

1474658886: mosquitto version 1.4.10 (build date Thu, 25 Aug 2016 10:12:09 +0100) starting
1474658886: Using default config.
1474658886: Opening ipv4 listen socket on port 1883.
1474658886: Error: Address already in use

openhab

14:50:21.826 [INFO ] [uartz.impl.StdSchedulerFactory:1324 ] - Quartz scheduler ‘openHAB-job-scheduler’ initialized from specified file: '/etc/openhab/quartz.properties’
14:50:21.828 [INFO ] [uartz.impl.StdSchedulerFactory:1328 ] - Quartz scheduler version: 2.1.7
14:50:21.831 [INFO ] [rg.quartz.core.QuartzScheduler:534 ] - Scheduler openHAB-job-scheduler_$_NON_CLUSTERED started.
14:50:21.843 [DEBUG] [o.config.core.ConfigDispatcher:146 ] - Processing openHAB default configuration file ‘/usr/share/openhab/configurations/openhab_default.cfg’.
14:50:21.847 [DEBUG] [o.config.core.ConfigDispatcher:168 ] - Processing openHAB main configuration file ‘/etc/openhab/configurations/openhab.cfg’.
14:50:22.049 [DEBUG] [.o.core.internal.CoreActivator:93 ] - UUID file already exists at ‘/usr/share/openhab/webapps/static/uuid’ with content '59fbe0a3-e726-42d9-baba-8fbe9fc28f3d’
14:50:22.054 [DEBUG] [.o.core.internal.CoreActivator:115 ] - Created file ‘/usr/share/openhab/webapps/static/version’ with content '1.8.3’
14:50:22.057 [INFO ] [.o.core.internal.CoreActivator:61 ] - openHAB runtime has been started (v1.8.3).
14:50:22.319 [DEBUG] [a.internal.AutoUpdateActivator:31 ] - AutoUpdate binding has been started.
14:50:26.346 [DEBUG] [.p.i.PersistenceModelActivator:23 ] - Registered ‘persistence’ configuration parser
14:50:26.393 [DEBUG] [.c.t.i.TransformationActivator:38 ] - Transformation Service has been started.
14:50:26.481 [DEBUG] [m.internal.MultimediaActivator:34 ] - Multimedia I/O bundle has been started.
14:50:26.531 [DEBUG] [.s.i.DiscoveryServiceActivator:27 ] - Discovery service has been started.
14:50:26.544 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl:93 ] - mDNS service has been started
14:50:26.574 [DEBUG] [.io.transport.mqtt.MqttService:123 ] - Starting MQTT Service…
14:50:26.727 [INFO ] [o.o.i.s.i.DiscoveryServiceImpl:53 ] - Service Discovery initialization completed.
14:50:26.738 [INFO ] [.io.transport.mqtt.MqttService:108 ] - MQTT Service initialization completed.
14:50:26.739 [INFO ] [o.i.t.m.i.MqttBrokerConnection:114 ] - Starting MQTT broker connection 'mymosquitto’
14:50:26.747 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:294 ] - Creating new client for ‘tcp://192.168.1.67:1883’ using id ‘openhab_123’ and file store ‘/tmp/mymosquitto’

14:50:45.950 [INFO ] [.o.io.habmin.HABminApplication:182 ] - Started HABmin REST API at /services/habmin
14:50:45.951 [DEBUG] [o.o.i.s.i.DiscoveryServiceImpl:66 ] - Registering new service _openhab-server._tcp.local. at port 8080
14:50:48.699 [DEBUG] [o.o.i.s.i.DiscoveryServiceImpl:66 ] - Registering new service _openhab-server-ssl._tcp.local. at port 8443
14:50:51.377 [DEBUG] [.a.mqtt.internal.MqttActivator:34 ] - Mqtt action has been started.
14:50:51.400 [DEBUG] [.zwave.internal.ZWaveActivator:36 ] - Z-Wave binding started. Version 1.8.0.201508211938
14:50:51.454 [DEBUG] [.z.internal.ZWaveActiveBinding:175 ] - ConverterHandler not initialised. Polling disabled.
14:50:51.457 [INFO ] [.z.internal.ZWaveActiveBinding:317 ] - ZWave ‘updated’ with null config
14:50:51.463 [DEBUG] [.b.mqtt.internal.MqttActivator:34 ] - MQTT binding has been started.
14:50:51.494 [DEBUG] [i.internal.GenericItemProvider:341 ] - Start processing binding configuration of Item ‘Temp (Type=NumberItem, State=Uninitialized)’ with ‘MqttGenericBindingProvider’ reader.
14:50:51.502 [DEBUG] [b.mqtt.internal.MqttItemConfig:72 ] - Loaded MQTT config for item ‘Temp’ : 1 subscribers, 0 publishers
14:50:51.505 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/temperature’
14:50:51.517 [DEBUG] [i.internal.GenericItemProvider:341 ] - Start processing binding configuration of Item ‘Hum (Type=NumberItem, State=Uninitialized)’ with ‘MqttGenericBindingProvider’ reader.
14:50:51.519 [DEBUG] [b.mqtt.internal.MqttItemConfig:72 ] - Loaded MQTT config for item ‘Hum’ : 1 subscribers, 0 publishers
14:50:51.521 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/humedad’
14:50:51.537 [DEBUG] [m.internal.MqttEventBusBinding:68 ] - MQTT: Activating event bus binding.
14:50:51.560 [DEBUG] [o.p.m.i.MqttPersistenceService:100 ] - Deactivating MQTT Persistence
14:50:51.563 [WARN ] [o.p.m.i.MqttPersistenceService:123 ] - mqtt-persistence:broker
14:50:51.565 [WARN ] [o.p.m.i.MqttPersistenceService:123 ] - mqtt-persistence:topic
14:50:51.566 [WARN ] [o.p.m.i.MqttPersistenceService:123 ] - mqtt-persistence:message
14:50:51.569 [DEBUG] [o.p.m.i.MqttPersistenceService:81 ] - Configuration updated for MQTT Persistence.
14:50:51.571 [DEBUG] [o.p.m.i.MqttPersistenceService:84 ] - Configuration incomplete. Cannot start yet.
14:50:51.581 [DEBUG] [.p.internal.PersistenceManager:146 ] - Initializing mqtt persistence service.
14:50:51.589 [DEBUG] [c.internal.ModelRepositoryImpl:64 ] - Configuration model ‘mqtt.persist’ can not be found
14:50:51.594 [DEBUG] [.m.internal.MqttitudeActivator:33 ] - Mqttitude binding has been started.
14:50:51.627 [DEBUG] [.b.m.internal.MqttitudeBinding:84 ] - Activating Mqttitude binding

Configuration for openhab.cfg and mqtt

mqtt:mymosquitto.url=tcp://192.168.1.67:1883
mqtt:mymosquitto.clientId=openhab_123

When the openhab service is started , it create one directory with the clientid of mosquitto

drwxr-xr-x 2 openhab openhab 4096 Sep 23 14:50 openhab_123-tcp1921681671883
pi@raspberrypi:/tmp/mymosquitto $


but when i connect the arduino to network . appear the problem : <>
and this directory is removed

pi@raspberrypi:/tmp/mymosquitto $ ls -l
total 0
pi@raspberrypi:/tmp/mymosquitto $


15:01:07.484 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/temperature’
15:01:07.487 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/humedad’
15:01:08.029 [ERROR] [o.i.t.m.i.MqttBrokerConnection:536 ] - MQTT connection to broker was lost
org.eclipse.paho.client.mqttv3.MqttException: Connection lost
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138) [mqtt-client-0.4.0.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65]
Caused by: java.io.EOFException: null
at java.io.DataInputStream.readByte(DataInputStream.java:267) ~[na:1.8.0_65]
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56) ~[na:na]
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100) [mqtt-client-0.4.0.jar:na]
… 1 common frames omitted
15:01:08.030 [ERROR] [o.i.t.m.i.MqttBrokerConnection:540 ] - MQTT connection to ‘mymosquitto’ was lost: Connection lost : ReasonCode 32109 : Cause : null
15:01:08.032 [INFO ] [o.i.t.m.i.MqttBrokerConnection:547 ] - Starting connection helper to periodically try restore connection to broker ‘mymosquitto’

Any idea where is the problem…

Best regards!!!

Are you using a unique client ID for openHAB and all of your devices? If you try to connect with the same clientID from another device it will kick any client with the same name off the broker.

I have one client.
the network only have 2 nodes. raspberry and arduino.

openhab try create the connection but it failure.

pi@raspberrypi:/tmp/mymosquitto $ ls -l
total 0
pi@raspberrypi:/tmp/mymosquitto $ ls -l
drwxr-xr-x 2 openhab openhab 4096 Sep 23 16:06 openhab_123-tcp1921681671883
pi@raspberrypi:/tmp/mymosquitto $ ls -l
total 0
pi@raspberrypi:/tmp/mymosquitto $ ls -l
total 0
pi@raspberrypi:/tmp/mymosquitto $ netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :8001 : LISTEN
tcp 0 0 :ssh : LISTEN
tcp 0 0 :1883 : LISTEN
tcp 0 0 192.168.1.67:1883 192.168.1.75:50001 TIME_WAIT
tcp 0 0 192.168.1.67:ssh 192.168.1.76:50975 ESTABLISHED
tcp 0 0 192.168.1.67:1883 192.168.1.75:49999 TIME_WAIT
tcp 0 256 192.168.1.67:ssh 192.168.1.76:50974 ESTABLISHED
tcp 0 0 192.168.1.67:1883 192.168.1.75:49996 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.67:59978 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.67:59974 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.67:59976 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.75:50002 ESTABLISHED
tcp 0 0 192.168.1.67:1883 192.168.1.67:59972 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.75:50000 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.75:49998 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.67:59970 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.67:59968 TIME_WAIT
tcp 0 0 192.168.1.67:1883 192.168.1.75:49997 TIME_WAIT
tcp6 0 0 [::]:http-alt [::]:
LISTEN
tcp6 0 0 [::]:5555 [::]:
LISTEN
tcp6 0 0 [::]:ssh [::]:
LISTEN
tcp6 0 0 [::]:8443 [::]:* LISTEN
tcp6 0 0 [::]:1883 [::]:* LISTEN
pi@raspberrypi:/tmp/mymosquitto $

the code for sketch is

byte MAC_ADDRESS[] = { 0xFE, 0xED, 0xDE, 0xAD, 0xBE, 0xEF };

// IP address of MQTT server
byte MQTT_SERVER[] = { 192, 168, 1, 67 };
byte ip[] = {192, 168, 1, 75 };

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Handles messages arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
}

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

void setup() {
Serial.begin(9600);
Ethernet.begin(MAC_ADDRESS,ip);
lcd.begin(16, 2);
dht.begin();
}

void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
char stemp[10];
char shumedad[10];

if (!client.connected())
{
client.connect(“openhab_123”);

}

if (isnan(t) || isnan(h)) {
Serial.println(“Failed to read from DHT”);
}
else {
Serial.print(“Temp :”);Serial.println(t);
Serial.print(“Humedad :”);Serial.println(h);

dtostrf(t,6,3,stemp);
client.publish(“home/temperature”,stemp);
//client.publish(“home/temperature/state”,stemp);
//client.publish(“temperature”,stemp);
dtostrf(h,6,3,shumedad);
client.publish(“home/humedad”,shumedad);
delay(5000);
}
}

You have two clients, openHAB is itself a client of the Broker (Mosquitto I’m assuming) and your Arduino is also client of the Broker. They must both have a unique name.

What do you see in the Broker’s logs? What do you see in the openHAB logs at that same time?


The log for mosquitto is the next

1474683939: home/temperature (QoS 0)
1474683939: Sending SUBACK to openhab_123
1474683939: Received SUBSCRIBE from openhab_123
1474683939: home/humedad (QoS 0)
1474683939: Sending SUBACK to openhab_123
1474683939: Sending CONNACK to openhab_123 (0, 0)
1474683939: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/temperature’, … (6 bytes))
1474683939: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/humedad’, … (6 bytes))
1474683944: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/temperature’, … (6 bytes))
1474683944: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/humedad’, … (6 bytes))
1474683949: Sending CONNACK to openhab_123 (0, 0)
1474683949: Received SUBSCRIBE from openhab_123
1474683949: home/temperature (QoS 0)
1474683949: Sending SUBACK to openhab_123
1474683949: Received SUBSCRIBE from openhab_123
1474683949: home/humedad (QoS 0)
1474683949: Sending SUBACK to openhab_123
1474683950: Sending CONNACK to openhab_123 (0, 0)
1474683950: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/temperature’, … (6 bytes))
1474683950: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/humedad’, … (6 bytes))
1474683955: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/temperature’, … (6 bytes))
1474683955: Received PUBLISH from openhab_123 (d0, q0, r0, m0, ‘home/humedad’, … (6 bytes))
1474683960: Sending CONNACK to openhab_123 (0, 0)
1474683960: Received SUBSCRIBE from openhab_123


the log for openhab

21:26:00.224 [INFO ] [o.i.t.m.i.MqttBrokerConnection:114 ] - Starting MQTT broker connection 'mymosquitto’
21:26:00.237 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/temperature’
21:26:00.241 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/humedad’
21:28:48.509 [ERROR] [o.i.t.m.i.MqttBrokerConnection:536 ] - MQTT connection to broker was lost
org.eclipse.paho.client.mqttv3.MqttException: Connection lost
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138) [mqtt-client-0.4.0.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65]
Caused by: java.io.EOFException: null
at java.io.DataInputStream.readByte(DataInputStream.java:267) ~[na:1.8.0_65]
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56) ~[na:na]
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100) [mqtt-client-0.4.0.jar:na]
… 1 common frames omitted
21:28:48.511 [ERROR] [o.i.t.m.i.MqttBrokerConnection:540 ] - MQTT connection to ‘mymosquitto’ was lost: Connection lost : ReasonCode 32109 : Cause : null
21:28:48.513 [INFO ] [o.i.t.m.i.MqttBrokerConnection:547 ] - Starting connection helper to periodically try restore connection to broker 'mymosquitto’
21:28:58.516 [INFO ] [o.i.t.m.i.MqttBrokerConnection:114 ] - Starting MQTT broker connection 'mymosquitto’
21:28:58.528 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/temperature’
21:28:58.530 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/humedad’
21:28:59.082 [ERROR] [o.i.t.m.i.MqttBrokerConnection:536 ] - MQTT connection to broker was lost
org.eclipse.paho.client.mqttv3.MqttException: Connection lost
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138) [mqtt-client-0.4.0.jar:na]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65]
Caused by: java.io.EOFException: null
at java.io.DataInputStream.readByte(DataInputStream.java:267) ~[na:1.8.0_65]
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56) ~[na:na]
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100) [mqtt-client-0.4.0.jar:na]
… 1 common frames omitted
21:28:59.083 [ERROR] [o.i.t.m.i.MqttBrokerConnection:540 ] - MQTT connection to ‘mymosquitto’ was lost: Connection lost : ReasonCode 32109 : Cause : null
21:28:59.084 [INFO ] [o.i.t.m.i.MqttBrokerConnection:547 ] - Starting connection helper to periodically try restore connection to broker 'mymosquitto’
21:29:09.087 [INFO ] [o.i.t.m.i.MqttBrokerConnection:114 ] - Starting MQTT broker connection 'mymosquitto’
21:29:09.100 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic 'home/temperature’
21:29:09.104 [DEBUG] [o.i.t.m.i.MqttBrokerConnection:476 ] - Starting message consumer for broker ‘mymosquitto’ on topic ‘home/humedad’

Configuration for openhab.cfg and mqtt

mqtt:mymosquitto.url=tcp://192.168.1.67:1883
mqtt:mymosquitto.clientId=openhab_123

Configuration for arduino

if (!client.connected())
{
client.connect(“openhab_123”);

}

fix the problem,

Thanks!!!

Best Regards!!!

Hi Francisco,

Cool you fixed the problem !

But … how ?!