Difference between revisions of "Controling Homeautomation devices using wifi presence"

From BubbaWiki
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
This guide is not finished yet. I'm working on it&nbsp;:-)<br>  
This guide is quite untested. I'd be happy to get some input on it in the forum at [http://forum.excito.net/viewforum.php?f=16 forum.excito.net/viewforum.php]<br>
 
This is a guide describing how to set up a device in Homeautomation that is automatically turned on or off based on whether a certain MAC&nbsp;address is connected to B3's wifi or not. This could then be used in the Homeautomation scheduling to turn other devices on or off based on this information, for example to lit up your home automatically when coming into wifi range.<br>
 
First you will have to set up a "dummy" device for each wifi unit. Do this by going into the device list in Homeautomations settings and make a new RAW device there. The only thing you need to put in is a description for the device (and optionally icons too). Remember the ID's of the new dummy devices (you can see the IDs in the device list).
 
Now, copy the text below into /usr/local/bin/hostapd_ha.sh.<br>  
<pre>#!/bin/bash
<pre>#!/bin/bash


Line 18: Line 24:


date &gt;&gt; /var/log/hostapd.log
date &gt;&gt; /var/log/hostapd.log
connectionState=0
if [ $2 = $connected ]; then
        connectionState=1
        echo "$3 Connected!" &gt;&gt; /var/log/hostapd.log
else if [ $2 = $disconnected ]; then
        echo "$3 Disconnected" &gt;&gt; /var/log/hostapd.log
    else
        echo "What happened now?" &gt;&gt; /var/log/hostapd.log
    fi
fi


for (( i=0; i&lt;=$(( $countHomeDevices -1 )); i++ )); do
for (( i=0; i&lt;=$(( $countHomeDevices -1 )); i++ )); do
  if [ $3 = "${homeDevices[$i]}" ]; then
    $(wget -O /dev/null -q ""${HomeAutomationUrl}"ajaxinterface.php?do=toggleStatus&amp;status="${connectionState}"&amp;deviceid="${HomeAutomationDeviceIds[$i]}"" )
  fi
done</pre>
Make the file executable using "chmod 755 /usr/local/bin/hostapd_ha.sh". Edit the HomeAutomationUrl, homeDevices and HomeAutomationDeviceIds to your needs.<br>
Included in the hostapd package (the program which turns B3 into an access point) there is a utility called hostapd_cli that is able to trigger events when devices are connected and disconnected. What we want is to start hostapd_cli as a daemon and tell it to run our script above upon connections/disconnections. To to this, copy the text below into /etc/init.d/hostapd_cli:<br>
<pre>#!/bin/sh
### BEGIN INIT INFO
# Provides: hostapd_cli
# Required-Start: $remote_fs ifup-br0 hostapd
# Required-Stop: $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Utility to trigger events based on connects/disconnects
# Description:
### END INIT INFO


  if [ $3 = "${homeDevices[$i]}" ]; then
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd_cli
NAME=hostapd_cli
DESC="hostapd cli"
PIDFILE=/var/run/hostapd_cli.pid


    connectionState=0
[ -x "$DAEMON_SBIN" ] || exit 0


    if [ $2 = $connected ]; then
DAEMON_OPTS="-B -a/usr/local/bin/hostapd_ha.sh"
      connectionState=1
      echo "$3 Connected!" &gt;&gt; /var/log/hostapd.log
    else if [ $2 = $disconnected ]; then
        echo "$3 Disconnected" &gt;&gt; /var/log/hostapd.log
      else
        echo "What happened now?" &gt;&gt; /var/log/hostapd.log
      fi
    fi


    $(wget -O /dev/null -q ""${HomeAutomationUrl}"ajaxinterface.php?do=toggleStatus&amp;status="${connectionState}"&amp;deviceid="${HomeAutomationDeviceIds[$i]}"" )
. /lib/lsb/init-functions
  fi


done
case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
      -- $DAEMON_OPTS &gt;/dev/null
    log_end_msg "$?"
  &nbsp;;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN"
    log_end_msg "$?"
  &nbsp;;;
  restart|force-reload)
    $0 stop
    sleep 8
    $0 start
  &nbsp;;;
  status)
    status_of_proc "$DAEMON_SBIN" "$NAME"
    exit $?
  &nbsp;;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload|status}" &gt;&amp;2
    exit 1
  &nbsp;;;
