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.
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:
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
/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".
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.
Date added record
Date sent sms
Recipient's phone number
Text message SMS
If an error occurred, this field contains the date of the occurrence of the error
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.
This is a very simple script written in PHP. It contains a form to send SMS, sent form is saved in the table "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.
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.
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.