1 year ago

Create a Raspberry Pi 3 Bluetooth tag

Have a Raspberry Pi 3 switch on your computer when you get home from work, by looking out for a Bluetooth device

Affordable automation is just one of the many popular projects for the Raspberry Pi since its release. We can easily see why: it’s small, easily programmable, and has a huge community backing it with software and support. Now that the Pi 3 has built-in Bluetooth, it’s even easier to create events to help control your house better. We’re going to show off a simple project by creating a Python script that sends a Wake-on-LAN (WoL) magic packet to your PC wh3en it detects a specific mobile phone’s Bluetooth signal.

STEP-01 Prepare your Raspberry Pi

We’ll need several extra pieces of software added to the Raspberry Pi to get our little system working. Both connecting to Bluetooth and sending a Wake-on-LAN signal require extra modules for Python. First, grab pip – the Python package installer – so we can add them:

Once that’s done, you can install the PyBluez and wakeonlan modules with pip, using the following two commands:

STEP-02 Get an address

We’re going to program the Pi to look out for a specific Bluetooth device, using the latter’s MAC address for complete accuracy. While creating this tutorial, we used an Android phone; for any Android device with Bluetooth capabilities, you just need to go to ‘About phone’ and then ‘Settings’ to find the MAC address for the Bluetooth interface. You’ll need to look up information specific to your type of phone or Bluetooth device on how to find its MAC address. Either way, make a note of this address.

STEP-03 Wake up your PC

Wake-on-LAN is built into many PCs. The wired network interface is receptive to the ‘magic packet’ that tells it to turn on. However, the WoL packet needs a MAC address to look for, rather than an IP address, as the PC is off. Boot into the BIOS of your PC; directions to do this should be on the splash screen when you turn your PC on. Enable Wake-on-LAN if it’s available, then boot into your operating system and look up the details on your wired network to find the MAC address.

STEP-04 Get coding

We’re just about ready to get this all working; all we need now is the code. Copy it from the listing or download it from magpi.cc/BluetoothTag. You’ll need to edit it a bit on the Raspberry Pi, replacing the MAC address for the phone with the device that activates the Wake-on-LAN. You’ll also need to edit the MAC address on the line wol.send_magic_packet to be that of your PC. Press F5 to give it a test; you may need to play about with your device’s Bluetooth settings for it to work properly.

STEP-05 Run at boot

It won’t be easy to run the script in IDLE all the time for this, so it’s best to have the script executed at boot time. Assuming this isn’t a Raspberry Pi you’ll be using for much desktop work, you should first set it to boot to command line and also to automatically log in. Once that’s done, open up your user profile with:

…and add this line to the end of the file:

Save and exit. Rebooting will start the script automatically.

STEP-06 Future edits

The script is easily upgradeable and expandable. You could have it look for multiple Bluetooth devices, you could change the timing (how long it checks and the time between checks), you can have it send a magic packet to multiple systems, etc. That’s just the beginning as well: with such triggers, you can set off many other automation tasks that can be controlled by Python.

Code listing

  • MrI IVD

    Interesting stuff! Getting the various -dev components installed really helped get things going for me. While I appreciated the clever uses shared in the article and code, I’m wondering about extending this to BLE devices too. With the number of Beacon-related topics out there, can a starter code using Smart BlueTooth devices be developed and shared?

  • Jason Lazerus

    Can I have this send an https post upon arriving home? And if so, does anyone have some detailed instructions?