Sonntag, 9. Juni 2013

Raspberry Pi + PiFace + FHEM with Homematic Homeautomation



Wiring of the PiFace:

Interface i got for the PiFace:

I wired each button to the PiFace inputs and the LED's to the outputs.
The display is not in use for now, but maybe in the future.

The OS i installed is Soft-float Debian “wheezy” because i wanted the option to try openhab.

For FHEM installation:

download the deb-file from http://fhem.de/fhem.html and install it with
dpkg -i fhem-5.4.deb
After that i needed a way to get the relais to switch, so i searched for it and found wiringpi which was a straight forward installation as described on the site.

So i was able to trigger the relais with:
gpio -p write 200 1   # first relais "on"
gpio -p write 201 0   # second relais "off"

as i got this running i wrote a script to turn on my NAS:
#!/bin/bash
LOCKDIR="/tmp/nas-on-lock"
PIDFILE="${LOCKDIR}/PID"

RESULT="64"
PING=$(ping <NAS-IP-Address> -c 1 | grep 64 | awk '{print $1}')
if [ "$RESULT" != "$PING" ]
then
  if mkdir "${LOCKDIR}" &>/dev/null; then
     /usr/local/bin/gpio -p write 201 1
     sleep 1
     /usr/local/bin/gpio -p write 201 0
     sleep 60
     rm -r "${LOCKDIR}"
  fi
else
  exit 0
fi
exit 0
and a small script to turn it off also:

#!/bin/bash
ssh root@<NAS-IP-Address> 'shutdown -h now'

for the shutdown to work you need to enable ssh-key authentication without password.


Next step was to configure fhem with adding following to the /opt/fhem/fhem.cfg file:

define NAS FS20 11111111 02
attr NAS dummy 1
attr NAS room Vorraum
define act_on_NAS notify NAS {\
 if ("%" ne "off") {\
  system("/opt/fhem/scripts/start-nas.sh &")\
 } else {\
  system("/opt/fhem/scripts/stop-nas.sh &")\
 }\
}
define NASping PRESENCE lan-ping <NAS-IP-Address>
define watchdog_NASpingoff watchdog NASping:absent 00:00:01 SAME setstate NAS off;; trigger watchdog_NASpingoff .
define watchdog_NASpingon watchdog NASping:present 00:00:01 SAME setstate NAS on;; trigger watchdog_NASpingon .

At this point i was able to turn on and off my NAS through FHEM.

Next was to configure my HM-CFG-LAN. I ran into a problem to find the device with the configuration utility "HomeMatic-Lan-Interface configuration" which i solved with connecting the HM-CFG-LAN direct to my Notebook and deactivated AES so it works with FHEM.
2 lines to the fhem.cfg and the LAN-Adapter was seen:
define HMLAN1 HMLAN <HM-CFG-LAN-IP>:1000
attr HMLAN1 hmId 8D0C2D
Pairing the 2 switches i got was pretty easy with as described in: http://www.fhemwiki.de/wiki/HomeMatic_Devices_pairen

So i was able to trigger the switches through FHEM, and configured one light to turn itself off if it is on for 3 minutes:
define CUL_HM_HM_LC_Sw1_an notify CUL_HM_HM_LC_Sw1:on* define CUL_HM_HM_LC_Sw1_aus at +00:03:00 set CUL_HM_HM_LC_Sw1 off
Next thing i wanted is to have a "turn all off switch"
So in stalled https://github.com/piface/pifacedigitalio  and changed the tests.py because thats my first contact with python.:
import pifacecommon
import pifacedigitalio
import os
pifacedigitalio.init()
pfd = pifacedigitalio.PiFaceDigital()
# create two functions
def turn_off_all(interrupt_bit, input_byte):
    cmd = 'echo "turn all things off'
    os.system(cmd)
    cmd2 = '/opt/fhem/fhem.pl localhost:7072 "set things off"'
    os.system(cmd2)
    return True  # keep waiting for interrupts

ifm = pifacecommon.InputFunctionMap()
# when switch 0 (input0) is pressed, run toggle_led0
ifm.register(
    input_num=0,
    direction=pifacecommon.IN_EVENT_DIR_ON,
    callback=turn_off_all,
    board_num=0)  # optional
pifacedigitalio.wait_for_input(ifm)  

this script has to run in background as a daemon, so i got following script in /etc/init.d/buttons:
#!/bin/bash
DAEMON=/usr/bin/python3
ARGS="/usr/local/bin/button-interrupt.py"
PIDFILE=/var/run/button.pid
case "$1" in
  start)
    echo "Starting buttons"
    /sbin/start-stop-daemon --start --pidfile $PIDFILE \
        --user fhem --group fhem \
        -b --make-pidfile \
        --chuid fhem \
        --exec $DAEMON $ARGS
    ;;
  stop)
    echo "Stopping server"
    /sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose
    ;;
  *)
    echo "Usage: /etc/init.d/daemon {start|stop}"
    exit 1
    ;;
esac
exit 0
Things were set to work, now the last thing was to set up a structure in FHEM to turn off all lights at once
 define things structure things CUL_HM_HM_LC_Sw1

433MHz 

Now,  the Hardware for the 433MHz sender and receiver:
I did the wiring as described here: http://ninjablocks.com/blogs/how-to/7506204-adding-433-to-your-raspberry-pi 
 The software is really easy:
Follow the installation steps on: https://github.com/ninjablocks/433Utils and it will work smoothly. But please be aware that the pins you use are hardcoded in the binary, so you have to change it before compiling.
After compiling the 433Utils you can read the code which is sent from your RF remote with "RFsniffer"
With "codesend xxxxxx " you can resend the code, and turn off/on your radio plug.

Here what i did in FHEM:

define Stablichter FS20 11111111 04
attr Stablichter dummy 1
attr Stablichter lights lights
attr Stablichter room Wohnzimmer
attr Stablichter things things
define act_on_Stablichter notify Stablichter {\
if ("%" eq "toggle") {\
 if (OldValue("Stablichter") eq "off") {\
   system("/opt/fhem/scripts/send-rf.sh 4523285 &");;\
   fhem("set Stablichter on")}\
   else {\
  system("/opt/fhem/scripts/send-rf.sh 4523284 &");;\
  fhem("set Stablichter off")}\
}\
if  ("%" eq "on") {\
 system("/opt/fhem/scripts/send-rf.sh 4523285 &")\
} elsif  ("%" eq "off") {\
 system("/opt/fhem/scripts/send-rf.sh 4523284 &")\
}\
}


If there are questions, just ask, i will reply as soon as possible.