Z wave - can openhab control a remote (Rasp Pi) z wave stick

zwave
aeonzwavestick
Tags: #<Tag:0x00007f18259b7370> #<Tag:0x00007f18259b7208>

(Psyciknz) #1

My openhab is on a virtualised Linux server. I currently also have a raspberry Pi with a dht11 sensor and two USB connected ups.

Found I connect a z wave controller to the rasp Pi and send commands to it remotely?


Corrupt FileSystems every 2-3 month?
(Jim) #2

I do exactly this. My zwave controller is connected to a Raspberry Pi in my livingroom, while OH runs on a in a VM. I have the zstick’s serial port shared with ser2net. There is a udev rule that names my zwave controller /dev/zstick.

ser2net.conf:

7000:raw:0:/dev/zstick:115200 8DATABITS NONE 1STOPBIT

Originally OH ran in a Linux VM. I used socat to connect the port shared with ser2net to a pty:

/usr/bin/socat PTY,link=/dev/zwave,group=dialout,mode=660 TCP:192.168.55.33:7000

This worked fairly well. The biggest problem I had is that if communication is interrupted between the VM and the pi socat dies and I have to restart both it and OH. There may be a way around this, but I ended up moving OH to a Windows VM and using Netburner Virtual Comm Port instead. This wasn’t my ideal solution, but it’s been much more reliable (I can unplug my zwave controller from the pi, pair a device, then plug the controller back in and the zwave will still function without having to touch a thing).


Share Z-wave dongle over IP (USB over IP using ser2net / socat ) guide
(Curlyel) #3

I’m using a “remote” Zwave controller based on ser2net+socat as well.

To mitigate the impacts of interrupts in the connection to the remote pi or to the zwave controller itself (unplugged stick), I’m using this little script to restart the socat. After starting socat it checks, if the tcp port were socat was bound to gets established. If not, the script restarts the ZWave binding via Karaf console:

#!/bin/bash
LOCALPORT=/rdev/ttyUSB1
REMOTEPORT=192.1.1.253:10002
BINDING="ZWave Binding"

while sleep 10 # infinite loop for checking socat connected to REMOTEPORT is still running
do
        if [[ $(ps ax | grep -v grep | grep tcp:$REMOTEPORT | wc -l) -eq 0 ]]
        then
                # restarting socat
                /usr/bin/socat pty,link=$LOCALPORT,echo=0,raw,waitslave,group=dialout,mode=660 tcp:$REMOTEPORT &
        fi

        if [[ $(netstat -an | grep $REMOTEPORT | awk '{print $6}' ) != "ESTABLISHED" ]]
        then
                # tcp is not established to REMOTEPORT, therefore restarting BINDING bundle
                /usr/bin/ssh openhab@localhost -p 8101 bundle:restart `ssh openhab@localhost -p 8101 bundle:list | grep "$BINDING" | cut -c1-3`
                sleep 120
        fi
done

Despite not beeing perfect, it’s working for me.

But I’m wondering, if @chris can implement some reset of the serial port connection once the communication to the zwave controller gets broken? So a restart of the whole bundle would not longer be neccessary.

Btw.: Just noticed, the RFXCom binding is capable of connecting to a ser2net remote port without the need for local socat. Probably ZWave binding could do it similar. It’s just an idea to further improve this yet fantastic binding :wink:


(Curlyel) #4

Just for the record:

I’ve slightly ammended the script to avoid some unneccesary bundle restarts:

#!/bin/bash
LOCALPORT=/rdev/ttyUSB1
REMOTEPORT=192.1.1.253:10002
BINDING="ZWave Binding"

while sleep 10 # infinite loop for checking socat connected to REMOTEPORT is still running
do
        if [[ $(ps ax | grep -v grep | grep tcp:$REMOTEPORT | wc -l) -eq 0 ]]
        then
                # restarting socat
                /usr/bin/socat pty,link=$LOCALPORT,echo=0,raw,waitslave,group=dialout,mode=660 tcp:$REMOTEPORT &

                while [[  $(ps ax | grep -v grep | grep tcp:$REMOTEPORT | wc -l) -gt 0 && $(netstat -an | grep $REMOTEPORT | awk '{print $6}' ) != "ESTABLISHED" ]]
                
                    do
                        # tcp is not established to REMOTEPORT, therefore restarting BINDING bundle
                        /usr/bin/ssh openhab@localhost -p 8101 bundle:restart `ssh openhab@localhost -p 8101 bundle:list | grep "$BINDING" | cut -c1-3`
                        sleep 30
                    done
            fi
