Presence alert in OpenHAB by DHCP server in ASUS router

The home automation system OpenHAB has the ability to coordinate difference networked devices to perform automatic control under different conditions. One of the important conditions is the presence of different residents in the house. Using "Network Health" module of OpenHAB allows the presence test through the detection of devices carried by the residents. One simply create item such as

Switch Resident1 {nh="android-xxxxxx"}
Switch Resident2 {nh="android-yyyyyy"}
...

This works well except the time resolution for the arrival and departure is limited by the period of network checking (which is set by networkhealth:refresh and defaulted to 1 minute). For real-time arrival notification, it can be achieved by the DHCP server of the local area network. We can configure it to flip on the corresponding presence switch as soon as a DHCP lease is offered to the corresponding device. For ISC DHCP server running on a computer, this can be done using the "on commit" event handling block. For ASUS router, which runs dnsmasq for dhcpd, we will need to update to the Asuswrt-Merlin firmware to enable similar facility. Once that's done and you can login to the router for a shell, you make an addition to the "dnsmasq.conf" by creating the file, "/jffs/configs/dnsmasq.conf.add", with the single-line content:

dhcp-script=/jffs/machab.sh

This will make dnsmasq call the script, "/jffs/machab.sh", whenever a lease is offered. The content of the file is as follows:

#!/bin/sh
HABURL='URL for the OpenHAB server with authentication credential'
MAPDAT=${0%/*}/machab.map
mac=$2
item=''
if [ ! -r "${MAPDAT}" ]; then exit 1; fi
while read m n; do
	if [ "$mac" = "$m" ]; then item=${n}; fi
done < ${MAPDAT}
if [ -z "$item" ]; then exit 0; fi
wget -O /dev/null --post-data=ON --no-check-certificate \
 --header "Content-Type: text/plain" ${HABURL}/rest/items/$item

(Remember to change the HABURL setting to suit your situation.)
The script look up the MAC address of the device from the map file, "/jffs/machab.map", which contains something like:

xx:xx:xx:xx:xx:xx	Resident1
yy:yy:yy:yy:yy:yy	Resident2
...

and flip the corresponding switch to "ON".
Finally, enable the "JFFS custom scripts and configs" from the administration web page of the router as shown below.

You might need to reboot the router for the configuration to take effect.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.