It’s 6 am. Time to get up. Your favorite song starts playing. A dim light comes on next to the bed. A few minutes later a bright light jars you from sleep. Meanwhile the smell of fresh-brewed coffee wafts in from the kitchen. Welcome to another automated morning with your Raspberry Pi and some Plugable PS-BTAPS1 Bluetooth Switches.
All you need to send your morning into the future are one Raspberry Pi, one Plugable USB-BT4LE Bluetooth adapter, and a Plugable PS-BTAPS1 Bluetooth switch for each lamp, coffee pot, or other morning essential you want to control.
There are four basic steps to get this going: 1. Get the Pi Ready, 2. Set Up Bluetooth, 3. Communicate with Your Switches, and 4. Set Up a Schedule. I assume you already have your Pi up and running. The instructions in this guide assume you are using the Raspian distribution, available here. Raspbian is also installed by default if you use the NOOBS installer.
Get the Pi ready
Connect your Pi to the internet, then make sure everything is up-to-date by running the following commands:
sudo apt-get update sudo apt-get upgrade
Answer “Yes” to any prompts.
Next install the latest Bluetooth drivers by running the following at the command prompt:
sudo apt-get install blueman
The script we will use to control the switches is written in Python and uses the PyBluez module. Both can be installed with this command:
sudo apt-get install python-bluez
Plug your Bluetooth adapter into the Pi and you are ready to go!
Set Up Bluetooth
Now, let’s make sure Bluetooth on the Pi is working and can communicate with the BTAPS1.
Plug the BTAPS1 into a convenient electric outlet. Make sure Bluetooth adapter is plugged into a USB port on your Pi and issue the following command:
hcitool scan
You should see a list of Bluetooth devices in the area. One of the lines should end with “Plugable.” That is your BTAPS1.
If you don’t see your switch in the list, make sure everything was set up correctly. If it still isn’t responding, contact support@plugable.com for help.
Write down the number in the first column. It’s the unique address for this switch, called a bdaddr. You’ll need it later. Mark the BTAPS1 you used so that you can distinguish it from the others you have (I write the bdaddr on a piece of masking tape attached to the switch). Plug in your other BTAPS1 switches one-by-one, run the command, write down each bdaddr, and mark each switch.
Communicate with Your Switches
Now that we know the Pi can see each switch through Bluetooth, let’s set up the script that controls them.
Ivan Fossa Ferrari, one of Plugable’s resident software geniuses, has written a script which allows you to switch the BTAPS1 off and on by sending a short command from your Pi. You can see the script and learn how it works here.
We will download that script and save it as a file that can be executed by Python on your Pi.
- Open the Epiphany browser on your Pi and open this same blog post there.
- Right-click the following link: Download Script
- Select “Save Link As…”
- In the window that appears, change the name of the file to btaps.py.
- Click Save. The btaps.py file should be saved in your home directory.
To test the script, plug one of your BTAPS1 switches into an outlet, then plug a lamp into it. Switch the lamp to the ON position. It shouldn’t turn on because the BTAPS1 should still in OFF status.
In terminal window, type this:
python ~/btaps.py 00:00:00:00:00:00 on
Replace 00:00:00:00:00:00 with the bdaddr for the switch you are using. The light should turn on!
If it doesn’t work, make sure the lamp is switched on, and that the address was typed correctly. If that isn’t the problem, make sure the BTAPS1 is still accessible to your Bluetooth adapter by running hcitool scan command again.
Now issue this command:
python ~/btaps.py 00:00:00:00:00:00 off
Again, replace 00:00:00:00:00:00 with the bdaddr for the switch you are using. The light should turn off.
Did it work? Great! Try the same thing with the other switches you have and make sure they respond to the command also. Remember to change the bdaddr in the command each time you test a different switch. If you forget the bdaddr for a switch, you can always find it out with the hcitool scan command.
Set Up a Schedule
Now it’s time to get creative. How do you want to wake up? Loud blast of music? Gentle lights? Toast and coffee? Let’s set it up. We’ll use the cron function in the Pi that allows us to schedule events in many different ways.
First, let’s make sure your Pi wakes you up in the morning of your time zone and not somewhere else. At the command line, type:
date
You Pi will display its concept of the time and date. Is it correct? Check the three letters to the right of the time. Do they look like your time zone? If they say UTC, your Pi still thinks it is in the UK, and has to be educated. Type this command:
sudo raspi-config
Select item 4 “Internationalization Options.” Go to item I2 “Change Timezone.” Wait a moment for the next screen to appear. Select your geographic area, then on the next screen, select your city or location. Press the tab key until you are on “OK”, then press Enter.
Type the date command again. Make sure it shows your correct time zone. If the time or date is wrong, correct it by typing a command in this format, using the current time and date in your time zone:
sudo date -s HH:MM:SS
Check it again.
Now let’s set up a cron job to turn on that light. Type the following at the command prompt. You don’t want to use sudo because this job will be personal to your login:
crontab -e
The default nano editor will open the the crontab file, which is used to schedule events to happen at specified times. You can use it to issue commands that turn your switches on and off whenever you want, and have it repeat in many different ways. If you have never used crontab before on this Pi, it will open a file that explains the format for scheduling new cron jobs. I’ll explain it here too.
A single cron job issues a single command on a schedule you set. For example, you can turn on a light every day at 7 am or turn your coffee pot off at 8 am. It consists of a single line added to your crontab file. You add that line to the crontab file with the crontab -e command. Never open the file and edit it directly.
The line has five numbers that determine when it takes effect. When the appointed time comes, the cron program issues the command that follows the numbers in the line. It looks like this:
M H DOM MON DOW COMMAND
- M refers to the minute you want the command issued. For example if you want the light to come on at 7:15 am, you would put 15 here.
- H refers to the hour, using the 24-hour clock. If you wanted the popcorn popper to make you popcorn at 4:00 pm, you would put 16 here.
- DOM refers to the day of the month. If you wanted your light to come on only on the third of the month, you would put 3 here.
- MON refers to the month. If you only wanted your command to be issued in November only, you would put 11 here.
- DOW refers to the day of the week, with Sunday being either 0 or 7 and all the other days numbered in order. If you wanted your coffee only on Monday, you would put 1 here.
- COMMAND refers to any command that can be executed by your Pi.
For any of the numbers, putting * will make it happen every minute, every hour, every month, or every day of the week, respectively.
For example, if you add this line, it will turn your switch on at 5:30 am every Wednesday:
30 05 * * 3 python ~/btaps.py 00:00:00:00:00:00 on
You can turn it off at 7:00 am every Wednesday like this:
00 07 * * 3 python ~/btaps.py 00:00:00:00:00:00 off
You can have it come on when you arrive home from work at 8:00 pm (you work hard!):
00 20 * * 3 python ~/btaps.py 00:00:00:00:00:00 on
This command would operate every day at 9:20 pm:
20 21 * * * python ~/btaps.py 00:00:00:00:00:00 on
It’s a good idea to practice this by making a sample lines that activate in a few minutes. For example, if the current time was 7:30 pm, a good test would be:
32 * * * * python ~/btaps.py 00:00:00:00:00:00 on 34 * * * * python ~/btaps.py 00:00:00:00:00:00 off
Save the file with control-o and control-x, and wait to see if the action happens. The light should turn on at 7:32 pm and turn off at 7:34.
You can wake up to your favorite song at 7 am everyday with the following cron job. Just connect your speakers to the Pi, and make sure your favorite song is in your home directory:
00 07 * * * omxplayer ~/yourfavoritesong.mp3
Once you feel you’ve mastered cron jobs, make a schedule for your morning. Create a line for each switching action for each BTAPS1 switch, then save the file as before with control-o and control-x. You can comment using a hash mark (#).
On every Tuesday and Thursday, the script below would fire up the coffee maker at 6:50 am, turn on your bedside lamp and play your song at 7 am, then turn on the bright lights at 7:10 am. It would then turn off everything when you leave for work at 8 am.
50 06 * * 2 python ~/btaps.py 00:00:00:00:00:00 on #Coffee maker ON 6:50 am TUE 00 07 * * 2 python ~/btaps.py 00:00:00:00:00:00 on #Bedside lamp ON 7 am TUE 00 07 * * 2 omxplayer ~/yourfavoritesong.mp3 #Song plays 7 am TUE 10 07 * * 2 python ~/btaps.py 00:00:00:00:00:00 on #Very bright lamp ON 7:10 am TUE 00 08 * * 2 python ~/btaps.py 00:00:00:00:00:00 off #Coffee Pot OFF 8 am TUE 00 08 * * 2 python ~/btaps.py 00:00:00:00:00:00 off #Bedside lamp OFF 8 am TUE 00 08 * * 2 python ~/btaps.py 00:00:00:00:00:00 off #Very bright lamp OFF 8 am TUE 50 06 * * 4 python ~/btaps.py 00:00:00:00:00:00 on #Coffee Pot ON 6:50 am THU 00 07 * * 4 python ~/btaps.py 00:00:00:00:00:00 on #Bedside lamp ON 7 am THU 00 07 * * 4 omxplayer ~/yourfavoritesong.mp3 #Song plays 7 am THU 10 07 * * 4 python ~/btaps.py 00:00:00:00:00:00 on #Very bright lamp ON 7:10 am THU 00 08 * * 4 python ~/btaps.py 00:00:00:00:00:00 off #Coffee Pot OFF 8 am THU 00 08 * * 4 python ~/btaps.py 00:00:00:00:00:00 off #Bedside lamp OFF 8 am THU 00 08 * * 4 python ~/btaps.py 00:00:00:00:00:00 off #Very bright lamp OFF 8 am THU
So fill up that coffee maker, plug in the BTAPS1, and hit the sack. Maybe the future hasn’t brought you a robot maid or personal helicopter yet, but your automated morning wake-up is already here with Pi and coffee!
If you have any questions at all, please comment below or email support@plugable.com. We’re happy to help!