done


(Psyciknz) #5

I ended up going this method: Dueling Openhabs (feeding from a 2nd with zwave)

Using the MQTT Event bus with a 2nd OH2 system to drive a main system. Seems to work well for the sensor I have connected. I’ll investigate further is control back the other way works when my relay is installed this weekend.


(Matt) #6

hi @curlyel,
i run into a similar problem when testing ser2net and my remote PI3 got restarted by some reason.
may i ask where/how you integrated your script to be run?

Matt


(Matt) #7

all working now, just add it to rc.local :roll_eyes:
just wondering which would be the best approach if i have more than one remote Z-Wave PI’s?

for each a separate script running or to see to get all in one script?


(2devnull) #8

What is the “remote” platform OS used for this on the Pi? I have a Sigma USB stick, is this the setup for that?


(Matt) #9

@2devnull i use the latest raspbian for this. and yes you can use it for that USB stick.
i tested my setup with a USB from zwave.me and Aeon USB stick and works perfectly with both of them.


(2devnull) #10

I was able to get this all working also with a BeagleBone Black.

  • ser2net on the BBB
  • socat on OH2 with systemd to reconnect if BBB is dis/reconnected. No need for looping scripts.

(Matt) #11

@curlyel i tried to amend your script with one more check, if the USB dongle is actually re-connected to the remote PI.
However it seems that i get never out of the while loop since the Zwave Binding is never restarting.

any idea what the problem could be? the SSH command which i added is working if i execute it by its own.

#!/bin/bash
LOCALPORT=/dev/ttyZWAVE1
REMOTEPORT=192.168.1.178:3333
BINDING="ZWave Binding"

while sleep 10 # infinite loop for checking socat connected to REMOTEPORT is still running
do
        if [[ $(ps ax | grep -v grep | grep tcp:$REMOTEPORT | wc -l) -eq 0 ]]
        then
                # restarting socat
                /usr/bin/socat pty,link=$LOCALPORT,echo=0,raw,waitslave,group=dialout,mode=660 tcp:$REMOTEPORT &

                while [[  $(ps ax | grep -v grep | grep tcp:$REMOTEPORT | wc -l) -gt 0 && $(netstat -an | grep $REMOTEPORT | awk '{print $6}' ) != "ESTABLISHED" && $(ssh openhab@192.168.1.178 ls /dev/ttyZWAVE0 2>/dev/null | wc -l) -gt 0 ]]
            
                do
                    # tcp is not established to REMOTEPORT, therefore restarting BINDING bundle
                    /usr/bin/ssh openhab@localhost -p 8101 bundle:restart `ssh openhab@localhost -p 8101 bundle:list | grep "$BINDING" | cut -c1-3`
                    sleep 30
                done
        fi
done

(Curlyel) #12

First, I have to admit, that I’ve recently commented out the automatic bundle restart part due to some issue: openHAB (very likely the JVM it runs in) is crashing on some bundle reloads. Most suspect is currently the RFXCom binding, but to be on the safe side, I disabled the automatic restart for all my rf-bindings (ZWave, ZigBee, RFXCom)…

Anyway, the automatic restart portion:

do
                    # tcp is not established to REMOTEPORT, therefore restarting BINDING bundle
                    /usr/bin/ssh openhab@localhost -p 8101 bundle:restart `ssh openhab@localhost -p 8101 bundle:list | grep "$BINDING" | cut -c1-3`
                    sleep 30
                done

…relies on key based authentication to Karaf console, to omit the input of any credentials. I guess, you’re already aware of it :wink:
If not, this something to be solved first…


(Matt) #13

@curlyel
actually your script is working perfectly fine for me as soon as i remove my added part from your script in the while loop.
thats why i thought i may have a syntax issue which i can’t see right now, maybe lack of coffee/sleep

&& $(ssh openhab@192.168.1.178 ls /dev/ttyZWAVE0 2>/dev/null | wc -l) -gt 0

for all SSH sessions i have the corresponding certificates in place for an password less authentication, so no issues there.

what i tried to archive was that the zwave binding gets only restarted if the Zwave usb was already reconnected.