Tuesday, March 15, 2011

HOWTO: Use Google and Asterisk For Free Home Telephone Service

Recently I have been playing around with free VOIP solutions on my cellphone, and they were pretty neat.  However, they had a lot of jitter, dropped calls, and the occasional connection problem.  I wanted something truly stable with excellent call quality for work besides my cellphone.  Well, I have seen the light and found the answer, and it is a system that uses google talk (what you use to make phone calls from gchat) and google voice.  When it is done, you won't know the difference from your old telco, and it will be free.


BEFORE YOU START:

INGREDIENTS:
1 Linux server (it can be old and not-so powerful)
1 ATA (analog telephone adapter)*
1 or more phone handsets**
1 router with 2 free ports (for your Asterisk server and your analog telephone adapter)
1 google voice account that you don't typically log into for gmail or chatting

*For purposes of this guide, I suggest using the Grandstream HandyTone HT286

**I didn't have a home phone before I did this so I snagged a 3-handset answering system which connect through only one unit (as many these days do) and thus avoid any extra work inherent in connecting multiple phones through your existing home phone wiring.

WHAT YOU WILL GET:
When you are done, you will have a phone that acts like a phone and can call any number in the US and Canda for free or international numbers for pocket change.  You will receive calls to your google voice number on your phone.  You will get incoming phone numbers in your caller id, but you won't get names without a little extra work.  This is because the phone company provides this to you by querying their database.

You can take some extra steps so your Asterisk server has its own caller id database built from your google contacts, but that is outside the scope of this guide.  Many modern handsets (like mine) can check your stored contacts and show the associated names when those people call, so this wasn't a big enough deal to warrant the extra work for me.

WHAT YOU WON'T GET:
911 service!  Also, there is no guarantee Google will keep this free forever.  You also don't get some kludged-together solution where you have to use NAT-traversal or punch holes in your firewall with open ports since this uses native capabilities of Asterisk.  Because this method uses Asterisk by itself, you don't need FreePBX, PIAF (PBX In A Flash), or any other unnecessary extra software.


THE BIG PICTURE (literally):


HOWTO:

1.Set up Asterisk (AKA, the bulk of the work):

Make sure you have a Debian or Ubuntu computer with a static IP address.  You can set up a static IP in /etc/network/interfaces.  I would guess you can use Red Hat-like distros, but some of the locations, required packages, and commands may be a bit different so only go this route if you know what you are doing.   Also, stick to the darn guide.  If you try to get clever and just use the Asterisk package for your distro, it will be horribly outdated and not contain the functionality to do what you want.  You really need to compile it yourself until Asterisk 1.8+ is included in your Linux distribution.

1A.On your Linux box, follow the guide here but don't do step 5 or 6 since those are changes for devices with ARM processors, and I assume you are just using a standard non-ARM computer here.  Also, you don't have to install the second list of packages for FreePBX since you won't need it.  Note that your packages may be slightly different versions than those listed.  For example, on my Debian Squeeze system I found that the package libmysqlclient15-dev was now version 16.  Also, the forum thread in the next step (1B) talks about configuring only the minimum modules needed to run Asterisk, so you may want to reference it later if you need to tune Asterisk for very crappy hardware, but don't worry about that until you get it working.

1B.Next, go to this post and do steps 2-5 since you already did step 1. If you have a different ATA (analog telephone adapter) from what I suggested, then you will need to enter the configuration information as shown in step 6 and configure it on your own.

If you don't see a listing in Google Voice's Settings->Phones for your Google Chat account, then you need to first fire up gmail and dial a call with Google chat.  It should show up then.  

1C.After you finish up that guide, you will need to make some quick additional changes to conquer two issues I encountered.  As root, open /etc/asterisk/extensions.conf and add the following lines just after "exten => you@gmail.com, n,Set(CALLERID(name)=${DB(cidname/${CALLERID(num)})})".

Add these lines just after that line (with "you" replaced with your username):


Then restart your Asterisk server as root by executing /etc/init.d/asterisk restart.

The first new line rips the + sign off of incoming numbers so your phone's callerid will be able to handle them properly.  For example, my handset would show the number, but I couldn't return a call to a missed incoming call from my call log menu on the handset because it didn't recognize the +1-555-5555 format as a valid phone number.  This fixes such issues.

