Page 1 of 1
Detecting a Tile
Posted: Fri Nov 23, 2018 12:15 pm
by oldjake
Hi
I've got a Pi that does some occupancy detection so that I can mess with the heating depending on who's in.
So, it's doing this:
Code: Select all
andyResult = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=10)
I get the MAC address of the device by running:
This works fine BUT...
If I have Bluetooth on my phone switched on all the time, it kills the WiFi speed (a known problem with some phones). So I bought one of these little tiles.
The MAC address appears when I do:
But when I run the code in my Pythin script on the BLE MAC address for the tile, it fails.
I'm guessing that it doesn't scan for BLE.
Is there another way I can do this?
Cheers
Andy
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 1:41 pm
by DirkS
oldjake wrote: ↑Fri Nov 23, 2018 12:15 pm
But when I run the code in my Pythin script on the BLE MAC address for the tile, it fails.
What code? Which module(s)?
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 6:27 pm
by oldjake
I think the module Bluez? It's a while since I installed it as it sort of works so I haven't worried until now.
Code is as my original post:
Code: Select all
andyResult = bluetooth.lookup_name('xx:xx:xx:xx:xx:xx', timeout=10)
It's just that one line. Then after that, it's:
Code: Select all
if (andyResult != None):
status = "In"
Phone BLE address - works a treat
Tile MAC address - doesn't work
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 6:42 pm
by Douglas6
That looks like code from the PyBluez Python module, which, as you've discovered, does not support BLE. You're going to want to look into Ian Harvey's bluepy module, which does support scanning for BLE devices, if memory serves, by way of a Scanner object.
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 9:04 pm
by bensimmo
Is there anything in BlueDot that can be used ?
https://bluedot.readthedocs.io/
written by one (or two) of the RPF regular python programmers
https://github.com/martinohanlon/BlueDot
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 9:19 pm
by Douglas6
No, Blue Dot uses strictly classic SSP communications. A fine program in its own right. You gotta love an app whose only interface is a big blue dot.
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 9:52 pm
by oldjake
Aha! Bluepy looks good. I've just installed that and looked at the scanner class. That looks like it scans for what's around.
What I can't suss (admittedly, I've only just started looking) is how to do the equivalent of the single line that I use at the moment to do a sort of 'ping' on one address.
The good thing is that I can see my tile and some general info from the command line blescan.
Re: Detecting a Tile
Posted: Fri Nov 23, 2018 10:13 pm
by Douglas6
Might be two lines instead of one:
1. devices = Scanner.scan(timeout)
2. if bdaddr in devices:
(do something)
Re: Detecting a Tile
Posted: Sat Nov 24, 2018 9:16 am
by oldjake
Thanks, Douglas6
I couldn't quite get your example to work but it pointed me in the right direction. I've fudged something together from the documentation which does work. It's probably overcomplicated in the extreme, and I'm not even going to pretend that I get what's happening at this stage, but it does the job for the moment.
Code: Select all
from bluepy.btle import Scanner, DefaultDelegate
import bluetooth
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
print "Discovered device", dev.addr
if "xx:xx:xx:xx:xx:xx" in dev.addr:
print "Found Tile"
else:
print "Tile not found"
elif isNewData:
print "Received new data from", dev.addr
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
I have emailed the Tile tech support people and asked about this. They told me that it was impossible... the Tile doesn't work like that. Not sure whether to e-mail them back and tell them to the good news or not.
Cheers
Andy