Introduction: Raspberry PI SMS Sender

About: Software, electronics, do-it-yourself

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.

This article can also see here:

Raspberry PI SMS sender

Step 1: Components

Step 2: Requirements

In this project we will need installed:

  • Apache2
  • PHP
  • PHP PostgreSQL
  • Mono for C#

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 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.
sms_idPrimary key
date_insertDate added record
date_sendDate sent sms
numberRecipient's phone number
messageText message SMS
date_errorIf an error occurred, this field contains the date of the occurrence of the error
date_cancelWith this field we can cancel sending 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".
	ini_set('display_errors', 1);

		try {
			$db = new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
		}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']);
    <title><?php echo exec('hostname'); ?></title>
<form action="sms.php" method="post">
		<td><input type="text" name="number"></td>
		<td colspan="2">
			<textarea name="message" style="width: 300px; height: 200px;"></textarea>
		<td colspan="2"><input type="submit" name="" value="Send"></td>


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: 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.

My website

Microcontroller Contest 2017

Participated in the
Microcontroller Contest 2017