How to share Aeotec Z-Stick USB over network using socat (useful for unprivileged LXC containers)

For Z-Wave related questions in Domoticz

Moderator: leecollings

Post Reply
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

How to share Aeotec Z-Stick USB over network using socat (useful for unprivileged LXC containers)

Post by ben53252642 »

Assuming Debian (this is for very advanced Linux users, no support / warranty is provided):

On USB server (device with the z-stick plugged in)

1) apt-get install socat screen

2) contents of /etc/init.d/usbserver

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: usbserver
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description:
### END INIT INFO

case "$1" in
'start')
        /usr/bin/screen -S socatmonitor -d -m /scripts/socatserver.sh
        ;;
'stop')
        pkill -f 'socatmonitor'
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
3) contents of /scripts/socatserver.sh

Code: Select all

#!/bin/bash
while true; do

# Check if socat is running for Aeotec Z-Stick Gen 5
if pgrep -f 'zsticksocatserver' >/dev/null 2>&1; then
echo "Z-Stick socat server is running"
else
pkill -f 'zsticksocatserver'
echo "starting zstick socat server..."
/usr/bin/screen -S zsticksocatserver -d -m socat -ls -s -d -d FILE:/dev/ttyACM0,b115200,raw,echo=0 TCP-LISTEN:1235,reuseaddr,reuseport
fi

sleep 1
done
On USB client

1) apt-get install socat screen

2) contents of /etc/init.d/usbclient

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides: usbclient
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 5
# Default-Stop:
# Description:
### END INIT INFO

case "$1" in
'start')
        /usr/bin/screen -S socatclientmonitor -d -m /scripts/socatclientmonitor.sh
        ;;
'stop')
        pkill -f 'socatclientmonitor'
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac
exit 0
3) contents of /scripts/socatclientmonitor.sh

Code: Select all

#!/bin/bash
while true; do

# Check if socat client is running for Aeotec Z-Stick Gen 5
if [ -e /dev/ttyACM0 ]; then
echo "Z-Stick socat is connected"
else
echo "Z-Stick socat not connected, establishing connection..."
socat pty,link=/dev/ttyACM0,b115200,raw tcp:192.168.0.21:1235&
fi

sleep 1
done
The advantage of this setup as opposed to USBIP is that it does not require the loading of a kernel module on the client which makes it easy to use in unprivileged LXC containers.

It's also possible to share RFXCOM (rfxtrx433e) via socat as well (and likely a lot of other USB serial devices).
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest