[Quick How To] Web SMS server with OpenWRT

sezione dedicata ai sistemi operativi alternativi Open Source

[Quick How To] Web SMS server with OpenWRT

Messaggio da leggereda root » 22/11/2015, 10:29


Check also my last How To:
[How To] Openwrt + Asterisk11 + GSM/SMS channel (chan_dongle):

"[How To] Control a simple DIY relay board via internet Web page or SMS":

"Cheap Digital Stereo WiFi Internet Radio & MP3 Player":


Quick How To create a Web SMS server with OpenWrt Barrier_Breaker
(don't use Chaos Calmer, it sucks, too many packages are missing, including Gnokii!)


Please note that some comments/descriptions/labels/messages in the scripts, configuration files and PHP files described here, are in Italian.
I will translate them in English as soon as I have enough free time and will post here the translated files.

This SMS server allows to send and receive/list/edit/save SMS, all from a simple web interface (http://xxx.xxx.xxx.xxx/sms/), using Gnokii and an E169 Huawei SMS dongle (many other dongles may also be used).
This SMS server also forwards all received SMS by email (an email is sent for each SMS received).
By editing the getsms.sh script, this server can also execute preset commands received by SMS (read notes in the section below).

There are 3 PHP web pages:
index.php to send SMS messages,
lista.php to see all received SMS messages,
editor.php to save/edit/delete the messages received

All pages are linked togheter.

Note: the maximum allowed message characters are 160 for each SMS, all characters in excess will be truncated, anyway there is a character number counter in the web page.

1) Install packages

opkg update

opkg install mini-sendmail
opkg install luci-proto-3g
opkg install kmod-usb-core
opkg install kmod-usb-uhci
opkg install kmod-usb-ohci
opkg install kmod-usb2
opkg install usbutils
opkg install usb-modeswitch kmod-usb-serial kmod-usb-serial-option kmod-usb-serial-wwan
opkg install gnokii
opkg install php5 php5-cgi

2) Configure system

create working directory:

mkdir /www/sms

than edit file "/etc/config/uhttpd" (add lines for PHP support):

[config uhttpd 'main'
list listen_http ''
list listen_http '[::]:80'
list listen_https ''
list listen_https '[::]:443'
option home '/www'
option rfc1918_filter '1'
option max_requests '3'
option max_connections '100'
option cert '/etc/uhttpd.crt'
option key '/etc/uhttpd.key'
option cgi_prefix '/cgi-bin'
option script_timeout '60'
option network_timeout '30'
option http_keepalive '20'
option tcp_keepalive '1'
option ubus_prefix '/ubus'
option config '/etc/httpd.conf'

option interpreter '.php=/usr/bin/php-cgi'
option index_page 'index.php index.html'

config cert 'px5g'
option days '730'
option bits '1024'
option country 'DE'
option state 'Berlin'
option location 'Berlin'
option commonname 'OpenWrt'


Basic Authentication on uhttpd

To add a password to uhttpd (web page access) to avoid that anyone can send/read SMS, use the following commands:

uci set uhttpd.main.config=/etc/httpd.conf
uci commit uhttpd
echo "/:user:password" > $(uci get uhttpd.main.config)
/etc/init.d/uhttpd restart

note: change user:password with your desidered user/password (for Web access)

create/edit file "/root/getsms.sh" (Gnokii script to check for new SMS messages and save them to /www/sms/dbsms file,
eventually it can also execute commands received by SMS, to do so uncomment and modify the lines after "case $ancmd"):

# OpenWRT + 3G dongle + Gnokii
# Barrier Breaker
# GetSMS - controlla la presenza di
# nuovi SMS sulla periferica e se
# riconosce dei comandi esegue delle
# operazioni.
#Send On text to switch on the relay
#Send Off text to switch off the relay

# Variabili
folder=/www/sms # Cartella di destinazione
newsms=$folder/newsms # Nuovi SMS
dbsms=$folder/dbsms # Database SMS ricevuti
newcmd=$folder/newcmd # Nuovi comandi da attuare
dbcmd=$folder/dbcmd # Database comandi eseguiti

numsms=5 # Numero di SMS da leggere

# Riempie il file newsms
/usr/bin/gnokii --getsms ME 0 $numsms -d > $newsms

# Riempie il file newcmd
/bin/grep Text -A1 $newsms | /bin/grep -v "Text\|--" > $newcmd

# Conta i comandi
numcmd=$(grep -c "^" $newcmd)

# Se non ci sono nuovi comandi non prosegue
if [ $numcmd -eq 0 ]
exit 1

# Analizza i comandi
for ancmd in $(cat $newcmd)
case $ancmd
# On) /root/comando_on.sh # esegue comando ON
# echo $ancmd >> $dbcmd;;
# Off) /root/comando_off.sh # esegue comando OFF
# echo $ancmd >> $dbcmd;;
# Restart) /sbin/reboot # Riavvia il router
# echo $ancmd >> $dbcmd;;
*) ;;

# Aggiunge i nuovi sms al database SMS

/bin/cat $newsms >> $dbsms
echo -n '<p>' >> $dbsms

# Manda una mail con i nuovi SMS
/bin/cat /www/sms/newsms | /usr/sbin/mini_sendmail -fsender@domain.com -ssmtp.domain.com -p25 recipient@domain.com

# Svuota i file newcmd e newsms
> $newcmd
> $newsms

# Svuota memoria SMS
/usr/bin/gnokii --deletesms ME 0 4
/usr/bin/gnokii --deletesms SM 0 4

exit 0

if you use a different dongle you may need to change the line: "/usr/bin/gnokii --getsms ME 0 $numsms -d > $newsms"
to: "/usr/bin/gnokii --getsms SM 0 $numsms -d > $newsms" (ex. for Huawey K3765)

Adapt the line "/bin/cat /www/sms/newsms | /usr/sbin/mini_sendmail -fsender@domain.com -ssmtp.domain.com -p25 recipient@domain.com" with your email addresses and SMTP server

Give the right permissions to the above script with the following command:

chmod 777 /root/getsms.sh

create/edit file "/root/.gnokiirc" (Gnokii config file)

model = AT
port = /dev/ttyUSB1
connection = serial

Note: if you use a different dongle you may need to change the port to "/dev/ttyUSB0" (ex. for Huawey K3765) or to "/dev/ttyUSB2"

copy Gnokii configuration file also to /etc:

cp /root/.gnokiirc /etc/.gnokiirc

create/edit file "/etc/crontabs/root":

*/1 * * * * /bin/sh /root/getsms.sh

Note: this crontab lauches the script to check for new SMS every minute.

create/edit file "/www/sms/index.php":

