1

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

pittbull74
  • 11
  • 2
  • Well, the bluetooth resource is only one and you're trying to access it before it's made available again, so I don't think it's surprising that you get this kind of behaviour. Maybe a simple `time.sleep` will help? – ChatterOne Jan 02 '18 at 10:34
  • Seem to be spot on yes. But having no understanding of how these things are working under the hood I am strained for options. Would it be possible to create a socket for each process and using that to call the lookup function? Could the lookups be queued some how? - Yes, I have tried sleeping, but since I have 4 processes running there's no guarantee that not one of these will be executed at the same time. – pittbull74 Jan 02 '18 at 10:41
  • Why do you have 4 processes running? If your idea is to have one process for each of the devices you want to "ping", you're probably much better off by using discovery and checking if any of the devices you want is in the discovery list (I mean with `discover_devices` ). – ChatterOne Jan 02 '18 at 12:33
  • Good idea on using discovery to limit bt communication. The reason for running 4 processes is - or was - to limit waiting. In other words to achieve some level of parallel processing. – pittbull74 Jan 02 '18 at 12:37
  • I abandoned the device_discovery because I found devices not showing up during a discovery but it did answer to lookup_name. – pittbull74 Jan 02 '18 at 13:48
  • To futher clarify - this command: "sudo l2ping macadr" will be able to ping a device successfully that does not show up in a device_discovery. – pittbull74 Jan 02 '18 at 15:05
  • Be careful with `sudo`ing commands. Either you have to input a password every time, or you have to set the `s` bit, which is inherently a security risk. – ChatterOne Jan 02 '18 at 15:56

0 Answers0