The other three lines let Asterisk pick up the phone while it is ringing, make it wait a very brief period, and then it sends a "1" tone to Google.  If you don't do something like this, incoming calls will be preceded by a message saying "Please press 1 to accept the call..." even if all such options are disabled on google voice.  Worse, pressing 1 didn't work for answering these calls.  If you don't want your phone to pick up calls when you don't answer, then you will need to do something a little different by having asterisk send 1 when you pick up a line based on the other method described here.

2. ATA Setup
2A.Grab your shiny new Handytone box, and connect it to power, ethernet, and your phone.  The manual for it is found here.

Pick up your handset and dial ***.  Then dial "01" to hear your IP address mode and 9 to toggle between dynamic and static.  I highly recommend a static IP.  After toggling to static, hit * to hear a readout of the Handytone's current IP address.   You can enter in a new one by typing 12 digits on the keypad (including any zero values).  You may need to reboot it after that.  You can do so by typing #, then 99, then 9.  After that, you can use a web browser on a computer connected to your router to access the web interface at the IP address you specified.

2B.On the web interface, type the default password "admin".

Click "basic settings" and enter an end-user password other than the default "123", then set the static configuration options making sure the IP is set to what you requested, and that subnet mask matches your router's (usually 255.255.255.0), and default router and DNS settings match your router's IP address for most users.  Also, set up your time info, and US folks in areas with DST should replace the Optional Rule with "3,2,7,2,0;11,1,7,2,0;60" so it follows the newer DST rules instead of the old ones preloaded on the device.  This will matter if you have a handset that resets its display time from the incoming line when you receive a call (as mine does).  Click "Update" when finished.



Click "advanced settings 1" and enter a new admin password in the first box (which you will use to login to the web interface in the future), your Asterix server's IP in the second and third boxes, and "101" in boxes 4 and 5.  If any STUN server options are turned on, deactivate them.  You can also fool around with the codec choices if you like.  I believe that the highest numbered G### options are the better ones.  Then click update (leaving all other settings alone on this page), and then click reboot.


No changes are needed on Advanced Settings 2.  If all goes well, after the reboot you can click status and see "Registered: Yes", and your Handytone won't flash red through the button on top intermittently.  Now make some calls and get some calls to try it out.  For me it is rock-solid stable even for 1hour+ teleconferences, and my Handy-tone is even plugged into a wireless network bridge since I was out of router ports.

After writing this, I figured out how to set your home area code as described here so you can dial local numbers with only seven digits.  Feel free to leave some comments praising me or asking for help. 

24 comments:

m-p-3 said...

http://redd.it/g5bbn

Roman said...

could you explain why asterisk is needed? can't you point ata directly to gv?

ZeroCool42 said...

I too would very much like to know the answer to Roman's question if you would be so kind :).

Also, what required features does the HandyTone provide when used in conjunction with the Asterix server? Is is possible to use a product such as this: http://www.amazon.co.uk/Socket-RJ45-Secondary-Telephone-Adapter/dp/B0021JNY38/ref=sr_1_3?ie=UTF8&qid=1300461112&sr=1-3 to achieve the same result?

Unknown said...

"1 google voice account that you don't typically log into for gmail or chatting"

Why does it have to be an otherwise unused account?

HeadSheez said...

Roman,
The reason is that your gtalk account does not provide a standard SIP line. Asterisk logs in to your gtalk account and pretends like it is a computer using gtalk as far as google knows, and it pretends like it is connected to a SIP line as far as your ATA+phone or IP phone knows. There is an ATA device (obihai obi110) that can do all this internally, but it is not common.

David,
I'm not sure, but I don't think that would work. The ATA has to have onboard functionality to talk to your Asterisk system being used as a SIP proxy and handle interaction between your phone and the box. For example, your ethernet and router have no idea what it means when you press "2" on a phone wired via ethernet. The box has to handle this interaction. A typical ATA+phone or IP phone is what you need.

Unununium,
I suggest an unused account because it logs into gtalk/gchat (and even posts a status message about Aterisk). It may cause problems for it or you when both are logged in simultaneously. I haven't had any problems with it myself, but it is probably a good precaution.

ThantiK said...

Love you for this. Amazing writeup. I'm going to be doing this but instead of an analog adapter, plugging the information into the new SIP capabilities of Gingerbread on my Android :)

Free cell phone calls! :)

Brad said...

Is it possible to set this up with 2 or more google voice numbers to have multiple lines? I am also interested in setting it up so if a client calls into my business number it picks up right away and can play a prerecorded message and music while "finding me" or basically forwarding to my cell.

