I am doing my best to learn Python by writing a script for my home automation setup. I want to monitor the presence of 4 Iphone's and as things are right now I want to avoid installing any app to handle this. (Yes, this is both impractical and not 100% accurate - but why go the easy route? :))
I have, by the use of Stackoverflow and Google, been able to create a fully functional script that runs on my RPI3 that does the following:
- Creates 4 processes (one for each person)
Checks if their phone is present by:
- pinging their ip (using dhcp reservations)
- looking up their mac in arp
- looking up their bluetooth name (using Pybluez)
Make a call to my Smart Home solution setting a virtual switch to either present or away
This all works fairly good - aside from one fact: Even though I am using three separate checks I am bound to get into a situation where they all 3 come up empty even with the person being home (Because Iphone's are notorious for switching of their wifi when in deep sleep, and bluetooth is not always reachable). And so for these situations I am redoing the checks for a few minutes before setting them as away.
When all is said and done this means that I am getting a lot of bluetooth calls, which I now believe is the source of my problem:
I randomly get no response from my bluetooth lookups.
I have tried two approaches:
- bluetooth.lookup_name(macadr, timeout=timeout)"
- subprocess.check_output(['hcitool', 'name', macadr])
and even though I am using Try/Catch it does not produce any error. That being said I am fairly sure that my "invisible" problem is a result of calling the lookup functions almost simultaneously - resulting in a "Device busy" problem.
As I am still learning the ropes in Python - is there a way for me to better control the communication with these devices? I should point out that none of the devices should be required to be paired with the solution.
The effect I am seeing can be reproduced from multiple shells by issuing "hcitool name AA:BB:CC:DD:EE:FF" simultaneously - the effect will be that they cancel each other out. Similarly - if I start my script and call "hcitool" from the prompt it will break the lookups in my script.
Any input would be highly appreciated.
//Thomas