Do you have a land-line that is constantly barraged with unwanted robo, solicitation, and “hangup” calls, even though you’re on the do not call registries? This project uses a Raspberry Pi and an OBi110 voice-bridge to create an Automated Attendant (AA) to screen calls for you! The annoyance calls on my line have dropped from five or six/day (with many more during political “seasons”) to zero. This is a low-power, low-cost solution with extreme expansion possibilities, and it’s a fun way to gain experience with the Pi & the Asterisk PBX software. As an added bonus, my wife “really loves it; our home is more peaceful”.
Raspberry Pi and OBi110
You won’t need to dive deeply into Asterisk to create this project. However, should you find yourself intrigued, I’ve included references to help you along the way. Familiarity with text editors (nano, vim), routing, and your local Internet infrastructure (IP assignment) is required for configuring addresses on the Raspberry Pi and OBi110.
Basic Analog Phone with answering machine
Secretly, what we’re doing is turning your old analogue phone into a VoIP phone using the OBi110 (a voice bridge), so that calls can be handled digitally by the Raspberry Pi. The only external Internet service used is the network time protocol (NTP) since the Pi doesn’t have a real-time-clock built-in (though one can be added). A network connection isn’t essential, but it is convenient for configuration. See the entry on Running RasPBX without an Internet connection. With no Internet connection, a crossover network cable can be used to tie the Pi directly to the OBi110.
Configuration of the OBi110 can be done via the phone hand-set, but it’s (much) easier using the web interface.
Note: The configuration described in this post assumes that you have only a single phone or base-station on a POTS line. Once the call has been screened, only the phone physically connected to the OBi110 will ring-through. The answering machine and base-station pictured above has several wireless phones that ring from the base-station. The base-station takes messages. Caller-id on your phone is not assumed or necessary.
This is the call flow: Asterisk answers an incoming call immediately. The caller is then informed that they have reached a number that does NOT accept solicitations and they should “please hang up NOW”; “to connect, press 1”. If no number is pressed within 10 seconds, Asterisk hangs up. If any number other than “1” is pressed, Asterisk hangs up. Once “1” is pressed, the caller is informed to please wait, while “music on hold” is played. As this happens, Asterisk rings the handset. If the phone isn’t picked up within 45 seconds, Asterisk hangs up. It’s assumed that the phone’s answering machine will pick-up before then (all that in 8 lines of code).
By changing the Asterisk configuration, your computer and/or other internet (SIP) phones/services can be rung at the same time. Asterisk can also be configure to record messages and forward them via email or picked up remotely. White/Black (allow/deny) phone number lists can be created, as-well-as many, many other features (but that’s beyond the scope of this post).
Checking with my local phone company, they wanted $6/month for Caller-id, and another $6.00/month for call screening. Dropping these two options alone, the pay-back period for this project is about one year. Drawing less than 20 watts of power, at $0.11/kwh, the electrical burden is about $20.00/year ($1/year/watt).
The basic steps for this project are:
1) Acquire the parts
2) Load the Raspberry Pi Asterisk distribution onto a 4Gb SDCARD
3) Test and perform initial configuration of the Raspberry Pi/Asterisk
4) Test and configure the OBi110
5) Configure Asterisk to recognize the OBi110, and set-up the Attendant program
6) Verify the OBi110 and Asterisk server are working together
Step 1:
You’ll need a Raspberry Pi, power supply, micro-usb cable, 4Gb SDCARD, OBi110, a couple of Ethernet cables to tie the Pi & OBi110 into your network, an SDCARD reader/writer (very cute, easy to lose, and the “lanyard” broke right-off), 4-conductor telephone cables (one comes with the OBi110), and a power strip to plug the two devices into. Acquire these from your favourite vendors; I happen to like the folks at Adafruit. You might have to hunt around for the Pi’s, since they tend to sell-out quickly.
If you’re not familiar with the Raspberry Pi, power is provided via a micro-usb connector, and the SDCARD substitutes for a hard-disc (a small SSD drive). You won’t need video/keybd/mouse if you ssh-login to the Pi (sshd is enabled by default). If you’re not familiar with SSH, just google “SSH Clients”. If you want to-go crazy with video, the Pi has HDMI and NTSC video outputs. Since all I have are VGA monitors, I use a powered converter (very important). I had intermittent problems with an un-powered HDMI=>VGA adapter; the consensus on the web seems to be that they draw too much power from the Raspberry Pi’s HDMI port. In the /boot/config.txt file on the Pi, I only had to set sdtv_mode=0 for video to work.
Step 2:
First, download the Raspberry Pi Asterisk distribution from here. The install it into your 4Gb memory card using the SDCARD writer; instructions for writing can be found here. You will need 4Gb free on your local system to unzip it. Copying the image to the memory card could take up to half an hour. Linux tip: Use a blocksize of 1M (1 megabyte) when issuing the “dd” command:
sudo dd bs=1M if=raspbx-19-01-2013.img of=/dev/sdc (your file names will vary)
Then plug the SDCARD into your Raspberry Pi (underside of circuit board).
Step 3:
Now, connect the Pi to your network with an Ethernet cable. Then plug the usb-A end of the cable into the 5V 1A (or more) power supply, and plug the micro-usb end into the connector on the top of the Pi circuit board, located above the SDCARD. After a couple of minutes, “ssh in-to” the Pi (ssh root@raspbx). The root password is “raspberry”. At this point, if you have familiarity with Ubuntu or Debian Linux distributions, you should feel pretty comfortable. Next, configure a static IP address for your Pi; you will do the same for the OBi110 later so they can talk to each other, and you can talk to them. Next, set your timezone by running the command:
dpkg-reconfigure tzdata
Update to the latest version of Asterisk (and apply OS upgrades) by running:
raspbx-upgrade
This could take an hour or so (don’t forget to type “y” when you are asked to continue). Towards the end of the upgrade you’ll be asked to keep or upgrade a couple of files. I opted for the upgrade (“Y”). If the web server (apache2) fails to start after the upgrade, run the commands (as user root):
chown www-data /var/lock/apache2
echo ServerName $HOSTNAME > /etc/apache2/conf.d/fqdn
service apache2 start
Now, lets add a couple of programs that will allow sound files to be created & modified:
sudo apt-get install mpg123 sox
and because I prefer vi as my editor:
sudo apt-get install vim
Reboot your Pi to verify it’s IP and connectivity.
Step 4:
Plug in your OBi110 to power and the network. Initially, the OBi110 assumes a DHCP server is running, so find its assigned IP. The OBi110 admin guide covers how to get the current IP address using the phone. Then enter the address into your browser: “http://xxx.xxx.xxx.xxx”, where the X’s are the OBi110’s IP address. The default user name and password is “admin”.
NOTE: As changes are made on each page to the OBi110, they must be saved by pressing the “submit” button on the bottom of each page BEFORE navigating to another page. Once all the pages have been configured, press the “Reboot” button in the upper right corner to activate the changes. Any-time changes are made, the OBi110 must be “Rebooted” to put them into effect.
Now (as a debug measure), plug your phone and POTS line into the OBi110. You should be able to make and receive calls as-if the OBi110 wasn’t present. If this is not true, then do-not proceed. Check the OBi110 FAQ or the Administration Guide.
A couple of good sites on the OBi110 are the quick-start guide, and configuring an OBi110 as an FXO gateway (FreePBX guide). Follow step 1 in “configuring an IBi110” (go ahead and set your local time zone on the same page). Then as you start into step 2, “YOURPBXIPADDRESS” is the static IP assigned to your Pi (the PBX); then change the AuthPassword to be something else besides “FXOPASSWORD“; set the URI to: OBITRUNK1@xxx.xxx.xxx.xxx, where “xxx…” is the IP address of the Pi. Be sure to press “Submit” before moving to the Line Interfaces page.
Go ahead and set InboundCallRoute (your phone #) as show, and RingDelay. If you don’t have caller-id, set RingDelay to 0 (zero). You don’t set any more parameters on the Line Port (setting the DialDigit[On/Off] times lower caused my phone to mis-dial).
NOTE: I added “ph,” in front of the “SP2(xxxxxxxxxx)” (don’t include the quotes, but the comma is important) so that both the phone and the PBX are “rung” at the same time; so in case the PBX doesn’t answer, a call won’t be missed (trusting, eh?). Don’t forget to “Submit” each page before going to the next one.
Next, we need to configure “Side A” of the OBi110; we’re not going to be using it to communicate with Google Talk, etc, like many other blog posts describe, instead we’re going to let the Pi “call out” to the telephone.
Under Service Providers => ITSP Profile A => SIP, set ProxyServer to the Pi’s IP address, check X_UseRefer, enter the Pi’s IP address into X_AccessList, and press “Submit”.
Under Voice Services => SP1 Service, set AuthUserName to PHONE, and enter a password you’ll remember in AuthPassword. In URI, enter PHONE@xxx.xxx.xxx.xxx (xxx.. is the Pi’s IP address), set MaxSessions to 5, check MWIEnable and X_VMWIEnable, then uncheck MessageWaiting, but don’t put anything in the field (don’t forget to “Submit”). At this point, you can “Reboot” the OBi110.
Step 5:
Grab the no-solicitors sound file (or use your own), create a custom sounds directory on the Pi, copy the file into the custom sound directory, and set the proper permissions (on the Pi):
mkdir /var/lib/asterisk/sounds/en/custom/
(from an outside shell) scp no-solicitors.wav root@xxx.xxx.xxx.xxx: (IP address of your Pi – and enter the root password – “raspberry”)
mv /root/no-solicitors.wav /var/lib/asterisk/sounds/en/custom/
chown -R asterisk.asterisk /var/lib/asterisk/sounds/en/custom/
Note: The “scp” program copies the .wav file to “/root” on the Pi from another computer where the file is located.
We’re almost there! Now we just have to update a couple of Asterisk configuration files located in the directory /etc/asterisk.
At the end of the file “/etc/asterisk/sip.conf“, add:
[OBi110](!)
type=friend ; Friends place calls and receive calls
context=from-trunk
secret= (The AuthPassword used for SP1 Service in the OBi110)
host=dynamic ; This peer register with us
dtmfmode=rfc2833 ; Choices are inband, rfc2833, or info
defaultuser=Home ; Username to use in INVITE until peer registers
isallow=all
allow=ulaw ; dtmfmode=inband only works with ulaw or alaw!
[PHONE](OBi110) ; Create an extension
[OBITRUNK1](!)
disallow=all
username=OBITRUNK1
secret= (The AuthPassword used for SP2 Service in the OBi110)
host=dynamic
type=friend
context=from-trunk
qualify=yes
dtmfmode=rfc2833
canreinvite=no
allow=ulaw
[OBITRUNK1](OBITRUNK1)
[xxxxxxxxxx](OBITRUNK1)
(Where “xxxxxxxxxx” is your phone number used for the InboundCallRoute parameter in the Physical Interfaces => LINE Port of the OBi110)
At the end of the file: “/etc/asterisk/extensions.conf“, add the actual working code:
[from-trunk]
; Answer and wait a second (1000ms) before continuing.
exten => xxxxxxxxxx,1,Answer( 1000 )
; Zap robo callers - uncomment the following line if desired.
; same => n,Zapateller() ; Send "number no longer in service" tones
; Play "no-solicitors" audio file & get a single digit touch-tone input
same => n,Read(digit,custom/no-solicitors,1) ; Get a digit
; same => n,SayDigits(${digit}) ; debug
; same => n,Goto(bye) ; debug
same => n,GotoIf($["${digit}" != "1"]?bye) ; verify digit
same => n,Playback(one-moment-please)
; Forward call to handset with music while waiting for pick-up (up to 45s)
same => n,Dial(SIP/PHONE, 45, m)
; If we time-out or the wrong digit is pressed, politely exit.
same => n(bye),Playback(vm-goodbye)
; Wait a second so that the text can finish getting out, then hang-up
same => n,Wait(1)
same => n,Hangup()
(Again, where “xxxxxxxxxx” is your phone number used for the InboundCallRoute parameter in the Physical Interfaces => LINE Port of the OBi110)
Then restart asterisk using the command:
service freepbx restart
Step 6:
Be sure the phone-line from the telephone company is plugged-into the OBi110, as-well-as your phone handset. While logged-into the Pi, run the command:
asterisk -rvvvvv
This will bring up the command-line interface, in verbose mode (level 5). When a call is made to your number, it should appear as lines in the command-line interface. Type “help” to see possible commands, and “quit” to exit. When a call is made, the asterisk command line-output should look something like:
== Using SIP RTP TOS bits 184
== Using SIP RTP CoS mark 5
-- Executing [xxxxxxxxxx@from-trunk:1] Answer("SIP/OBITRUNK1-00000009", " 1000 ") in new stack
-- Executing [xxxxxxxxxx@from-trunk:2] Read("SIP/OBITRUNK1-00000009", "digit,custom/no-solicitors,1") in new stack
-- Accepting a maximum of 1 digits.
-- <SIP/OBITRUNK1-00000009> Playing 'custom/no-solicitors.slin' (language 'en')
(and so on). If you get nothing, and the phone just rings through, then chances are very good that the OBi110 has not been configured properly. Note: other messages will indicate improper password, etc., so pay close attention to the diagnostic output. Go back and check the OBi110 configurations, be sure that they were all saved properly, then reboot the OBi110, and try again. Also, be sure you can “ping” each device from another computer on the network.
Last, but not least, is the issue of security. I haven’t touched on this before, as I assume that your system is behind a firewall and no “bad-guys” have access to it. VoIP Info has a page addressing issues, Digium has a page also. The biggest vulnerability is someone accessing your PBX from the Internet and making “expensive” calls.
Tons of documentation about the hardware & software is available just by “googling”. Since the Raspberry Pi is open-source hardware, the schematics can be found here.
Digium created and maintains the source code (available here), and the best references are the books create by the Authors of the code themselves: “Asterisk: The definitive guide (3rd edition)“, and a pre-release of the 4th edition.
Asterisk includes a wonderful collection of voice snippets from Allison Smith, the IVR Voice. They reside in the directory /var/lib/asterisk/sounds. Asterisk for the Raspberry Pi is also bundled with FreePBX; feel free to explore this web-based interface for Asterisk.
This project barely touches the capabilities of Asterisk. Look around the web. See what kinds of projects Asterisk (and FreePBX) are being used for. I think you’ll be amazed.
UPDATE: Be sure to power-down your Pi before removing power (“halt -p”) and wait for a single red LED indication. I did some shifting of power cables, and caused unrecoverable (EXT-4) file-system damage (good thing I had a back-up SDCARD). Putting it on a UPS is probably a good idea.