More playing with Asterisk

I have been playing some more with Asterisk and I’ve got 2 useful bits to share.

MP3 Voicemail

The simple one first, a recent update to Android (not sure what exactly) means it won’t playback wav files attached to emails. This is a problem as when Asterisk records a voicemail it can be configured to email the recording in wav to mailbox owner. A little bit of googling turned this up http://bernaerts.dyndns.org/linux/179-asterisk-voicemail-mp3. It needed updating a little to get it to work on my Raspberry Pi.

First I needed to change one of the sed line to match WAV not wav to fix the file name from this:

...| sed 's/.wav/.mp3/g' > stream.part3.mp3.head

to this:

...| sed 's/.WAV/.mp3/g' > stream.part3.mp3.head

Secondly lame doesn’t like the encoding for the wav file (I think it’s because the stored values are unsigned) so we need to run it through sox to fix it first.

sox stream.part3.wav -e signed stream.part3a.wav
lame -m m -b 24 stream.part3a.wav stream.part3.mp3

This not only makes it so I can listen to the messages on my phone and tablet it also makes the mails smaller so they take up less bandwidth.

Using a 3G Stick to make calls

Having used OBi110 to hook my Asterisk VoIP rig up to a standard phone line I was looking for a way to hook a mobile phone to the system. There are 2 options with Asterisk, chan_bluetooth and chan_dongle.

Chan_bluetooth uses a bluetooth connection to a mobile phone to make and receive calls. I had a look at this but it meant keeping phone plugged into a charger and having another bluetooth adapter plugged in.

Chan_dongle work with certain Huawei 3G USB modems. These 3G sticks are basically full phones with a USB interface. I’ve already been using one of listed modems for my SMS/MMS project and I had a spare one kicking around. It needed the firmware updating to make it work, which was a bit of a challenge as it required setting up a real Windows machine as I couldn’t get it to work in a VM.

Setting up the dongle was a little tricky at first as I couldn’t get a set of udev rules to match the stick properly to ensure it always ends up with the same device names. The code does let you specify the stick using it’s IMEI which helps if you have multiple sticks plugged into the same computer.

Once configured it was easy to set up the extensions.conf to allow making and receiving calls. The main reason for setting this up was to have a portable VoIP rig that I can take to different places and not have to worry about a fixed phone line. There is a upcoming hackday that I have a plan for.

Playing with Asterisk PBX

I’ve been meaning to get back and have a proper play with Asterisk again for a while. Last week Amazon sent me one of those emails about things you’ve looked at but not bought and I spotted this:

It was down from £60 to £35 so I did exactly what they wanted and bought one.

Now normally I don’t use my land line at all, it’s just there to let the internets in, it doesn’t even have a handset plugged in. But there are a few little projects kicking around the back of my mind I’ve been thinking about for a while and the OBi110 should let me play with them.

The first is to see if the (unused, never given to anybody but my ISP to set up the conection) number for the land line has ended up on any lists for scamers/spammers and people generally trying to sell me stuff. My mobile gets at least 1 call a week about payment protection and the like and even my work office number has started getting recorded calls about getting my boiler replaced.

I could have probably just used the call log on the OBi110 but I wanted to be able to potentially record these calls and a few other things so I needed something a little smarter which is were Asterisk comes in. Asterisk is a opensource VoIP PBX this basically means it acts like a telephone exchange for calls made over the internet. I’ve seen people run Asterisk on the old Linksys Slugs so I was sure it should run fine on a Raspberry Pi as long as it wasn’t dealing with too many calls and not doing much codex transcoding. As I already had a Pi running my SMS/MMS rig it seamed like a good place to put all my telephone stuff.

Installing Asterisk on the Pi was just a case of running apt-get install asterisk. It comes with a bunch of default config files (in /etc/asterisk), but there are 2 main ones that I needed to change to make some simple things work.

sip.conf
This file is where you can configure what clients can connect to your asterisk instance via the SIP protocol. To start with I’m going to set up 2 different clients, one for a softphone running on my laptop and one for the OBi110. It sets up few things, but the import bit for later is the context which controls which bit of the extentions.conf file we jump to when receiving a call from each client.

[general]
context=local
srvlookup=yes

[softphone]
defaultuser=softphone
type=friend
secret=password123
qualify=no
nat=no
host=dynamic
canreinvite=no
context=local
disallow=all ; only the sensible codecs
allow=ulaw
allow=alaw
allow=gsm

[obihai]
defaultuser=obihai
type=friend
secret=password123
qualify=yes
dtmfmode=rfc2833
canreinvite=no
context=external
disallow=all
allow=ulaw

extensions.conf
This file defines how Asterisk should handle calls, it has two contexts called local and external. The local context defines 2 paths, the first for extension 100, when this number is called from the softphone Asterisk calls out to a small python program called agi-mqtt which publishes a JSON object to the calls/local MQTT topic which contains all the information Asterisk has about the call. It then answers the call then plays audio file containing HelloWorld and finally hangs the call up. I’m mainly using this local context to testing things out before copying them over to the external context.

The second path through the local context uses a special case extension number “_0Z.”, this matches any number that starts with 0[1-9] (so won’t match against 100). This path forwards the dialed number on to the OBi110 to place the call via the PSTN line.

The external context only contains 1 path which matches the phone number of the PSTN line and currently matches the 100 extension (play HelloWorld). At some point later I’ll setup this path to forward calls to a local softphone or forward to a voicemail account.

[local]
exten => _0Z.,1,AGI(/opt/asterisk/agi-mqtt/mqtt,/opt/asterisk/agi-mqtt/mqtt.cfg,calls/local)
exten => _0Z.,2,Dial(SIP/${EXTEN}@obihai);
exten => _0Z.,3,Congestion()
exten => _0Z.,103,Congestion()
exten => t,1,Hangup()

exten => 100,1,AGI(/opt/asterisk/agi-mqtt/mqtt,/opt/asterisk/agi-mqtt/mqtt.cfg,calls/local)
exten => 100,2,Answer()
exten => 100,3,Playback(en_US/hello-world)
exten => 100,4,Hangup()

[inbound]

exten => 0123456789,1,AGI(/opt/asterisk/agi-mqtt/mqtt,/opt/asterisk/agi-mqtt/mqtt.cfg,calls/pstn-in)
exten => 0123456789,2,Answer()
exten => 0123456789,3,Playback(en_US/hello-world)
exten => 0123456789,4,Hangup()

Now Asterisk is all working properly I setup the OBi110 using the instructions found here.

After a bit of playing I have inbound and outbound calls working and some MQTT enabled logging. Next up is looking at using the SIP Client built into Android to allow calls to be made and received from my mobile phone.