Bookmark Free VoIP Services!

Proximity Detection Perfection: Bluetooth + Asterisk + iPhone

13.03.2008 03:35 VoIP Asterisk - Source: nerdvittles.com

Proximity Detection Perfection: Bluetooth + Asterisk + iPhone

Filed under: — ward @ 3:00 am

If impressing your friends happens to be your thing, then you'll enjoy watching them all wet their pants today after you show them this little trick. It's been almost two years since we first introduced Bluetooth Proximity Detection to the Asterisk community. Because we can't seem to get enough programming torture lately, we decided to revisit Follow Me Phoning by taking it to a whole new level with an all-new set of devices. If you're new to all of this, what we're talking about is the ability to stroll out of your home or office and have your telephone calls follow you on your cellphone automatically... without touching anything. And, when you return, the home or office phones start ringing again just as if you never left. Won't your boss or spouse be thrilled? What makes all of this possible, of course, is Bluetooth which happens to be running on your TrixBox/Asterisk server and on your brand new iPhone.

Since our original article, TrixBox has replaced Asterisk@Home as the leading (free) turnkey Asterisk system. freePBX has replaced the Asterisk Management Portal as the best Asterisk shell on the planet. And, of course, the iPhone has blown the cellphone industry out of the water. For long-time readers of our column, you may recall that we whined that the Treo 650 used in the original article was lousy for proximity detection because you always had to turn the display on to activate Bluetooth. It also drained the battery quickly which is probably why Palm left it turned off most of the time. You won't have either of those problems with the iPhone. The iPhone Bluetooth Adapter is always broadcasting away assuming you have turned it on. And we left the iPhone near our Asterisk server for 8 hours with virtually no battery drain on the phone. So, yes, it's damn near perfect for this task... and so many others.

Our plan today is simple enough. We want to design a phone system so that, when you walk into your home or office, the phones ring in the office when there's an incoming call. And, when you walk out of your home or office carrying your iPhone with Bluetooth, calls to your home or office extension ring on your iPhone. If you're using one of our turnkey TrixBox systems at the top of this page, this project should take you about 20 minutes to complete once you have a dLink DBT120 USB Bluetooth Adapter in hand.

System Requirements. As mentioned, you'll need a Linux-based Asterisk server (we're working on Windows and Mac support), and we recommend our turnkey system because it has most of the software you'll need for this project preinstalled. That includes loads of Linux utilities, the Linux Bluetooth software toolkit, freePBX, Asterisk 1.2.x, and WebMin. But any TrixBox server will work. Other than your Asterisk server, the only other hardware you'll need is a dLink DBT120 Bluetooth Adapter. They run $30-$40 from most electronics stores although many retailers put them on sale regularly. CentOS 4.2 and above (used in the TrixBox builds) comes with the Bluetooth software preinstalled. If you're using some other Linux flavor, consult our original article for installation instructions.




DBT120 Bluetooth Adapter Installation. Boot up your Asterisk server if it is not already running and log in as root. Plug the DBT120 into an available USB slot. Now issue the following command:

/etc/init.d/bluetooth restart