Anonymous said...

You got this from Maximum PC

HeadSheez said...

Brad,
You can set up 2 lines using 2 separate google accounts and then assigning the lines to separate extensions. I recall googling and finding some posts on setting up your extensions.conf file to handle this. I think that gtalk allows for more than one line on a single gtalk account and the obihai obi110 device can handle this somehow, but I have no idea how to do this on Asterisk.

Anon,
This guide is actually a bit different than the MaximumPC one. For one thing, I am not pushing any front end or pbx-distro, and I give a specific guide about how to set up a specific ATA. Also, the majority of the guide is content on forum posts other people made. I specifically did not want to use PBX In A Flash, FreePBX, etc. I wanted to do it natively in Asterisk so I could get my hands a little dirty and learn something about it.

Anonymous said...

This looks interesting. Do you think this would also work with a Cisco Call Manager router instead of asterik and a PBX?

HeadSheez said...

Anon,
I doubt it. The obihai is the only closed box I have seen that can do it, and Cisco is notorious for intentionally limiting their products for lock-in purposes.

ioan said...

Can Asterisk run on a router with openwrt installed? Here is a link on how to install Asterisk on openwrt: http://lestblood.imagodirt.net/archives/83-Asterisk-on-OpenWRT.html

TSR said...

If you want a simpler way of getting the "Press 1 to answer..." prompt to work add the line

dtmfmode=inband

to sip.conf. This will also allow the voice menus of a business you call to work, which didn't for me under your setup.

HeadSheez said...

TSR.
Thank you for that helpful info. I did this in my sip.conf as well now, and it works great.

Anonymous said...

Thanks! I did struggle until I noticed a setting on my grandstream box- I had to switch NAT transversal from "Yes, STUN server is..." to "No"

For a guy who didn't know squat about Ubuntu- you've done VERY well with your instructions paired with the others that you referenced. thanks!

Unknown said...

I got this setup and can dial out fine. When I receive a call, I get Google Voice asking me to press 1 to accept the call, and no matter what I hit it won't allow the connection. I don't have any other devices tied to this voice account, is that required? Can you point me to something to resolve this?

dbray925 said...

Thank you so much for this article. Worked great on Ubuntu 10.04. To let other readers know, you can simply use this PPA for Asterisk, and it still works perfectly fine. No need to compile from source: https://wiki.asterisk.org/wiki/display/AST/Asterisk+Packages#AsteriskPackages-APT%28Debian%2FUbuntu%29

Robert said...

I've gotten to the end and have Registered: Yes Unfortunately, when calling out I get a busy tone no matter the number. When calling in the phone does not ring and I have to leave a message on google voice. Any ideas?

l3ioHazardX said...

everything works great thanks for the guide. I was wondering how to get voicemail working. When I set up asterisk to handle the voicemail it goes back to the googlevoice voicemail. How I want it to work is to use asterisk as the voicemail service so I can check it from the phone.

Jeremy said...

I was getting the following error in my /var/log/asterisk/messages file and in the asterisk console.

db.c: Unable to open Asterisk database '/usr/lib/asterisk/astdb': Permission denied

I don't know if anyone else has run into this problem, but changing the group on the /usr/lib/asterisk folder to asterisk fixed it for me.

Unknown said...

I followed all the instructions, the Grandstream is registered and I doubled checked all the instructions but when I try to dial a number I hear a dial tone, dial the 1-xxx-xxx-xxxx number and I don't hear anything. No ringer, no busy tone, etc. What did I do wrong? I'm sure I'm close but can't figure out why it won't dial out. Any help would be greatly appreciated!

Unknown said...

Alright, so I did some of my own troubleshooting and went back through the instructions again. I noticed using my Debian 6.0.2.1 install disk and software manager I was only using Asterisk 1.6 so I upgraded to 1.8. By the time it was installed and the .conf files were updated again it worked like a charm! Very excited to have free home phone service using an old linux box and the ATA part you suggested. Thank you very much for the tutorial. Well done.

md said...

I followed this excellent guide and set up my asterisk server working great for calling US numbers and receiving calls. But I always got busy tone when I dial international numbers. How should I change the settings? In addition, I noticed that after I dial a number, there is a ~8 seconds silence before I hear a ringer tone. Can we shorten this? Thanks

Anonymous said...

How could this be implemented into an home alarm system?