Using a bluetooth USB dongle for a presence detection system to activate a relay by using a GPIO (or doing anything else) , when a bluetooth device is detected, for example your phone

Note: the script simply scans for any bluetooth device in range, so there is no need of pairing or trusting the device you want to detect.

prerequisites:

  opkg install kmod-bluetooth bluez-libs bluez-utils kmod-usb-core kmod-usb-uhci kmod-usb2 usbutils

start bluetooth:

  /etc/init.d/dbus start
  /etc/init.d/bluetoothd start
  /usr/bin/hciconfig hci0 up
  

Note: to enable bluetooth at boot put the above three lines in the “/etc/rc.local” file

To scan for remote devices and to discover your device MAC Address, put your device in “discoverable mode” and use the following command:

  hcitool scan

in my case the desired MAC Address is 30:21:15:13:9C:29

Note: the device should remains in “discoverable mode”, if you do not want to let your device discoverable, you need to pair it and trust it.

to pair and trust your device do the following:

   bluetoothctl pair 30:21:15:13:9C:29
   bluetoothctl trust 30:21:15:13:9C:29
   

to check if the device has been paired:

  bluetoothctl paired-devices

example:

  root@OpenWrt:~# bluetoothctl paired-devices
  Device 38:2D:E8:49:C1:87 Galaxy J5
  Device 30:21:15:13:9C:29 BT-888
  

the script: nano /root/check_presence_bt.sh

  #!/bin/sh
  # Bluetooth Presence Detection
  # By Lovisolo P.M. - parknat12@yahoo.com
  # my OpenWRT, Raspberry and Linux, personal forum: http://forum.49v.com
  #
  while :
  do
  # scan for bluetooth devices and put the result into a variable:
  c=$(/usr/bin/hcitool scan)
  #
  # check for a specific MAC ADDRESS presence:
  if [ `echo $c | grep -c "30:21:15:13:9C:29" ` -gt 0 ]
  then
  /bin/echo 'Device detected, relay is ON'
  /bin/echo 1 > /sys/class/gpio/gpio1/value # you may need to change this depending on your router
  # you may add here a command or a script to be executed when the device is available
  else
  /bin/echo 'Device is off or out of range, relay is OFF'
  /bin/echo 0 > /sys/class/gpio/gpio1/value # you may need to change this depending on your router
  # you may add here a command or a script to be executed when the device is unavailable
  fi
  done
  # eof

give the right permissions to the script:

  chmod 755 /root/check_presence_bt.sh

test it:

  /bin/sh /root/check_presence_bt.sh

to launch the scan every minute, edit crontab:

  nano /etc/crontabs/root

and insert the following:

*/1 * * * * /bin/sh /root/check_presence_bt.sh

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
  • Last modified: 2022/07/27 08:28
  • by altuntepe