[How To] install and configure a full Voip server (Asterisk 13) on OpenWrt

In this tutorial, I'm going to show you how to install and fully configure Asterisk 13 Voip server on OpenWRT 18.xx.xx, I commented out all parts that need to be modified with your actual configuration data.

  • Features: SIP channels, Jingle/XMPP client channel, GSM and SMS channel (chan_dongle), Blacklist, IVR (interactive voice reponse), Call-back, Wakeup call, Voicemail, Voicemail messages to email, Chanspy, Ring groups, Call diversion, Call Forwarding, Music on hold, shell script execution from a phone call (IVR choice) or from a SMS or XMPP message received (on part three of this how-to).

  • References
    my previous Asterisk_11_on_OpenWrt_14.04 guide: https://forum.archive.openwrt.org/viewtopic.php?id=53874&p=1#p253590
    Baresip Openwrt (14.04) SIP client: see previous link at post 4
    chan_dongle project: https://github.com/bg111/asterisk-chan-dongle

Important NOTE: USB external /overlay is needed, minimum 256 Mbytes of free space available is needed
Minimum of 64 Mbytes RAM is needed for full features server, CPU 400 Mhz or better

First part: packages installation

  • Prerequisites:
opkg update 
opkg install nano
opkg install zram-swap
opkg install msmtp
opkg install sox lame mpg123 # optional
  • mailsend SMTP configuration (gmail):

nano /etc/msmtprc

account default
host smtp.gmail.com
port 587
auth on
user usename@gmail.com
password your-gmail-password
auto_from off
from username@gmail.com
tls on
tls_starttls on
tls_certcheck off
logfile
syslog LOG_MAIL
  • create install script:

nano /root/asterisk-install.sh

insert the following lines:

opkg update
opkg install asterisk13 asterisk13-app-authenticate asterisk13-app-chanisavail asterisk13-app-chanspy asterisk13-app-confbridge asterisk13-app-controlplayback asterisk13-app-disa asterisk13-app-dumpchan asterisk13-app-exec asterisk13-app-followme asterisk13-app-ices asterisk13-app-ivrdemo asterisk13-app-minivm asterisk13-app-mixmonitor asterisk13-app-mp3 asterisk13-app-originate asterisk13-app-playtones asterisk13-app-read asterisk13-app-readexten asterisk13-app-record asterisk13-app-saycounted asterisk13-app-sayunixtime asterisk13-app-senddtmf asterisk13-app-sendtext asterisk13-app-sms asterisk13-app-stack asterisk13-app-system asterisk13-app-transfer asterisk13-app-url asterisk13-app-userevent asterisk13-app-verbose asterisk13-app-waitforring asterisk13-app-waitforsilence asterisk13-app-waituntil asterisk13-app-while asterisk13-app-zapateller asterisk13-bridge-builtin-features asterisk13-bridge-holding asterisk13-bridge-native-rtp asterisk13-bridge-simple asterisk13-bridge-softmix asterisk13-cdr asterisk13-cel-custom asterisk13-cel-manager asterisk13-chan-alsa asterisk13-chan-bridge-media asterisk13-chan-console asterisk13-chan-dongle asterisk13-chan-mobile asterisk13-chan-motif asterisk13-chan-oss asterisk13-chan-phone asterisk13-chan-rtp asterisk13-chan-sip asterisk13-codec-a-mu asterisk13-codec-alaw asterisk13-codec-g722 asterisk13-codec-gsm asterisk13-codec-lpc10 asterisk13-codec-resample asterisk13-codec-ulaw asterisk13-curl asterisk13-format-g723 asterisk13-format-g726 asterisk13-format-gsm asterisk13-format-ilbc asterisk13-format-ogg-vorbis asterisk13-format-pcm asterisk13-format-siren14 asterisk13-format-siren7 asterisk13-format-sln asterisk13-format-wav asterisk13-format-wav-gsm asterisk13-func-base64 asterisk13-func-blacklist asterisk13-func-callcompletion asterisk13-func-channel asterisk13-func-config asterisk13-func-cut asterisk13-func-dialgroup asterisk13-func-dialplan asterisk13-func-groupcount asterisk13-func-hangupcause asterisk13-func-holdintercept asterisk13-func-iconv asterisk13-func-jitterbuffer asterisk13-func-module asterisk13-func-periodic-hook asterisk13-func-pitchshift asterisk13-func-shell asterisk13-func-sysinfo asterisk13-func-talkdetect asterisk13-func-uri asterisk13-func-version asterisk13-func-vmcount asterisk13-func-volume asterisk13-pbx-loopback asterisk13-pbx-spool asterisk13-pjsip asterisk13-res-adsi asterisk13-res-agi asterisk13-res-clialiases asterisk13-res-clioriginate asterisk13-res-convert asterisk13-res-hep asterisk13-res-hep-pjsip asterisk13-res-hep-rtcp asterisk13-res-limit asterisk13-res-manager-presencestate asterisk13-res-monitor asterisk13-res-musiconhold asterisk13-res-mutestream asterisk13-res-parking asterisk13-res-phoneprov asterisk13-res-pjproject asterisk13-res-pjsip-phoneprov asterisk13-res-realtime asterisk13-res-rtp-asterisk asterisk13-res-rtp-multicast asterisk13-res-security-log asterisk13-res-smdi asterisk13-res-sorcery asterisk13-res-speech asterisk13-res-srtp asterisk13-res-timing-timerfd asterisk13-res-xmpp asterisk13-sounds asterisk13-util-smsq asterisk13-util-streamplayer asterisk13-voicemail

  • give the right permissions to the script:

