Saturday, January 7, 2017

Device proximity detection

There are a lot of projects that you could want to have your pi do certain things based on if you are or are not near it.  I've found two good ways to sense this assuming you carry your phone with you:


You can use wifi to detect devices within further proximity to the wap, or bluetooth to detect devices within closer proximity to the pi...

Detect when you are in the area using WiFi


This method assumes your pi and your phone use the same wifi, such as at your house, and that your WiFi is always on.  Also you will want to set a static IP on your phone, or DHCP lease reservation on your router, so the pi is always trying to find YOUR phone and not a different device that connects to your wifi network later.


This will return that your phone is in range whenever it's pingable from your pi (connected to the same network).  Your pi can be on ethernet or wifi, and as long as your phone is connected to your LAN this should work.

Detect when you are in the immediate proximity using Bluetooth


This is a little different because it's based on how close your phone is to your PI, not your wifi network.  With the bluetooth module (built in to a pi3), the pi is trying to reach your phone.

You will need two things, the full MAC address of your phone's Bluetooth radio, and your phone's name.  Basically your pi sends out a unicast packet to the MAC of your phone, asking for it's name.  If it gets a response with the name it's expecting (that you hardcoded in the script), then it considers it in range.


This will return "in range" whenever your pi is within bluetooth radio range, typically 20-30 feet indoors?  Note that the device does NOT have to be paired to the pi to respond to it, so there is nothing you need to do on your bluetooth device other than get the name it responds with, and MAC address.  If you are having trouble figuring out the name, try print(bluetooth.lookup_nam("MAC")).

Possible Applications

Presence detection can be a very powerful thing to add to projects.  Lets say for example, you have a pi that texts you a photo when it sees someone coming in your front door.  You can use presence to disable that whenever you are in the area, so you don't get spammed with pictures of yourself.  Another possible application is lighting that turns on when you arrive home.

Limitations

I've run into some problems with the bluetooth radio, most notable when you are constantly searching if a device is in range, it will make the wifi unusable.  This isn't a problem if you are using ethernet, but with WiFi you might want to have the bluetooth device only search every 5-10 seconds or so.

Remember that generally your pi can't ping a wifi device directly (unless it IS the WAP, but that's not covered here).  The packet always gets sent to the wifi access point, and then to your device from there.  Though this isn't a bad thing, if you are in a big company that has wifi access points all over the place, your pi might be able to detect you if you are anywhere on the premises.

With bluetooth, the minimum requirement is that the radio is on.  With WiFi, the radio has to be on, and your device connected to a network that's connected somehow to the pi.

Good luck with your projects!

A simple raspberry pi Juke Box (what I have so far)

So I wanted to make a pi that will randomly select MP3 files from a directory and play them out a headphone jack.  Then I would use something like BTSync (or Risilio or whatever its called now) to push mp3's into it from a computer.  I've got the basic script that plays random tracks:



I'm just using random.choice(os.listdir()) to find a random file.  Then I use .replace to change spaces to have an escape character.  Finally I'm formulating a launch command to run it with omxplayer and executing it.

I've got a grep via os.system to see if the track is still playing and if not, moves on.  From another terminal you can run "pkill omxplayer" to terminate the track which is the same as it ending normally:



I've also got some logic that will keep the same track from playing twice, that's where it keeps picking randomTrack until it's not the same as lastRandomTrack.

During the wait I want to add buttons for the following:
1. Kill OMX player (if you just want to skip the track)
2. Kill OMX player AND move the track out of the play list (to tracks/disabled or something)
3. Safely shut down the radio for power off
4. A power switch (turns on the pi which auto launches to the radio, use the safe shutdown button before flipping it off)

More to come!