Posted on 2 Comments

How to Make Raspberry Pi Bluetooth Proximity Monitor


This project uses a Raspberry Pi W Zero to continuously monitor an area for a specific Bluetooth MAC address. When the Bluetooth MAC address is within a certain range, a WeMo connected device is turned on. When the MAC address leaves the area, the WeMo devices is turned off.

There are many use cases for this other than turning a WeMo device on and off. This could easily be modified to initiate any action. For example: send a text or an email when a specific Bluetooth MAC address is near by. Notify you when your kids or wife arrive and leave. Have the device speak a customized greeting using gTTS (Google Text to Speech), etc.

There are many different us-cases.

Hardware Required

Raspberry Pi W Zero Starter Kit

You can get the Raspberry Pi components more cheaply on many websites. However, this one has everything you need to start, including a case and power supply.

SanDisk 16GB SD Card

A large SD card isn’t needed for this project. As stated above, this device will only be used to monitor Bluetooth activity.

Igsignia SD Memory Card Reader

This isn’t 100% necessary. If you have a SD card reader, you do not need this. I personally like this because I can install Raspberry Pi operating systems onto SD cards from any computer.

WeMo Smartplug

This will be used to let the Raspberry Pi turn on the light.

Software Required


Setup the SD Card

Download the NOOBs Lite operating system for your Raspberry Pi

There are several ways to install Raspbian on a SD card. I have chosen to use NOOBs in these instructions because it is the easiest for a beginner. NOOBs light is a lightweight bootable interface that allows you to use a Wi-Fi connection to choose the operating system that you want to install. We will be choosing a lightweight OS called Raspbian Stretch Lite. The reason for this is that this Raspberry Pi will serve only one purpose, Bluetooth monitoring. All of the other bells and whistles found in all other Raspberry Pi operating systems are not needed. This is typically refered to as a headless system, one that operates without a monitor. If you don’t want to use NOOBs, you can install Raspbian Stretch Lite directly by downloading it and flashing your SD card with the image. View instructions on How to Install Raspbian Stretch Lite on SD Card.

Unzip the NOOBs Lite zip file. Extract all of the files to a folder on your computer.

Put the SD card into your SD Card Reader and browse to the SD card folder. Copy all of the files extracted from the NOOBs zip file into the root directory of your SD card.

Remove the SD Card from your SD card reader.

Setting up the Raspberry Pi

Insert your SD card into the Raspberry Pi Zero board.

Plug the micro HDMI cable that came with the kit into the Raspberry Pi. Plug the other end into a display of your choice.

Use the micro USB cable that came with your kit to plug in a keyboard.

Plug the power cable that came with the kit into your Raspberry Pi. Plug the other end into a wall outlet.

The Raspberry Pi will boot for the first time.

You will be prompted to select a Wi-Fi network. Select your network and enter your password. This Wi-Fi connection will be used to download the operating system. You may need to flip flop between a USB mouse and keyboard to go through the prompts.

When prompted to select the operating system, choose: Raspbian Stretch Lite

Once connected, your Raspberry Pi will begin downloading Raspbian Stretch Lite. Once complete, your Raspberry Pi will reboot.

When prompted, you may be prompted to enter the default username and password:


It is recommended that you change the default password. It is never a good idea to keep defaults. This can be done using the following command:

sudo passwd

Setting up the Raspberry Pi for Bluetooth Proximity

Let’s make sure everything is up-to-date before installing any packages. Issue the following commands:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install vim
sudo update-rc.d ssh enable

I personally like using Vi or Vim editor. Enable SSH so that you can control the Raspberry Pi remotely if needed. It is a lot easier to SSH into the Raspberry Pi and mange it than to have it plugged into an external display.

Install Python

Our Python program was written for and tested against Python 2.7.13. If you want to use a newer version of Python, you may need to modify the code for the Bluetooth Proximity monitor. Check your version of Python:

python –version
sudo apt-get install python
sudo apt-get install python-pip
sudo apt-get install bluetooth libbluetooth-dev bluez bluez-tools
sudo python -m pip install pybluez

This will install python and pip. It will also install some tools for working with Bluetooth devices.

Navigate to the pi home directory. It should be: /home/pi/

Download the bluetooth proximity monitor python code to your raspberry pi:

sudo wget

Change the permission of the file:

sudo chmod 777 /home/pi/

Edit the script, replacing the temporary Bluetooth MAC address with your own MAC address. On an iPhone, you can find your Bluetooth MAC address by going to Settings > General > About > Bluetooth. Save the file, and run the program:

sudo python

Setup Script to Control WeMo Device

Install PeeWee:

sudo pip install peewee

Install git:

sudo apt-get install git

Navigate to a directory where you want to install the WeMo Control python libraries. Take note, you will need to modify the program to use this path. It would make sense to put it in the pi home directory: /home/pi/, Download the repository:

git clone

change the permissions of the library:

sudo chmod 755 ./wemocontrol/*.py

Edit the top of your ./ (it should be inside the directory that was just created in the above step) file to setup a storage location for database and also make nicknames for your WeMos and their IP addresses. I only have one device and the edited portion of the file looks like this. Your IP address and name of the device will obviously be different.

db = SqliteDatabase(‘/home/pi/wemocontrol/database.db’)

wemos = [

If you do not know the path to your WeMo, you can use a network IP scanning tool to find it. An easy app to use on the iPhone to find the IP address of the WeMo is Fing, but there are many tools available.

Run the following commands to setup the database and populate it with some initial values:

python ./
./ multiupdate

Modify to turn the lights on and off by modifying the near_cmd and far_cmd. The path to your wemocontrol directory and your WeMo device name may be different

near_cmd = ‘python /home/pi/wemocontrol/ downstairs.on’
far_cmd = ‘python /home/pi/wemocontrol/’

Also, test running these commands at the command line to test turning your WeMo device on and off outside of the program. If it doesn’t work via the command line, it definitely will not work inside the Python program.

Run the Bluetooth Proximity Program

sudo python ./home/pi/

This will run continuously. Test the output of this script by moving your device around the room. You will see debug output indicating whether you are near or far away from the Raspberry Pi. If you have configured your WeMo correctly, and put the correct commands in the near_cmd and far_cmd variable, the light should turn on and off.

Final Touches

If for some reason your Raspberry Pi were to crash or need to be rebooted, you don’t want to have to run this program every single time. You can add the script to the Raspberry Pi startup so that it will execute every time the Raspberry Pi turns on. You can do so by editing the following file: /etc/rc.local

Right above “exit” enter the command to execute your Bluetooth Monitor script. Save the file. Reboot the Raspberry Pi to test. The following command will execute the Python script in the background when the Raspberry Pi boots.

sudo nohup /home/pi/ &

I like to restart the device daily. In the event that there is a WiFi connectivity problem or a programming problem, restarting nightly can help reconnect and start the Python script. This can easily be done by creating a cronjob. To create a cronjob to run daily at 8 am, issue the following command:

crontab -e

At the bottom of the file, add:

30 8 * * * sudo /sbin/shutdown -r now

This will run the shutdown reboot command every morning at 8:30 am.