chmod 755 /root/asterisk-install.sh

  • launch the script and wait packages installation to be completed:

sh /root/asterisk-install.sh

  • when finished reboot the router
2 Likes

Part Two A: configuration files (overwrite the default configuration files)

nano /etc/asterisk/asterisk.conf

[directories](!)
astetcdir => /etc/asterisk
astmoddir => /usr/lib/asterisk/modules
astvarlibdir => /usr/lib/asterisk
astdbdir => /usr/lib/asterisk
astkeydir => /usr/lib/asterisk
astdatadir => /usr/lib/asterisk
astagidir => /usr/lib/asterisk/agi-bin
astspooldir => /root/spool/asterisk
astrundir => /var/run/asterisk
astlogdir => /root/spool/asterisk
astsbindir => /usr/sbin

[options]

verbose = 5
debug = 5
execincludes = yes
languageprefix = yes
maxcalls = 10
transmit_silence = yes
defaultlanguage = en
documentation_language = en_US
hideconnect = yes
;internal_timing = yes

[compat]
pbx_realtime=1.6
res_agi=1.6
app_set=1.6

nano /etc/asterisk/modules.conf

[modules]

autoload=yes
noload => res_timing_pthread.so
noload => pbx_gtkconsole.so
load => res_musiconhold.so
noload => chan_alsa.so
noload => chan_console.so
noload => pbx_ael.so
load => pbx_spool.so
noload => chan_motif.so
noload => res_timing_timerfd.so
load => chan_dongle.so
noload => chan_iax2.so
load => res_xmpp.so

nano /etc/asterisk/dongle.conf

[general]

interval=15

[defaults]

context=from-pstn
group=1
rxgain=5
txgain=-3
autodeletesms=yes
resetdongle=yes
u2diag=-1
usecallingpres=yes
callingpres=allowed_passed_screen
disablesms=no
language=en
smsaspdu=yes
mindtmfgap=45
mindtmfduration=40
mindtmfinterval=200
callwaiting=auto
disable=no
initstate=start
exten=+123456789 ;; put here your phone number
dtmf=relax

[dongle0]
audio=/dev/ttyUSB1 ;; check /dev/ttyUSBx with DMESG 
data=/dev/ttyUSB2 ;; check /dev/ttyUSBx with DMESG

nano /etc/asterisk/xmpp.conf

[general]

