Mqtt conection refused sometimes

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:

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

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!

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

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.

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?

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

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?

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.

I found this client written in C:

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


different mqtt client implementation, same problem:

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:

^CFailed to start disconnect, return code: Disconnected

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

Let me sleep over that...

It may be overkill but it may just work

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

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.