Saturday 15 April 2017

openHAB BlueTooth presence detection (Raspberry Pi)

Assuming you run openHAB on a Linux device,
there is a simple way to have any mobile or other BT device detected.

bluetooth-dongle

First, of course, you need a bluetooth device!
Any cheap usb dongle should work fine.
I use an old Bluetooth 2.0 with no issues. I didn't have to install any drivers on Raspberry.

Then you need to install 2 packages:
bluez & python-gobject
like this:

sudo apt-get install bluez
sudo apt-get install python-gobject

Now, in the folder configurations/scripts (or any other) create script file e.g bt_scan.sh containing:

#!/bin/bash

    DEVICES="$(hcitool name XX:XX:XX:XX:XX:XX)"

    if [[ $DEVICES = "Mi Phone" ]]
    then
      mosquitto_pub -t /home/bt/boss -m "OPEN"
    else
      mosquitto_pub -t /home/bt/boss -m "CLOSED"
    fi


replace XX:XX:XX:XX:XX:XX with your mobile's BT mac address and Mi Phone with your mobile's BT Name.
What this script does, is it runs the command
hcitool name XX:XX:XX:XX:XX:XX
which will return your mobile's BT Name, if it is detected. If it is not it will return nothing.
In my case the name it returns is Mi Phone.
If the phone is detected, the script will publish the string OPEN to the MQTT Broker /home/bt/boss
With this mosquitto broker and openHAB you can do anything you like, turn on lights, deactivate alarms, etc..

Finally, add a line in your cron jobs to get this script run every minute or so..
In the file /etc/crontab add the line

*  *    * * *   root    /etc/openhab/configurations/scripts/bt_scan.sh

And you are done.


Using this approach, you don't actually need openHAB.
You can have the linux box running the mosquitto and the ESP8266s controlling the lights, alarms, etc..

No comments:

Post a Comment