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.
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.
Where is node-red running?
Where is the script that calls mosquitto_sub running?
Is the value that you want to publish, a value that you already have in node-red?
What s/w is subscribing to the MQTT topic to pick up the value being published?
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?
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.
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.