esac


exit 0


</pre>
</pre>
Make the file executable using "chmod 755 /etc/init.d/hostapd_cli". Now configure this daemon to start automatically when booting using "update-rc.d hostapd_cli defaults". Also start the daemon now by simply running "/etc/init.d/hostapd_cli start".<br>

Latest revision as of 11:52, 2 July 2012

This guide is quite untested. I'd be happy to get some input on it in the forum at forum.excito.net/viewforum.php

This is a guide describing how to set up a device in Homeautomation that is automatically turned on or off based on whether a certain MAC address is connected to B3's wifi or not. This could then be used in the Homeautomation scheduling to turn other devices on or off based on this information, for example to lit up your home automatically when coming into wifi range.

First you will have to set up a "dummy" device for each wifi unit. Do this by going into the device list in Homeautomations settings and make a new RAW device there. The only thing you need to put in is a description for the device (and optionally icons too). Remember the ID's of the new dummy devices (you can see the IDs in the device list).

Now, copy the text below into /usr/local/bin/hostapd_ha.sh.

#!/bin/bash

HomeAutomationUrl="http://192.168.168.1/homeautomation/"

# Devices that affects HomeAutomation
homeDevices[0]="84:00:d2:e4:3b:9f" #Xperia Ray
homeDevices[1]="d0:df:c7:fd:8d:ea" #Galaxy Tab

# HomeAutomation device ids
HomeAutomationDeviceIds[0]="20" #Xperia Ray
HomeAutomationDeviceIds[1]="21" #Galaxy Tab

connected="AP-STA-CONNECTED"
disconnected="AP-STA-DISCONNECTED"

countHomeDevices=${#homeDevices[*]}

date >> /var/log/hostapd.log

connectionState=0
if [ $2 = $connected ]; then
        connectionState=1
        echo "$3 Connected!" >> /var/log/hostapd.log
else if [ $2 = $disconnected ]; then
        echo "$3 Disconnected" >> /var/log/hostapd.log
     else
        echo "What happened now?" >> /var/log/hostapd.log
     fi
fi

for (( i=0; i<=$(( $countHomeDevices -1 )); i++ )); do
  if [ $3 = "${homeDevices[$i]}" ]; then
    $(wget -O /dev/null -q ""${HomeAutomationUrl}"ajaxinterface.php?do=toggleStatus&status="${connectionState}"&deviceid="${HomeAutomationDeviceIds[$i]}"" )
  fi
done

Make the file executable using "chmod 755 /usr/local/bin/hostapd_ha.sh". Edit the HomeAutomationUrl, homeDevices and HomeAutomationDeviceIds to your needs.

Included in the hostapd package (the program which turns B3 into an access point) there is a utility called hostapd_cli that is able to trigger events when devices are connected and disconnected. What we want is to start hostapd_cli as a daemon and tell it to run our script above upon connections/disconnections. To to this, copy the text below into /etc/init.d/hostapd_cli:

#!/bin/sh

### BEGIN INIT INFO
# Provides: hostapd_cli
# Required-Start: $remote_fs ifup-br0 hostapd
# Required-Stop: $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Utility to trigger events based on connects/disconnects
# Description:
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON_SBIN=/usr/sbin/hostapd_cli
NAME=hostapd_cli
DESC="hostapd cli"
PIDFILE=/var/run/hostapd_cli.pid

[ -x "$DAEMON_SBIN" ] || exit 0

DAEMON_OPTS="-B -a/usr/local/bin/hostapd_ha.sh"

. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
    start-stop-daemon --start --oknodo --quiet --exec "$DAEMON_SBIN" \
      -- $DAEMON_OPTS >/dev/null
    log_end_msg "$?"
    ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    start-stop-daemon --stop --oknodo --quiet --exec "$DAEMON_SBIN"
    log_end_msg "$?"
    ;;
  restart|force-reload)
    $0 stop
    sleep 8
    $0 start
    ;;
  status)
    status_of_proc "$DAEMON_SBIN" "$NAME"
    exit $?
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

Make the file executable using "chmod 755 /etc/init.d/hostapd_cli". Now configure this daemon to start automatically when booting using "update-rc.d hostapd_cli defaults". Also start the daemon now by simply running "/etc/init.d/hostapd_cli start".