Controlling the mains with bluetooth proximity

I’ve found another use for my parallel port controlled socket box; by using it in conjunction with a bluetooth dongle, I can detect when I have wandered out of the room. I can turn off a load of peripherals, then turn them all back on when I return to the room seamlessly. I’ve simply plugged the extension leads which power my monitors, speakers, kettle, lamps and fan into the parallel controlled socket then let a ruby script act as the brains of the operation.

In order to detect only my phone I had to find out the address. To do this I turned my bluetooth to discoverable and used hcitool:

$ hcitool scan
Scanning ...
	00:26:5F:C6:30:A3	Mike

This command lists all discoverable bluetooth devices in range; as you can see, only my phone is in range and discoverable. Once I had this address, I could set my bluetooth back to hidden. Next I wrote a script that to repeatedly check for the presence of my phone. This time it searches specifically for the address, this means that it is able to detect it when it is set to hidden. The script is slightly more complex than simply checking for the presence of the device. It double checks that the device is really gone; this was discovered to be necessary during testing, as occasionally the device is not detected even when it is present which can lead the power flickering off then on again. There is also a line to turn everything back on if the process is terminated, this is especially helpful for being able to see what’s happened if anything goes wrong.

bluetooth_detect.rb

#You’ll have to look this up manually by using the hcitool command
BLUETOOTH_ADDRESS='00:26:5F:C6:30:A3'

#Get this from http://www.mike-worth.com/2012/02/24/controlling-the-mains-via-the-parallel-port/
PARALLEL_CONTROL='/home/mike/parallel_control/parallel_control'
ADDRESS_BIT=1

#If the process is terminated turn everything back on
Kernel.at_exit{system(PARALLEL_CONTROL+' '+ADDRESS_BIT.to_s+' 1 > /dev/null')}

def isPresent(address)
  if (`hcitool name #{address}`=='')
    return false
  else
    return true
  end
end

while true
  if isPresent(BLUETOOTH_ADDRESS)
    system(PARALLEL_CONTROL+' '+ADDRESS_BIT.to_s+' 1 > /dev/null')
    sleep 5
  else
    #double check we didn't just momentarily loose the connection
    if !isPresent(BLUETOOTH_ADDRESS)
      system(PARALLEL_CONTROL+' '+ADDRESS_BIT.to_s+' 0 > /dev/null')
    end
  end
end

10 thoughts on “Controlling the mains with bluetooth proximity

  1. Pingback: Controlling the power with bluetooth - Hack a Day

  2. Pingback: Controlling the power with bluetooth « Hackaday « Cool Internet Projects

  3. Pingback: Electronics and Similar What Not | Pearltrees

  4. Pingback: Use Bluetooth To Automatically Turn Off Your Electronics When You Leave The Room | Lifehacker Australia

    1. Mike Post author

      I’m currently having the opposite range problem – my speakers cut out if I am in the next room doing uni work. I strongly suspect this is down to the thick stone walls in the part of the building I live in.

      I’ve had a look at the man page for hcitool and it should be possible to set up a connection then keep checking the signal strength, turning things off if it drops low enough

      Reply
  5. Pingback: Technology News & Updates » Too Lazy to Turn Off Your Electronics ? Bluetooth will help!

  6. Pingback: Automatically Turn Off Your Electronics When You Leave The Room via Bluetooth | Rangify – Tips and Tutorials Related To Technology, iPhone, iPad and Everything In Between

  7. Pingback: Improving the aerial on a mini bluetooth dongle | Products of Mike's Mind

Leave a Reply