Bluetooth presence detection without Raspberry Pi 3

hey @Bit-River trying to use your script to find my fitbit or even my cell phone but I keep getting this error

pi@raspberrypi:~ $ sudo python3 bt.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
192.168.1.11 - - [21/Dec/2017 21:10:42] "GET /api/search/C6:17:76:1B:30:C2 HTTP/1.1" 200 -
[2017-12-21 21:10:43,275] ERROR in app: Exception on /favicon.ico [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 949, in _set_status
self._status = to_native(value)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/_compat.py", line 195, in to_native
return x.decode(charset, errors)
AttributeError: 'NotFound' object has no attribute 'decode'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1737, in make_response
status=status_or_headers)
  File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 842, in __init__
self.status = status
  File "/usr/local/lib/python3.6/site-packages/werkzeug/wrappers.py", line 951, in _set_status
raise TypeError('Invalid status argument')
TypeError: Invalid status argument
192.168.1.11 - - [21/Dec/2017 21:10:43] "GET /favicon.ico HTTP/1.1" 500 -

Not sure if I missed a step or something but I always get “false” for every device

Oh, I haven’t seen that error before. it’s looks like the flask sub-program doesn’t like the following section of my script:

@app.errorhandler(404)
def not_found(error):
    return("Not Found ", error)

Though it’s working fine for me right now with no errors, could you check to see if there’s a copy & paste error with that piece of code?

Also, it doesn’t need to be there - it’s handy for logs, error checking but otherwise serves no purpose.

So I would try the script without that section, leaving the rest as is. A simple comment-out will do.

   #@app.errorhandler(404)
   #def not_found(error):
        #return("Not Found ", error)

Other than that the only thing that looks different is using all Caps in the MAC address, where I usually have it in lower case. I’d be surprised if that was the cause, but worth a try.

Thank you! Commenting out those lines of code and using lowercase worked :slight_smile:

1 Like

DUDE - massive kudos!

just set this up on a pi zero at works like a charm! Thank you for sharing

Got a couple of mains 110V to 3.3V step down converters to see if i can 3D print a case with a plug connected so it can stay on in the middle of the house :slight_smile:

1 Like

Thrilled it’s useful for ya :slight_smile:

Another option is to use the cheaper esp8266 with a Bluetooth module:

Devs are currently working to get it running on a esp32 (wifi+bluetooth built in for ~$8/unit). I have one up but it’s not terribly stable yet on the esp32 - it will run for a day or two then crash. Been watching the github because esp32 support would make deploying a bunch of these real easy. They also read MiFlora data if you have those devices.

1 Like

I built an ESP32 presence detector and although it works great for a few days, it eventually stops working until powered off/on. I think the ESP32’s are quite fussy about power supply stability so I’m going to try going down the Pi zero W route.

Hi, where do you save the python script on the Pi?

I tried using the standard bluetooth_tracker and bluetooth_le_tracker. Slowed down my raspi quite a bit. Way too laggy for my taste.

Just used this script with these BLe devices:
https://www.amazon.com/gp/product/B07F39J8HL/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1

Works great! I also get cutouts here and there but I’m going to filter them out with some automation scripting.

I have never coded in python but this gives me the opportunity to learn and maybe do some interesting things with it. :slight_smile:

Thank you, Bit-River!
-Rey

2 Likes

@Bit-River

Hey - Thank you so much for this writeup! I think this MIGHT be what I’m looking to use my Tile Trackers for and hope you can help me out with some guidance…

I’m essentially looking to use Tile Bluetooth Trackers to identify if anyone is home or not home without running the Tile app on our phones with Bluetooth running 24/7. I tried to accomplish this using a Kindle Fire tablet which I installed the Play Store/Google Play Services/etc on and installed the Tile app. Upon logging in on the Kindle Fire and granting access to Bluetooth and location services the Tile app continually told me it wasn’t able to receive location access no matter what I did.

Before I order a new Pi 3 Model B+ or maybe a Pi Zero W I wanted to check with you to ensure I’m understanding this properly… Unfortunately, I have a spare Pi 2 Model B and a Pi Zero here, but no bluetooth dongles available right now.

  • I currently have a Raspberry Pi 3 Model B running Home Assistant using the Hassbian installation method. You can find my full configuration repo here.

  • Using your Python script I should be able to keep Bluetooth turned OFF on my phone and use the binary_sensor to determine if a Tile is home or not home, is this correct?

  • Can I install this on my existing Pi 3 Model B which is running Hassbian already?

If I can install this I believe I need to run sudo apt-get install libglib2.0-dev python-flask and then activate my venv by running source /srv/homeassistant/bin/activate.

Once my venv is activated I can run pip install bluepy, correct?

Where would I actually place the Python script? Would this go inside of /home/homeassistant/.homeassistant?

Thank you so much! I really appreciate any guidance you can provide. You can also reach me on Discord at bk#5430 if that’s easier for you.

Thank you! Cheers…

