OpenWrt Forum Archive

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

The content of this topic has been archived on 31 Mar 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.


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

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

"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 (, 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 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
touch /www/sms/dbsms
touch /www/sms/dbcmd

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/" (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/            # esegue comando ON
#        echo $ancmd >> $dbcmd;;
#        Off) /root/          # 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 -p25

# 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  -p25" with your email addresses and SMTP server

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

chmod 777 /root/

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/

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:
* 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="#ffffff">
<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="#ffffff" align="left">
<a href="index.php">Torna a invio SMS</a></td>
<td bgcolor="#ffffff" align="center">
<a href="dbsms">Salva SMS</a></td>
<td bgcolor="#ffffff" 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="#ffffff" align="left">
<a href="dbsms">Salva una copia degli SMS</a></td>
<td bgcolor="#ffffff" align="center">
<a href="lista.php">Torna a Lista messaggi ricevuti senza fare modifiche</a></td>

(Last edited by pilovis on 14 Sep 2016, 11:05)

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)

or if you want to send a Flash message use the following command:

/bin/echo "message" | /usr/bin/gnokii --config /etc/.gnokiirc --sendsms +xx.xxxxxxxx  --class 0        

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/

The received SMS list file is located here:


(Last edited by pilovis on 26 Apr 2016, 17:19)

Cant find package gnokii in CC 15.05

opkg update
opkg install gnokii
Unknown package 'gnokii'.
Collected errors:
 * opkg_install_cmd: Cannot install package gnokii.

It's clearly stated at the beginning of this thread:

pilovis wrote:



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!)



Sorry, blame the OpenWRT developpers.

(Last edited by pilovis on 16 Dec 2015, 11:58)

Don't blame the developers, but the community which is not willing / able to maintain a plethora of packages.

pilovis wrote:
pilovis wrote:

Instructions how to transform a cheap 110/220Vac Home PIR Motion Sensor in a "USB PIR motion sensor" for OpenWRT/Raspberry/Arduino

... if you have a router that supports GPIO inputs (Vodafone Station does NOT) you can connect the PIR sensor directly to GPIO without using USB, simply connect the collector of the Optocoupler output transistor to a GPIO input pin (button active low image).

hi pilovis!
thank for all your works!
i have read this guides: … ugbuttons,
i want to check another gpio port (etc Lan1,2,3. led) to connect pir sensor. I could check status of them by uci command. how to excute a event when it change status without loop checking!
p/s: sorry my bad english

how to control with voice call (detect dtmf tone)? are you try this? it's easy, faster and get direct report in few case!

(Last edited by phantnang on 17 Dec 2015, 19:44)

phantnang wrote:

how to excute a event when it change status without loop checking!

Everything is a file.
You can get notified of a file modification by INOTIFY.
Check inotifyd applet from busybox.

The discussion might have continued from here.