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:
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
but sometimes I see this
Error: Connection refused
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.
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.
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?
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.
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.