# Mqtt conection refused sometimes

**URL:** <https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927>\
**Category:** General\
**Created:** [13 February 2021 12:34 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927 "2021-02-13T12:34:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 12:34 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/1 "2021-02-13T12:34:24Z")

</div>

Hello together,

I'm tinkering a camera device with a pir motion sensor for the door.  
Looks good so far. There's only one problem.

I use a script that reads the signal from the pir sensor and when motion is detected it publishes a message to the mosquitto mqtt broker. This works also fine but not very reliable.

I use the mosquitto\_pub command delivered with mosquitto.  
When this is working the log shows this:

```auto
no motion
no motion
motion detected
Client (null) sending CONNECT
Client (null) received CONNACK (0)
Client (null) sending PUBLISH (d0, q1, r0, m1, 'raspis/entrycam/motion', ... (3 bytes))
Client (null) received PUBACK (Mid: 1, RC:0)
Client (null) sending DISCONNECT
no motion
no motion

```

but sometimes I see this

```auto
no motion
no motion
motion detected
Error: Connection refused
no motion
no motion

```

I do not understand why this happens. When this happens it takes some time.  
Then it works again as expected until it fails again after uncertain time.

First I thought I had some mosquitto configuration, that there is some limit there but I do not see what is misconfigured.  
I also do not see anything in the mosquitto logs, when this happens. The connection to the device is stable. I'm connected via ssh and this is stable.

The command I use is :

mosquitto\_pub -h broker -t raspis/entrycam/motion -u user -P password -m 'yes' -q 1 -d

Any ideas?

Help is very appreciated.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2021 13:21 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/2 "2021-02-13T13:21:51Z")

</div>

Why don't you use the node red mqtt node to publish it?

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 15:39 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/3 "2021-02-13T15:39:40Z")

</div>

What do you mean. I'm relative new to node-red.  
I use some shelly devices and this is my first self tinkered device.  
Do you have an example or a link for me?

I was looking for mqtt clients and found only the mosquitto reference implementation.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2021 15:54 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/4 "2021-02-13T15:54:30Z")

</div>

I wasn't suggesting using a different broker (mosquitto is the mqtt broker, your script or node-red is the mqtt client).  
Perhaps I need to ask some different questions first.

1. Where is node-red running?
2. Where is the script that calls mosquitto\_sub running?
3. Is the value that you want to publish, a value that you already have in node-red?
4. What s/w is subscribing to the MQTT topic to pick up the value being published?

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 16:05 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/5 "2021-02-13T16:05:30Z")

</div>

Sorry! I see. Not enough information.

I have mosquitto and node-red running on a raspberry pi 4 in my network.  
The script that runs the mosquitto\_pub runs on a different pi zero which is connected via wifi to the network.  
I was looking for a lightweight mqtt subscribe and publish client which can be run from the pi zero CLI. Maybe later I will use something other like C or another programming language.  
The pi zero has a PIR sensor which detects motion. For now I only want to publish the information that there is motion detected and node-red triggers some shelly device to maybe switch on the light.

By the way, I was using this guide for orientation: [Using The Mosquitto\_pub and Mosquitto\_sub MQTT Client Tools- Examples](http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2021 16:49 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/6 "2021-02-13T16:49:56Z")

</div>

OK, so your question isn't anything to do with node-red, however it is easy to change that. In your situation I would run node-red on the Zero too and publish to the pi 4 from there. The Zero will easily cope with node-red. Why mess about with scripts when you can do it in node-red?

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 18:48 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/7 "2021-02-13T18:48:31Z")

</div>

Isn't that a bit oversized?  
I only want to publish two kind of messages from the pi zero.  
One is the motion if detected and the second one will be the temperature outside.

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 19:44 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/8 "2021-02-13T19:44:48Z")

</div>

I found this client written in C:

> **[Eclipse Paho | The Eclipse Foundation](https://www.eclipse.org/paho/index.php?page=clients%2Fc%2Findex.php)**
>
> The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 375 open source projects, including runtimes, tools and frameworks.

Unlike others it is available for the armv6h platform. I'll give it a try...

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [13 February 2021 20:13 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/9 "2021-02-13T20:13:45Z")

</div>

hmm,

different mqtt client implementation, same problem:

```auto
no motion
no motion
no motion
no motion
motion detected
motion detected
Connect failed, rc Failure

```

But it's worse, this client is completely hanging up then.  
😐

If I press CTRL-C then I got this:

```auto
^CFailed to start disconnect, return code: Disconnected

```

Afterwards, the loop goes on.  
Looks like some problem while connected.

Let me sleep over that...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 February 2021 20:32 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/10 "2021-02-13T20:32:13Z")

</div>

It may be overkill but it may just work

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2021 20:45 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/11 "2021-02-13T20:45:10Z")

</div>

> [@tazinblack](#):
>
> I'll give it a try...

Why, the problem is not mosquitto, it is solid and reliable.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 February 2021 20:50 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/12 "2021-02-13T20:50:21Z")

</div>

> [@tazinblack](#):
>
> Isn't that a bit oversized

If you have a Pi you might as well use it, why not?

In fact if that is all you are doing then all you need is something like a Wemos D1 Mini or one of the other similar devices. More effort to get going but once running you can forget about it. No SD card to go AWOL when you unexpectedly loose power.

---

<div class="post-metadata">

**Author:** ![tazinblack](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tazinblack/32/37140_2.png) [@tazinblack](https://discourse.nodered.org/u/tazinblack)\
**Post date:** [6 April 2021 05:35 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/13 "2021-04-06T05:35:29Z")

</div>

Finally found the problem.  
The reason was that I did use the DNS name for the MQTT broker and not the IP.  
Since my fritzbox does IP networking in dual stack mode I have IPv4 and IPv6 working in parallel.  
When I do a name resolve via DNS I get sometimes the IPv4 address and sometimes the IPv6 address. And the problem is, that my mosquitto seems to handle only IPv4 requests.

Thanks for all your hints!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 April 2021 05:36 UTC](https://discourse.nodered.org/t/mqtt-conection-refused-sometimes/40927/14 "2021-04-20T05:36:16Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
