Thursday, July 14, 2016

Home Automation via Bluetooth Proximity Awareness

-----
Bluetooth devices are everywhere and so are Raspberry PI computers. Here we show how to combine the two for some home automation.
-----
If you are not interested in duplicating the build maybe this summary of what the rig does will change your mind: Basically this allows any discoverable Bluetooth device (cell phone, smart watch, automobile, tablet, PC, toothbrush, etc.) to control GPIO outputs on the Raspberry PI. For our use, the Raspberry PI monitors if one of the Bluetooth devices is found in range after being absent for a predetermined time. If 'yes', turn ON some lighting in house so there is no fumbling around in the dark. Then, turn the lights back OFF after a predetermined time. It is easy to modify the Python source for other creative applications.

In addition to controlling the lights, the Bluetooth device "IN/OUT"
status and other statistics are logged to the terminal and a Google Drive Sheet via IFTTT.
-----

A while back we did a proof of concept. In the video above the blue LED turns ON or OFF depending on if the Raspberry PI (with BT-LE dongle) senses a Bluetooth signal from the iPad. iPad Bluetooth OFF then blue LED OFF. iPad Bluetooth ON then blue LED on. From this point it is a trivial task to switch a relay to control a brighter heavier load LED strip in place of the low current blue LED.
-----
And here is a short video of the completed rig turning ON and OFF a LED strip light. Note we are toggling GPIO13 (the relay control line) via WebIOPI. It works the same with the recognized Bluetooth devices, but it would make for a long and boring video to show that.

-----
Materials:
- Raspberry PI Model B
- WiFi USB dongle (already on board the Raspberry PI3 Model B)
- Bluetooth USB dongle (already on board the Raspberry PI3 Model B)
- Switching relay to control the light Only 1-channel needed, but more allows for expansion. Spend the extra $1 for a unit like the one in the link with opto isolation built in to help protect your RasPI.
- LED light strip Switch low voltage for safety. This light is what the relay turns on to light up the room. Many examples in the link.
- Power supply for the RasPI, wire, maybe a USB hub, a few other very obvious things.
-----

Connect it up like this:

-----
When you get the rig together it will look something like this. The RasPI pictured has a few extra 'things' attached to it to support our LAN status and "Internet Up" status monitoring projects (in addition to a few other chores).

-----
Now for setting up the software... The program has comments all throughout the code it to try to make things straightforward. It is optional, but if you want to log the status of your Bluetooth devices to your Google Drive you need to do a few things first.
- Create a Google account if you don't have one.
- Create an IFTTT account you don't have one.
- Connect the Google Drive channel and the Maker Channel.
- Follow the IFTTT directions on creating a Maker Channel trigger to update a Goggle Drive file.
- Establish the Bluetooth devices that will be tracked. Thanks to prb3333 the utilities and procedures to do this are on their instructables.com site. This project leverages their work and credits them in the source code below.
- Now, copy the source code below and paste it into your favorite Raspbery PI Python editor. If you are logging to Goggle Drive with IFTTT adjust the Maker Channel 'secret code' to match the one assigned to your account. If you decided not to log to Goggle Drive then comment out the IFTTT Maker Channel call statements in the code.
- Enter the Bluetooth addresses you want to track into the variables of the Python source.
- Adjust the scan variables, number of tracked devices, ON time, etc. variables to meet your application needs.
-------
All the wires connected? Python script in the RasPI? Variables adjusted to meet your needs? Good job! Save and run the script to enjoy the automation.

The logs to Goggle Drive and the RasPI terminal will look something like this:

Device_name = [1,2,3,4,5,6,7,8,9] # Friendly name of the BT device Device_ID = [1,2,3,4,5,6,7,8,9] # BT identifier for device as xx:xx:xx:xx:xx:xxLast_In = [1,2,3,4,5,6,7,8,9] #Time of last check in.Last_Out = [1,2,3,4,5,6,7,8,9] #Time of last check outDevice_Status = [1,2,3,4,5,6,7,8,9] # String info on if device is IN or OUT.Device_Delta = [1,2,3,4,5,6,7,8,9] # Tracks the time a device has had the same BT check status

t0 = datetime.datetime.now() # for timestamp math. t0 is the time the program run startedFirst_run = 1 # always log intitial status for 1st runLog_Deactivated = 0runtime = datetime.datetime.now() - t0 # track how long the program has been running.

# Inverts or 'blinks' the LEDS every .5 secs to confirm program is running # Delay until next scan of BT device status. for jj in range (0, Scan_interval): GPIO.output(11,GPIO.HIGH) #FLASH Red RGB on time.sleep(0.5)

GPIO.output(11,GPIO.LOW) #Red LED Off time.sleep(0.5)

for jj in range (0, 10): #Quickie flack of red LED to show new scan starting GPIO.output(11,GPIO.HIGH) #FLASH Red RGB on time.sleep(0.1)