Node-red-node-pi-gpiod localhost connection refused, try this

Just thought id leave this here as i had some trouble with it:
If you are trying to use node-red-node-pi-gpiod and you are getting an error that the connection on localhost was refused although the daemon is running try changing localhost to ::1.
This is the way to loopback to localhost via ipv6 which when available is now being used by default by raspberry pi os if available. The daemon binds to this if started with the -l flag and up v6 is the Standard.
Maybe this saves somebody some googling.

Johannes

That is odd, I would have thought both would work. Does 127.0.0.1 work?

Thats what the node tries by default when localhost is entered.
I think if it actually tried localhost it might work but the problem is that its trying 127.0.0.1 but I also tried entering it directly no change, only ::1 will work.

It shouldn't it should pass localhost to the OS. Traditonally localhost is mapped to 127.0.0.1 by the OS

$ ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.100 ms

but I that on a recent raspbian I get

$ ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.061 ms

This happens because in /etc/hosts is

127.0.0.1	localhost
::1		localhost ip6-localhost ip6-loopback

so if IPV6 is enabled it uses ::1. I am surprised that pigpiod cares though, either should work. What does ping localhost show?

pi@testipi3:~ $ ping localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.104 ms

The underlying js-pigpio library seems to automatically convert it. I think the problem might be also that when the pgpio daemon is started with the -l flag that only allows local connections it binds to ip v6 if enabled and than won’t accept the connection on 127.0.0.1.
Other people have solved it by adding the -n 127.0.0.1 to their pigpiod exec argument to also bind it to ip v4 localhost.

OK. I am not going to say I fully understand, as I don't.

The pigpiod node tries to connect to the pigpio daemon that can be installed and run on raspberries. For some things like pmw it’s a lot more efficient to use this daemon instead of the normal gpio nodes to control them directly.
This daemon has as one of its start arguments the option to bind it to localhost so that it can’t be accessed from other machines as you don’t want to necessarily expose your gpio control on the network.
I think what happens is that when it v6 is enabled it binds the daemon to only listen on ::1 and this way connections via 127.0.0.1 are refused by the daemon.
Unfortunately the node or it’s underlying library tries to do just that if localhost is entered. I think this happens somewhere in the underlying python libraries used.
Well at least that’s what I think is happening.

EDIT

Just confirmed this by adding the -n 127.0.0.1 argument to the systemd service file that starts the daemon to also bind it to ipv4 localhost 127.0.0.1 and now the node can connect with both localhost or 127.0.0.1 to the daemon.
So either using ::1 in the node or starting the daemon with a binding also to 127.0.0.1 will work.

OK, that makes sense.

1 Like