[asterisk]
type=client
serverhost=your-server-address
username=username
secret=your-password
priority=25
port=5222
usetls=no
usesasl=no
status=available
statusmessage="Available"
timeout=15
keepalive=yes
sendtodialplan=yes ;; this will take send unsolicited incoming xmpp messages into the dialplan
context=incoming-xmpp ;; this sets the context those incoming messages are sent to
;; note: for xmpp server on OpenWrt see https://openwrt.org/docs/guide-user/services/xmpp.server

nano /etc/asterisk/voicemail.conf

[general]

mailcmd=/usr/bin/msmtp receiver@mail.provider.com

[voicemail]

240 => 1234,root,receiver@mail.provider.com,attach=yes
  • notes: 240 is the mailbox number, mailbox password is 1234

Part Two B: configuration files (overwrite the default configuration files)

nano /etc/asterisk/sip.conf

 [general]
 transport=udp
 bindport=5060
 bindaddr=0.0.0.0
 nat=yes
 language=en
 allowguest=no
 srvlookup=no
 disallow=all
 allow=ulaw
 allow=alaw
 allow=gsm
 dateformat=%F %T
 alwaysauthreject=yes
 localnet=192.168.1.0/255.255.255.0 ;; change this according to your LAN configuration
 localnet=127.0.0.0/255.255.255.0
 tcpbindaddr=0.0.0.0
 tcpenable=yes
 jbenable=yes
 jbforce=yes
 jbmaxsize=250
 jbimpl=adaptive
 jbtargetextra=40
 jblog=no
 
 [200] ; internal extension ; SIP phones must be connected to LAN
 user=200
 type=friend
 secret=password_change_me
 host=dynamic
 qualify=yes
 nat=yes
 insecure=invite,port
 context=from-internal
 
 [201] ; internal extension ; SIP phones must be connected to LAN
 user=201
 type=friend
 secret=password_change_me
 host=dynamic
 qualify=yes
 nat=yes
 insecure=invite,port
 context=from-internal
 
;;put here your SIP Voip provider configurations, remove comments (;) to enable after editing:
;;note: change 0123456789 with your voipnumber
;;this is a general example, see your providers guides for the actual configurations
 
;register => 0123456789:voippassword@voip.provider.address.com:5060/0123456789
 
;[0123456789]
;username=0123456789 
;type=friend
;srvlookup=yes
;secret=voippassword
;realm=voip.provider.address.com
;nat=yes
;qualify=yes
;dtmfmode=auto
;insecure=invite,port
;host=voip.provider.address.com
;fromuser=0123456789
;fromdomain=voip.provider.address.com

Part Two C: configuration files (overwrite the default configuration files)

  • basic extensions configuration (SIP, GSM and SMS)

nano /etc/asterisk/extensions.conf

[globals]

[general]
static=yes
writeprotect=yes
autofallthrough=yes

[from-internal]

;
;Chanspy Scanning
exten => 555,1,Chanspy(all,b)

;
;internal extension 200 - rings for 30 seconds
exten => 200,1,Dial(SIP/200,30)
exten => 200,2,Hangup()

;
;internal extension 201 - rings for 30 seconds
exten => 201,1,Dial(SIP/201,30)
exten => 201,2,Hangup()

;
; internal extension 98 voicemail management (password 1234 as per voicemail.conf)
exten => 98,1,Set(CHANNEL(language)=en)
exten => 98,n,VoiceMailMain(240@voicemail)
exten => 98,n,Wait(1)
exten => 98,n,Hangup()

;
; Ring group 600, ring all internal extensions for 30 seconds, if no one answers, the call goes to voicemail
exten => 600,1,Answer()
exten => 600,n,Set(CHANNEL(language)=en)
exten => 600,n,Ringing
exten => 600,n,Wait(1)
exten => 600,n,Dial(SIP/200&SIP/201,30)
exten => 600,n,Playback(vm-isunavail)
exten => 600,n,VoiceMail(240@voicemail)
exten => 600,n,Hangup()

