Raspberry Pi – Uses of Bluetooth

Very good, In this article I wanted to document some utility that I got from one of the Raspberry that is around the house, I have a mini USB bluetooth device that I connected to the Pi, I'll give you a couple of scripts in case you may be interested: One will first detect us if we are ready to execute an action; and the second will connect a wireless speaker!

Raspberry-Bluetooth-bujarra

As I told you some time ago, I will be posting curious scripts that I have been compiling for the entire home automation system that I set up at home, I hope you can find something useful. As I was saying, the first script will use the USB bluetooth device connected to the Raspberry to find me, Like Pi is at home, when it finds me it will execute a command (In my case it will deactivate the home alarm). And the second script will connect the same Bluetooth dongle from the Pi to a wireless bluetooth speaker, to send him audios, Type Music, radio or some command that I want to talk to me.

 

The first thing will be to install the requirements, and verify the device's MAC, if it is not on our list, we will verify that we have the drivers loaded, To find out the IDE of your device you can search for it with 'lsusb’ .
[SourceCode]Sudo apt-get install bluez python-bluez

He did tool dev
Devices:
Done it 00:24:94:AA:XX:XX[/SourceCode]
 

My first need was to try to automate the home alarm system, And this is very easy, If my mobile is at home then the alarm should be deactivated, When my phone is not there, The home alarm must be set. How? Very easy if we have an Android, since they always respond to PING, so in our DHCP at home we make a MAC assignment and with any script we pinged it, That doesn't respond… Raise the alarm! What answers… Disassemble the alarm! But I have an iPhone and this is not so simple, As you may know, iPhones in chillout mode do not respond to PINGs, that is, almost never, unless you have it unlocked. Solution… Use Bluetooth! By the way, For the curious, Setting up the alarm is nothing more than motion sensors that I have around the house, that when the alarm is mounted they send a signal to a camera to record videos (of 10s) and he sends them to me by email, In addition to sounding through the loudspeakers one of these submarine sirens =). Those same sensors, When the alarm is disconnected, they help me turn on the different lights in the house as I walk.

Python script to check if something responds via bluetooth, I call him 'busca_hector.py', and search for my iPhone every time, once he finds it, he runs a script and falls asleep 5 minutes:
[SourceCode]#!/usr/bin/python
Import Bluetooth
import time

HectorIN = 0
HectorOUT = 0

print "In/Out Board"

while True:
print "Looking for " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
result = bluetooth.lookup_name('A0:18:28:59:XX:XX', timeout=5)
if (result != None):
print "Hector: in"
if HectorIN == 0:
HectorIN = HectorIN + 1
import subprocess
subprocess.call(['Bash','/home/pi/alarma_apaga.sh'])
subprocess.call(['Bash','/home/pi/textoAvoz.sh','Welcome home Hector'])
HectorOUT = 0
time.sleep(300)
else:
print "Hector: out"
HectorIN = 0
HectorOUT = HectorOUT + 1
print "Hector IN:"
print HectorIN
print "HectorOUT"
print HectorOUT
print "——"[/SourceCode]
 

We can test it by running:
[SourceCode]python /home/pi/busca_hector.py[/SourceCode]

GOOD, As you may know, there are several ways to get that Python script to boot with the Pi, The easiest thing to put it in the cron easily, We run 'crontab -e’ and we add ‘@reboot python /home/pi/busca_hector.py

 

Connecting a wireless speaker,

So far to detect any bluetooth device, With that we will be able to detect any device such as a mobile phone and execute any action. Now let's give more use to the Bluetooth dongle, Since I had a bluetooth speaker lying around, I show you what would be necessary to connect to the Raspberry said wireless speaker:

 

When pairing, it will give an error if we do not first edit the file 'sudo vim /usr/bin/bluez-simple-agent', replacing the word 'KeyboardDisplay' with 'DisplayYesNo'

GOOD, We start a scan to see if you see our speakers, We run 'hcitool scan':
[SourceCode]Scanning …
00:58:50:52:98:48 H88[/SourceCode]

Now we will pair the devices, for this, If it has a button, the speaker will press it and run it:
[SourceCode]sudo bluez-simple-agent hci0 00:58:50:52:XX:XX
Release
New device (/org/bluez/2405/hci0/dev_00_58_50_52_XX_XX)[/SourceCode]

We connect to the speaker with:
[SourceCode]SUDO Bluez-Test-Audio Connect 00:58:50:52:XX:XX[/SourceCode]

We make you trust our speaker:
[SourceCode]sudo bluez-test-device trusted 00:58:50:52:XX:XX yes[/SourceCode]

And we verify that it has changed to 1:
[SourceCode]sudo bluez-test-device trusted 00:58:50:52:XX:XX[/SourceCode]

We modify the file '.asocundrc’ with: 'vim ~/.asoundrc’
[SourceCode]PCM.bluetooth {
Type Bluetooth
device00:58:50:52:XX:XX
}[/SourceCode]

We also publish 'audio-conf’ with: 'vim /etc/bluetooth/audio.conf’ and we add:
[SourceCode]Disable=Medium
Enable=Socket[/SourceCode]
 

Also, I'll install an app so I can easily play any MP3 or M3U, whether from local files or URLs, such as an online radio station =)
[SourceCode]SUDO apt-get install bluez-alsa mpg321
sudo /etc/init.d/bluetooth restart[/SourceCode]
 

To play an mp3 file we will try with:
[SourceCode]MPG321-A Bluetooth-G 15 cancion.mp3[/SourceCode]
 

To play .m3u or playlists, it will be enough to put the address:
[SourceCode]mpg123 -@ http://radio7.oye.fm:7501/listen.pls -to bluetooth[/SourceCode]
 

We can search for the radio station we like the most on the Internet, that there are a lot of lists! I give you a couple of examples, Radio 3 –> HTTP://radiolive.rtve.es/radio3.m3u & Radio Euskadi –> HTTP://www.listenlive.eu/radioeuskadi.m3u

Recommended Posts

Author

nheobug@bujarra.com
Autor del blog Bujarra.com Cualquier necesidad que tengas, Do not hesitate to contact me, I will try to help you whenever I can, Sharing is living ;) . Enjoy documents!!!