/************************************************** *******************
* Chan_Dongle SMS Script v.0.01
* initially written for The Raspberry Asterisk
* and modified for OpenWRT/Gnokii by pilovis
* Author: Troy Nahrwold
* Email: Troy(at)eternalworks(dot)com
* Company: Eternal Works
* Website: www.eternalworks.com
* Disclaimer:
* This product is solely a private production of the above named
* author, and is neither endorsed nor supported by Eternal Works.
* Although this product has been thuroughly tested, it is
* distributed AS IS, and the author assumes no liability for any
* damages this script may cause to your system. The author
* has provided full source code and encourages you to review the
* source code to determine any effects it may have on your system.
* (c) Copyright 2011, Troy A Nahrwold, Eternal Works, LLC.
* All Rights Reserved.
************************************************** *******************/
$ini = "| /usr/bin/gnokii --config /etc/.gnokiirc --sendsms";
if(isset($_REQUEST['phonenumbers']) && !empty($_REQUEST['phonenumbers']) && !empty($_REQUEST['message']))
$message = substr($_REQUEST['message'],0,160);
$phonenumberarray1 = explode(' ',$_REQUEST['phonenumbers']);
foreach ($phonenumberarray1 as $phonenumber)
$output = "Testo: $message<br><br>\n";
$runcommand = '/bin/echo ' . $message . ' | /usr/bin/gnokii --config /etc/.gnokiirc --sendsms ' . $phonenumber;
$output .= "Invio messaggio a: $phonenumber<br>\n";
* inserisce la data e l'ora:
* echo date('d/m/Y - H:i:s');
* dipendenze:
* opkg install zoneinfo-core
* opkg install zoneinfo-europe
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Invio SMS da web</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript">
function countChar() {
var count_char_textarea = document.getElementById("message");
// count_char_textarea.value = count_char_textarea.value.length;
var char_length = document.getElementById("char_length");
if ( count_char_textarea.value.length > 160 ) {
count_char_textarea.value = count_char_textarea.value.substr(0, 160);
char_length.innerHTML = count_char_textarea.value.length;
<body bgcolor="#84b0fd" text="#030303" link="#9abcde">
<a href="./index.php"><h2 align="center"></h2></a>
<table border="0" cellspacing="0" cellpadding="1" width="600" bgcolor="#ffffff" align="center">
<table border="0" cellspacing="0" cellpadding="3" width="100%" bgcolor="#ffffff" align="center">
<tr bgcolor="#abcdef">
<td><b><?php echo $output; ?></b></td>
<tr><form action="index.php" method="post">
<p><b>Numero di cellulare destinatario:</b> <br><font size="-2">(Usare il formato internazionale: +39XXXXXXXX)</font></p>
<textarea id="phonenumbers" name="phonenumbers"></textarea>
<p><b>Testo del messaggio da inviare:</b> <br> <font size="-2">(Massimo 160 caratteri, se superiore il messaggio verra' troncato) </font></p>
<textarea id="message" name="message" size="160" rows="3" cols="40" onchange="countChar()" onkeyup="countChar()"></textarea><br /><br/>
<font size="2">conteggio caratteri del messaggio: <span id="char_length"> 0 </span></font>
<button type="submit">Invia SMS</button><br /><br />
<td bgcolor="#ffffff"><a href="javascript:history.back()">Invia lo stesso messaggio ad altro numero</a></td>
<table border="0" cellspacing="0" cellpadding="3" width="100%" bgcolor="#ffffff" align="center">
<td bgcolor="#000000">
<a href="lista.php">Lista messaggi ricevuti</a></td>

create/edit file "/www/sms/lista.php":


$myfilename = "/www/sms/dbsms";
echo file_get_contents($myfilename);

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMS ricevuti (autorefresh = 10s)</title>
<link rel="stylesheet" href="style.css" type="text/css">
<meta http-equiv="refresh" content="10" >
<body bgcolor="#84b0fd" text="#000000" link="#9abcde">
<table border="0" cellspacing="0" cellpadding="3" width="100%" bgcolor="#ffffff" align="right">
<td bgcolor="#000000" align="left">
<a href="index.php">Torna a invio SMS</a></td>
<td bgcolor="#000000" align="center">
<a href="dbsms">Salva SMS</a></td>
<td bgcolor="#000000" align="right">
<a href="editor.php">Editor SMS</a></td>

create/edit file "/www/sms/editor.php":


// configuration
$url = 'lista.php';
$file = '/www/sms/dbsms';

// check if form has been submitted
if (isset($_POST['text']))
// save the text contents
file_put_contents($file, $_POST['text']);

// redirect to form again
header(sprintf('Location: %s', $url));
printf('<a href="%s">Moved</a>.', htmlspecialchars($url));

// read the textfile
$text = file_get_contents($file);

<body bgcolor="#84b0fd" text="#030303" link="#9abcde">
Questo form ti permette di modificare la lista degli SMS ricevuti.
<b>L'operazione una volta effettuata non e' piu' reversibile,</b>
<b>e' consigliabile salvare una copia degli SMS prima di modificarli</b>
<i>Nota: non rimuovere i tag < p > di separazione degli SMS rimanenti</i>
<!-- HTML form -->
<form action="" method="post">
<textarea rows="22" cols="80" name="text">
<?php echo htmlspecialchars($text) ?></textarea>
<input type="submit" />
<input type="reset" />
<table border="0" cellspacing="0" cellpadding="3" width="100%" bgcolor="#ffffff" align="center">
<td bgcolor="#000000" align="left">
<a href="dbsms">Salva SMS</a></td>
<td bgcolor="#000000" align="center">
<a href="lista.php">Torna a Lista messaggi ricevuti senza fare modifiche</a></td>
Avatar utente
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin

Re: [Quick How To] Web SMS server with OpenWRT

Messaggio da leggereda root » 22/11/2015, 10:30

Note: this SMS server does not need an internet connection to work (except for email sending if recipient is located out of your local LAN), you may access the web pages through the local lan by ethernet and/or Wifi.

- Additional notes:

You can send a sms message by using the console with this command:

/bin/echo "message" | /usr/bin/gnokii --config /etc/.gnokiirc --sendsms +xx.xxxxxxxx (+countrycode.number)

To check the correct configuration of Gnokii with your dongle, use the following command:

/usr/bin/gnokii --identify

You should get something like this:

root@OpenWrt:~# /usr/bin/gnokii --identify
GNOKII Version 0.6.21
IMEI : 354578034577855
Manufacturer : huawei
Model : E169
Product name : E169
Revision :

To check for new SMS from console use the following command:

/bin/sh /root/getsms.sh

The received SMS list file is located here:

Avatar utente
Messaggi: 450
Iscritto il: 10/04/2012, 10:55
Località: Turin

Torna a OpenWRT

Chi c’è in linea

Visitano il forum: Nessuno e 1 ospite