; CALL OUT RULES ; see https://www.voip-info.org/asterisk-dialplan-patterns/
;
; GSM Dongle CALL OUT rules
exten => _1NXXNXXXXXX,1,Dial(dongle/dongle0/${EXTEN})
;
; SIP VOIP CALL OUT rules  (change 0123456789 with the voipnumber you have in sip.conf)
exten => _1NXXNXXXXXX,1,Dial(SIP/${EXTEN}@0123456789)


[default]

; SIP VOIP CALL IN rules (change 0123456789 with the voipnumber you have in sip.conf)
exten => 0123456789,1,Answer()
exten => 0123456789,n,Set(CHANNEL(language)=en)
exten => 0123456789,n,Ringing
exten => 0123456789,n,Wait(1)
exten => 0123456789,n,Dial(SIP/200&SIP/201,30,r)
exten => 0123456789,n,Playback(vm-isunavail)
exten => 0123456789,n,VoiceMail(240@voicemail)
exten => 0123456789,n,Hangup()

[from-pstn]

;
; GSM DONGLE CALL IN rules (change +123456789 with the exten number you have in dongle.conf)
exten => +123456789,1,NoOp(${CALLERID(NUM)})
exten => +123456789,n,Answer()
exten => +123456789,,n,Set(CHANNEL(language)=e
exten => +123456789,n,Ringing
exten => +123456789,n,Wait(1)
exten => +123456789,n,Dial(SIP/200&SIP/201,30)
exten => +123456789,n,Playback(vm-isunavail)
exten => +123456789,n,VoiceMail(240@voicemail)
exten => +123456789,n,Hangup()

;
; all SMS's received on GSM dongle will be forwarded by email (change username@gmail.com as per /etc/msmtprc)
exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})}) 
exten => sms,n,System(echo 'From: ${CALLERID(num)} <username@gmail.com>\nTo: <receiver@mail.provider.com>\nSubject:SMS received\nfrom: ${CALLERID(num)}\n${BASE64_DECODE(${SMS_BASE64})}' >> /tmp/sms.txt)
exten => sms,n,System(/usr/bin/msmtp username@gmail.com < /tmp/sms.txt &)
exten => sms,n,Wait(8)
exten => sms,n,System(/bin/rm -f /tmp/sms.txt) 
exten => sms,n,Wait(2)
exten => sms,n,Hangup() 
  • Post install asterisk configurations:

/etc/init.d/asterisk disable

nano /etc/rc.local

#
/bin/sleep 10
/etc/init.d/asterisk start &
/bin/sleep 3
#
exit 0

reboot the router

Now you have an Asterisk Voip server up and running on your router

  • Asterisk commands
 /etc/init.d/asterisk start/stop/restart
 asterisk -vvvvr # asterisk cli

Part three A: Shell script execution from a SIP voip call - IVR choice: 1 or 2

note: to be added to /etc/asterisk/extensions.conf under [default] section

; Shell script execution from a SIP voip call - IVR choice
; change 0123456789 with the voipnumber you have in sip.conf
;
exten => 0123456789,1,NoOp(${CALLERID})
exten => 0123456789,n,Answer()
exten => 0123456789,n,Ringing
exten => 0123456789,n,Set(TIMEOUT(digit)=7)
exten => 0123456789,n,Set(TIMEOUT(response)=21)
exten => 0123456789,n,Authenticate(1234,,4) ;; change 1234 with a password of your choice - 4 digits
exten => 0123456789,n,Background(1&activated&2&de-activated) ;; audio messages in /usr/share/asterisk/sounds
exten => 0123456789,n,WaitExten(5,2)
exten => 0123456789,n,Hangup()
;
exten => 1,1,Playback(activated)
exten => 1,2,System(/www/cgi-bin/start.cgi &) ;; shell script 1
exten => 1,3,Wait(2)
exten => 1,4,Hangup()
;
exten => 2,1,Playback(de-activated)
exten => 2,2,System(/www/cgi-bin/stop.cgi &) ;; shell script 2
exten => 2,3,Wait(2)
exten => 2,4,Hangup()

to be continued soon ...

1 Like

Hello,
your tutorial very helpful, thank you very much.

Is there any step coming??
im using your tutorial to make my project