Some friends asking me about my Homebred setup for my home automation – It’s been a while since I promise to write about it – so here you go. (will open the code as soon as I can make it readable for public and clean it up from my custom setups)
The setup is simple, nothing fancy.
It’s using tricky simple presence detection – a.k.a detecting MAC address of my phone and trigger scripts based on that. The weakness of it that it will need DDWRT
router use other router that list connected clients MAC address, but you will need to adjust the scrapper code to login and scrap your router interface)
Presence Detection
Install DDWRT on your router. Make sure the setting to expose info
page
After this is enabled. It will expose Info.htm
that you can use to scrap for information about your router. See the Wireless clients table on the bottom of the Info.htm
page
Create daemon that check DDWRT status page on some interval and check for the existence of specific MACs. If some specific MACs is detected, go to a folder and run all scripts.
The Presence Script
The daemon itself is a Nodejs script that run on my Raspberry Pi. It run using cron on some certain interval.
I use Nodejs script for easy development effort :). For request Nodejs have Request and for scrapping it have Cheerio that makes all scrapping needs as easy as creating jQuery selector. For the DDWRT info page.
var request = require('request');
var cheerio = require('cheerio');
request('http://10.0.1.1/Info.htm', function (error, response, body) {
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
var clients = $('#wireless_table tr');
// loop for clients - do the necessary MAC detection here. Don't forget to save it to external flag file so you can create event
// `onEnter`, `onExit` etc
// I save list of MACs that need to be checked in a simple JSON file
}
});
When the above script detect an event, it will try to list all executable script in a folder – For example I have /opt/MYMACADDRESS/enter.d/
and /opt/MYMACADRESS/exit.d/
so when my MAC is detected as entering, it will executes all executable scripts in /opt/MYMACADRESS/enter.d/*
In Nodejs, it’s as easy as
// loop for scripts and execute it using the code below
child = exec(scriptFileName, function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
So I can create many scripts using shebang
notation in the folder and those will be executed accordingly.
Scripts
The scripts are fine tuned to my home setup. But it can be anything. Here’s some Ideas
- Set
transmission
torrent client to unlimited/usr/bin/transmission-remote -D && /usr/bin/transmission-remote -U
(when I am at work) - Set
transmission
to 0kb/sec/usr/bin/transmission-remote -d 0 && /usr/bin/transmission-remote -u 0
(when I am at home) - Bash script that update Dynamic DNS provider with my new IP – It used to twit my public IP to a protected twitter account 🙂
- Twit to the protected twitter account if some specific MACs is entering my wifi network
- Turn on Air Conditioner by accessing IR arduino trough raspberry GPIO
- Wake On Lan my gaming PC by utility called powerwake
powerwake 10.0.1.100
Non DDWRT Router
Well the idea can be applied to other router interface. Most router use basic authentication
to access the internal web application. In Nodejs it’s as easy as (or use basic auth bearer Token)
request.get('http://192.168.1.1/').auth('username', 'password', false);
And then tweak your scrapper accordingly to harvest for MAC address or static IPs.
This is very interesting, Thank you for sharing your article. I really appreciate your efforts and I will be waiting for your further post thanks once again.
good idea, thanks information