You'll probably be told that your system couldn't stop bluetooth (because it wasn't running) and then it'll restart. Now let's make sure everything is running that should be:

/etc/init.d/bluetooth status

You should see messages that look like this:

hcid (pid somenumber) is running ...
sdpd (pid somennumber) is running ...

If you're alerted that some other application isn't running, we don't care. Now let's be sure the system has found your bluetooth adapter:

hcitool dev

Assuming you get a response telling you the system found device hci0 with the MAC address of the adapter, you have successfully installed your DBT120. So it's time to move on.

Configuring Linux Bluetooth Software to Start Automatically. You don't want to have to manually start up your Linux Bluetooth software each time you reboot your server. The easiest way to automatically start it is to use WebMin which comes preinstalled with our TrixBox builds. While logged in as root, make sure WebMin is running:

/etc/webmin/start

Then point your web browser to port 10000 on your Asterisk server: http://192.168.0.108:10000/. When prompted for username and password, type root and the password you assigned the root user on your system. From the Main Screen of WebMin, click the System button and then the Bootup and Shutdown link. Find bluetooth in the list of applications and click on it. The Action Details screen should show that bluetooth is running. Now click the Yes button beside "Start at Boot Time?" and then click the Save button to reconfigure your server. That wasn't hard, was it? Go back to the command prompt on your Asterisk server and stop WebMin by issuing the following command: /etc/webmin/stop. No need to waste processing cycles for a tool we're not using.

Deciphering the MAC Address of Your iPhone Bluetooth Adapter. We're going to be communicating with your iPhone to determine when you're in and when you're out. In order to do that, we need the MAC address of the iPhone Bluetooth Adapter. Here's how to find it. Move your iPhone within 10 feet or so of your Asterisk server. Then put your iPhone in Bluetooth Discovery Mode by choosing Settings, General, Bluetooth from the Main Screen on your phone. Be sure Bluetooth is set to ON. Once you've done that, the iPhone will report that it is Now Discoverable. Put the iPhone down near your Asterisk server. Now jump back to your Asterisk server console and issue the following command. Try it several times until you get the MAC address of your iPhone's Bluetooth Adapter:

hcitool scan

The system will whir away for a few seconds and then will report back the Bluetooth MAC address and name assigned to the adapter. It's probably your name. Write both of them down. We'll need the MAC address in a minute.

[root@asterisk1 ~]# hcitool scan
Scanning ...
    00:1D:64:C9:58:BA      Ward Mundy

Proximity Detection Design. Now we've got all the hardware information we need to make proximity detection work. We'll download the Proximity Detection Software in a minute.But first, sit down with a pencil and write down the other information you'll need to configure the Proximity Detection Software. To make the software as flexibile as possible, we've reworked the code a bit since the original article. With the new code, it's possible to manage multiple extensions of multiple people with multiple iPhones. So what you'll need is the extension numbers of the people that want to use this and the iPhone numbers of those people. For example, you may want to forward extension 200 to 6782345678 and extension 202 to 6783456789. Just make sure that the forwarding numbers are in the correct format for the default outbound dialing rules on your Asterisk server. If your server expects numbers to always begin with a 9 or a 1, be sure to include it in the dial string, or the calls won't be completed when they are forwarded. Obviously, you'll also need the MAC address for each of your iPhone Bluetooth Adapters so just repeat the drill above with each iPhone until you have all of the addresses. Finally, you'll need to assign an 8-character (or less) name to each user. So make yourself a nice little chart:

WARD 00:1D:64:C9:58:BA 200 6782345678
MARY 00:2D:54:C9:59:AB 201 6783456789

Today's installment assumes you are using a single Asterisk server both for your phone system AND proximity detection. We're still working on a multi-server solution. The only drawback with the current design is that the iPhones need to be placed close to that server when you arrive at your home or office. You can experiment on the distance the iPhones can be away from the server. Different Bluetooth adapters and iPhones have slightly different ranges. The bottom line is you always want to leave the iPhones close enough to the server with the Bluetooth adapter so that the proximity detection works reliably all the time. And, yes, we know it would be nice to be able to do all of this using a VMware or Parallels build of TrixBox running on a Windows or Mac desktop. Then you could detect the iPhone at the actual desktop where you hang out most of the time. We're working on it, but we just couldn't get it done for today's article. Stay tuned!

Proximity Detection Software Installation. All that remains to be done is to download and configure the proximity detection script and then put it in motion on your Linux machine. Log into your Asterisk server as root and move to the /root directory to download and unzip the script:

cd /root
wget http://nerdvittles.com/trixbox123/proximity.zip
unzip proximity.zip
chmod +x proximity

If you're going to be setting up proximity detection for multiple people, just make copies of the proximity script, e.g. cp proximity proximity1. Then edit each of the scripts and fill in the data from the little chart you made: nano -w proximity

deviceuser=WARD
devicemac=00:4B:63:D5:62:AB
myextension=200
mycellphone=6783456789

Save your changes and exit the editor: Ctrl-X, Y, then Enter.

Setting Up the Crontab Jobs. The last step is to set up a crontab entry for each script so that it gets run once a minute during whatever hours each day you want to monitor your iPhone. While still logged in as root, edit /etc/crontab: nano -w /etc/crontab. Insert a line like the following at the bottom of the existing file. This code would monitor your iPhone from 6 a.m. to 9 p.m. each day. To monitor your phone 24 hours a day, replace 6-21 with an additional asterisk.

* 6-21 * * * root /root/proximity > /dev/null

Save your changes and exit the editor: Ctrl-X, Y, then Enter. Repeat the drill for the other phones you want to monitor substituting the correct script names, and you're done.

Incidentally, if you ever want to disable the Proximity Detection System, just edit the crontab file and comment out the lines you want to disable by inserting # at the beginning of the line(s). If it happens that your calls are still being forwarded to your iPhone, you can cancel the forwarding from any Asterisk extension by dialing *74. Enjoy!

Nerd Vittles Demo Hot Line (courtesy of les.net). You now can take a number of Nerd Vittles projects for a test drive... by phone! The current demos include (1) MailCall for Asterisk with password 1111 (retrieve your email by phone), (2) NewsClips for Asterisk (latest news headlines in dozens of categories), (3) Weather Forecasts by U.S. Airport Code, and (4) Weather Forecasts by U.S. ZIP Code. You're not prompted for #4 yet, but it does work! Just call our number (shown in the left margin) and take any or all of them for a spin. The sound quality may not be perfect due to performance limitations of our ancient Intel 386 demo machine. But the price is right.

Nerd Vittles Fan Club Map. Thanks for visiting! We hope you'll take a second and add yourself to our Frappr World Map. In making your entry, you can choose an icon: guy, gal, nerd, or geek. For those that don't know the difference in the last two, here's the best definition we've found: "a nerd is very similar to a geek, but with more RAM and a faster modem." We're always looking for the best BBQ joints on the planet. So, if you know of one, add it to the map while you're visiting as well.

Original text is here

www.sitename.com

  Add comment

Name: 
E-Mail: 
Comment: 
Enter code: