This website uses 'cookies' to give you the best, most relevant experience. Using this website means you're happy with this. read more Accept & Close
Start Games
Do It Yourself
DIY & Home Improvement

Raspberry PI SMS sender

Raspberry PI SMS sender
Raspberry PI SMS sender
How to make a simple system for sending SMS using Raspberry PI and USB modem. The SMS will be able to send via the form on the website. The project will be created using PHP and C# on Linux. The software to communicate with the modem I will write in C#. To store the sent SMS I use the PostgreSQL database. The USB modem requires a SIM card and the cost depends on the GSM operator.

Step 1: Components

Step 2: Requirements

In this project we will need installed: Install the apache2 package by typing the following command in to the Terminal:
sudo apt-get update sudo apt-get install apache2
Then we install PHP.
sudo apt-get install php5 libapache2-mod-php5
Install the PostgreSQL.
sudo apt-get install php5-pgsql
Now install mono.
sudo apt-get install mono-complete

Step 3: USB modem and Raspberry PI

We plug in USB dongle modem to Raspberry PI. To see if our modem is visible on system, we type in the terminal following command:
lsusb
My USB modem in the system is seen as:
Bus 001 Device 005: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard

Serial ports should be visible as /dev/ttyUSB0. To check this we use the command
ls /dev/ttyU*
/dev/ttyUSB0 /dev/ttyUSB1 /dev/ttyUSB2

We will now test the AT commands on the USB modem. For this purpose, we will use the minicom program.
sudo apt-get install minicom
After installing minicom, we have to configure it. Type the command below, then select "Serial port setup".
minicom -s
Set <b>Serial Device</b> and <b>Bps/Par/Bits</b>.
Set Serial Device and Bps/Par/Bits.
After setting minicom, we check that the modem is working properly. For this we use the AT command ATE 1 and AT.
If everything is fine the answer from the modem will be "OK".

Step 4: Database creation

Now we are creating a database in PostgreSQL with a single table for storing outgoing SMS.
Field Description
sms_id Primary key
date_insert Date added record
date_send Date sent sms
number Recipient's phone number
message Text message SMS
date_error If an error occurred, this field contains the date of the occurrence of the error
date_cancel With this field we can cancel sending sms
CREATE TABLE sms ( sms_id serial NOT NULL, date_insert timestamp with time zone NOT NULL DEFAULT now(), date_send timestamp with time zone, "number" character varying(16) NOT NULL, message character varying(1600) NOT NULL, date_error timestamp with time zone, date_cancel timestamp with time zone, CONSTRAINT sms_pkey PRIMARY KEY (sms_id) )

Step 5: Form for sending text messages

Before using these scripts, you need to configure your connection to the database server. I use an external
database on my server, but you can also use a local database on the Raspberry PI.
$dbname=''; $host=''; $dbuser=''; $dbpass='';
This is a very simple script written in PHP. It contains a form to send SMS, sent form is saved in the table "sms".
<?php error_reporting(E_ALL); ini_set('display_errors', 1); if( $_SERVER['REQUEST_METHOD'] == 'POST' ){ $dbname=''; $host=''; $dbuser=''; $dbpass=''; try { $db = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $e){ echo "ERROR: " . $e->getMessage(); } $sql = "INSERT INTO sms(number, message) VALUES(:number, :message)"; $statement = $db->prepare($sql); $statement->bindValue(':number', $_POST['number']); $statement->bindValue(':message', $_POST['message']); $statement->execute(); $statement->closeCursor(); } ?> <html> <head> <title><?php echo exec('hostname'); ?></title> </head> <body> <form action="sms.php" method="post"> <table> <tr> <td>Number:</td> <td><input type="text" name="number"></td> </tr> <tr> <td colspan="2"> <textarea name="message" style="width: 300px; height: 200px;"></textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" name="" value="Send"></td> </tr> </table> </form> </body> </html>

Step 6: Software for sending SMS

The SMS software was written in C#. To be able to execute EXE on linux, you must have mono installed.
At the beginning of you need to configure the connection to the database and set the serial port under which you have a USB modem.
private static string db_login = ""; private static string db_datebase = ""; private static string db_password = ""; private static string db_host = ""; private static int db_port = 5432; private static string rs232_port = "/dev/ttyUSB0";
Download: SMS-cron.zip

For build I am using MonoDevelop on Windows and then compiled files I upload to Raspberry PI.
After compilation, we create "SMS-cron" folder in home directory and copy files there: Mono.Security.dll, Npgsql.dll, SMS-cron.exe We open the crontab for editing.
crontab -e
We add this line and save.
* * * * * ./home/pi/SMS-cron/SMS-cron.exe
Now SMS-cron will check every minute in the "sms" table for any new messages to send.

Step 7: The end

Time to send SMS from Raspberry PI.
SMS from <b>Raspberry PI</b> :)
SMS from Raspberry PI :)