Controling Homeautomation devices using wifi presence
This guide is not finished yet. I'm working on it :-)
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.
First, 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 for (( i=0; i<=$(( $countHomeDevices -1 )); i++ )); do if [ $3 = "${homeDevices[$i]}" ]; then 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 $(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. You find the device ids in the device config page in Homeautomation.
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
Now create a link to start this daemon automatically using "ln -s /etc/init.d/hostapd_cli /etc/rc2.d/S20hostapd_cli". (I don't think this is the correct way to do it, I'll check up later the nice way). Also start the daemon by simply running "/etc/init.d/hostapd_cli start".