I wanted a way to detect if someone was in the house without anyone having to do anything. This is useful, for example, to run a script or launch a program if we can identify who it is. The solution I wanted to play with was using mobile phones. Given that folks are fairly attached to their phones these days this seemed a reasonable detection method.
The script below is written in Python and uses bluetooth to detect if anyone is in the house. It goes a bit further than that and will play an MP3 depending on who it detects. The MAC of each bluetooth device needs to be specified. There’s no need for the devices to be discoverable so is ideal. Of course the mobile phone must have bluetooth enabled for this to work. Oh yeah – and a BT USB dongle of course.
The ‘bluez’ libraries are required, so in Debian/Ubuntu you need to ‘apt-get install python-bluez’. It is useful to have the base ‘bluez’ package also installed.
To make sure the OS can see your bluetooth adapter, run ‘hcitool scan’ – this should show your BT adapter’s MAC address.
It will not play (or change user state) if another user’s MP3 file is currently playing. This can be tweaked to be more useful, such as activating a PIR sensor if no device is detected.
You’ll probably notice that there are multiple users in this example but no ‘for’ loop – it’s all work in progress 🙂
#!/usr/bin/python import bluetooth import time import pygame pygame.mixer.init() pygame.mixer.music.set_volume(0.3) user1 = 0 user2 = 0 while True: user1_was = user1 user2_was = user2 result = bluetooth.lookup_name('D8:90:xx:xx:xx:xx’, timeout=10) if (result != None): user1 = 1 else: user1 = 0 if (user1 != user1_was and user1 == 1 and pygame.mixer.music.get_busy() == False): print "Welcome user1" pygame.mixer.music.load("sounds/user1.mp3") pygame.mixer.music.play() result = bluetooth.lookup_name('00:EE:xx:xx:xx:xx’, timeout=10) if (result != None): user2 = 1 else: user2 = 0 if (user2 != user2_was and user2 == 1 and pygame.mixer.music.get_busy() == False): print "Welcome user2" pygame.mixer.music.load("sounds/user2.mp3") pygame.mixer.music.play() time.sleep(30)