Bluetooth presence detection without Raspberry Pi 3

@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?