@nukee26

  • Once you obtain the MAC addresses can you re-install the Tile app?

  • Secondly - How do you determine which Tile’s MAC address is which Tile?

  • Lastly - If you have any guidance with my questions here any assistance would be greatly appreciated.

Cheers! Thank you!

The answer is yes to both of these.

Though I haven’t used it from a Pi3 B, I don’t see any reason why it won’t work.

For the venv, I haven’t tried but it should be fine as long as the venv is allowed to access BLE.

Where would I actually place the Python script? Would this go inside of /home/homeassistant/.homeassistant ?

If you’re running Hassbian, call the script from /etc/rc.local - put it in you pi home directory is easiest I guess.

Though if you’ve used a venv you might have to adjust for that.

Enjoy!

@Bit-River – Thanks for your reply. I’m still trying to get this sorted out and I’m stuck…

  • I tried placed your script in /home/pi/tile.py and /home/homeassistant/.homeassistant and when I try to run python tile.py in either directory it gives me this error
python tile.py
Traceback (most recent call last):
  File "tile.py", line 2, in <module>
    from bluepy.btle import Scanner, DefaultDelegate
ImportError: No module named bluepy.btle

I executed pip3 install bluepy inside of the venv at /srv/homeassistant and also tried placing this in /home/homeassistant/.homeassistant as the homeassistant user.

(homeassistant) homeassistant@hassbian:/home/pi $ pip3 install bluepy
Looking in indexes: https://pypi.python.org/simple, https://www.piwheels.org/simple
Collecting bluepy
  Downloading https://www.piwheels.org/simple/bluepy/bluepy-1.3.0-cp35-cp35m-linux_armv7l.whl (515kB)
    100% |################################| 522kB 1.5MB/s
Installing collected packages: bluepy
Successfully installed bluepy-1.3.0

Last issue is I have recorded the MAC address for my 4 Tile’s, however, how do I determine which MAC address corresponds with each Tile?

Thanks, I hope you’re able to help!

Could you try calling tile.py with python3 tile.py ? I imagine that should do the trick for you.

Also, on mine I have to run it as root, so sudo python3 tile.py or within rc.local

Finally, for checking which Tile is which tin foil is the quickest way :slight_smile:

Just wrap them all in tin-foil (on put them inside a metal box), then unwrap / remove one at a time - using sudo hcitool lescan at each step to see which device appears.

1 Like

@Bit-River – I’m now getting this error when I run python3 tile.py. Again, this is on a RaspberryPi running Hassbian.

Thanks, I hope you can help! I’m almost there…

(homeassistant) homeassistant@hassbian:/home/pi $ python3 tile.py
 * Serving Flask app "tile" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
[2018-12-11 16:46:47,107] ERROR in app: Exception on /api/search/c8:31:53:38:c9:f1 [GET]
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "tile.py", line 18, in device_scan
    devices = scanner.scan(3.0)
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 790, in start
    self._mgmtCmd("le on")
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
127.0.0.1 - - [11/Dec/2018 16:46:47] "GET /api/search/c8:31:53:38:c9:f1 HTTP/1.1" 500 -
[2018-12-11 16:47:18,110] ERROR in app: Exception on /api/search/c8:31:53:38:c9:f1 [GET]
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/srv/homeassistant/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "tile.py", line 18, in device_scan
    devices = scanner.scan(3.0)
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 790, in start
    self._mgmtCmd("le on")
  File "/srv/homeassistant/lib/python3.5/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'le on' (code: 20, error: Permission Denied)
127.0.0.1 - - [11/Dec/2018 16:47:18] "GET /api/search/c8:31:53:38:c9:f1 HTTP/1.1" 500 -

Try

sudo python3 tile.py

1 Like

@Bit-River – Thanks so much for your help thus far. I have it mostly working but have a few more questions and hope you can help!

  • After each reboot I seem to have to run sudo setcap 'cap_net_raw,cap_net_admin+eip' /srv/homeassistant/lib/python3.5/site-packages/bluepy/bluepy-helper shouldn’t this persist reboots?

  • If I restart Home Assistant I have to go through multiple steps. Any idea on how I can automate this via cron or otherwise?

After a restart of Home Assistant it seems I have to do the following:

  1. screen
  2. sudo -u homeassistant -H -s
  3. source /srv/homeassistant/bin/activate
  4. python3 tile.py
  5. ctrl + a
  6. ctrl +d

Even though the Tiles are all within 10-12 feet of the Raspberry Pi it seems they randomly show switch states between off and on as you can see here:

Thanks!

@Bit-River – Any chance you can help regarding the questions above?

Thanks, really appreciate your time!

Hi,

I’m not sure why you’d have to run ‘sudo setcap …’, I don’t have to do anything similar so perhaps it’s due to the virtual env.

For the second part, I’d put all that into a script then call the script in /etc/rc.local and that should do the trick.

good day. tell me can i use my phone (iphone) as a tag?