In a next future I will post a HOW-TO about installing and full configuring Asterisk13 on an OpenWRT router,
This time it is just a quick HOW-TO about receiving a SMS message with Asterisk (with chan_dongle) and then execute any command found (if any) in the SMS body message.
this is the asterisk dialplan:
nano /etc/asterisk/extensions.conf
[from-dongle]
;
exten => sms,1,Noop(Incoming SMS from ${CALLERID(num)} ${BASE64_DECODE(${SMS_BASE64})})
exten => sms,n,System(echo 'From: ${CALLERID(num)} \n${BASE64_DECODE(${SMS_BASE64})}' >> /tmp/sms.txt)
exten => sms,n,Wait(3)
exten => sms,n,System(/bin/sh /root/getsms.sh)
exten => sms,n,Wait(15)
exten => sms,n,System(/bin/rm -f /tmp/sms.txt)
exten => sms,n,Wait(2)
exten => sms,n,Hangup()
this is the script launched by asterisk when a SMS is received
nano /root/getsms.sh
#!/bin/sh
#################################################
# GetSMS - script to check for commands
#################################################
# Variables
folder=/www/sms
dbsms=$folder/dbsms.txt # SMS received database
newcmd=$folder/newcmd.txt # new commands to be executed
dbcmd=$folder/dbcmd.txt # executed commands database
now=$(date) # current date and time
# fill newcmd file with new commands received, if any
/bin/grep XX /tmp/sms.txt > $newcmd
# count the commands received, if any
numcmd=$(grep -c "^" $newcmd)
# if no commands are found, go ahed
if [ $numcmd -eq 0 ]
then
exit 1
fi
# check for any new command
for ancmd in $(cat $newcmd)
do
case $ancmd
in
XXon) /root/on.sh & # if the body of the message contains "XXon", launch the on.sh script
echo $ancmd >> $dbcmd;;
XXoff) /root/off.sh & # if the body of the message contains "XXoff", launch the off.sh script
echo $ancmd >> $dbcmd;;
*) ;;
esac
done
# add new sms to database file
/bin/cat /tmp/sms.txt >> $dbsms
echo -n '<p>' >> $dbsms
# wait 2 seconds
/bin/sleep 2
# empty newcmd file
> $newcmd
chmod 755 /root/getsms.sh
nano /etc/asterisk/dongle.conf
[general]
interval=15
[defaults]
context=from-dongle
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=15
mindtmfduration=40
mindtmfinterval=200
callwaiting=auto
disable=no
initstate=start
exten=+39xxxxxxxxx # put your mobile number here
dtmf=relax
[dongle0]
audio=/dev/ttyUSB1
data=/dev/ttyUSB2
;imei=358109024359102
;imsi=222956701469191
in this exampe, the two commands I configured in the getsms.sh script, are:
XXon and XXoff , XX is the prefix to recognize a command.
Note: do not add anything else to the SMS body, just a single command without any space before or after the command, otherwise the command will not be executed.
You can obviously add more commands to the above script at your choice.
All the SMS received and all the command executed are in two files: /www/sms/dbsms.txt and /www/sms/newcmd.tx, so you can point your browser to http://your-router-ip/sms to easily read them.