Tuesday, December 2, 2008

How to send sms from pc using gsm modem and sql server?



Introduction
I give you an example on how you can send SMS messages to any mobile from your computer using a gsm modem.You can use your gsm mobile phone to send the sms.

System Architecture
It cosists of a GSM Modem, that is attached to the PC with a phone-to-PC datacable or blue tooth, an SQL-SMS Gateway software installed onto your PC, an SQL database server, such as SQL Express or MS SQL and your SMS application . As you can see on the figure, your SMS application will create an SQL record in the database. The SMS gateway will poll this record using an SQL SELECT statement and will send it using a GSM modem.


Preparing your database server
To get this architecture running, first you should prepare your database. This means that you should create two database tables. One will be used for sending SMS messages (ozekimessageout) and the other will be used for receiving SMS messages (ozekimessagein). The database table you create should contain a field for sender number, recipient number and message text. For sending messages you also need a status field, that will indicate wether the message has been sent.

The recommended database table layout can be seen on Figure 2. Please note that you can add extra columns to this layout freely. After creating the database table layout you should also create a username and a password that can be used to log into database.

create database ozeki
GO

use database ozeki
GO

CREATE TABLE ozekimessagein (
id int IDENTITY (1,1),
sender varchar(30),
receiver varchar(30),
msg varchar(160),
senttime varchar(100),
receivedtime varchar(100),
operator varchar(30),
msgtype varchar(30),
reference varchar(30),
);

CREATE TABLE ozekimessageout (
id int IDENTITY (1,1),
sender varchar(30),
receiver varchar(30),
msg varchar(160),
senttime varchar(100),
receivedtime varchar(100),
operator varchar(100),
msgtype varchar(30),
reference varchar(30),
status varchar(30),
errormsg varchar(250)
);
GO

sp_addLogin 'ozekiuser', 'ozekipass'
GO

sp_addsrvrolemember 'ozekiuser', 'sysadmin'
GO

Configuring the SMS Gateway
Once the database has been setup you should install and configure the SMS Gateway. The SMS Gateway we use in this article is the Ozeki NG - SMS Gateway it can be downloaded from www.ozekisms.com. There are other similar SMS gateways available. We chose this SMS Gateway, because it is very reliable, easy to configure and it is based on .NET which means it integrates well into our architecture. In the SMS gateway first you should configure the GSM modem attached to your PC. This can be done by adding a GSM Modem service provider connection. Detailed steps:

Step 1.) Open http://127.0.0.1:9501 in Internet Explorer, login with admin/abc123

Step 2.) In the "Serviceprovider connections" menu click on "Add service provider connection", then select "GSM Modem Connection" and click "Install"

Step 3.) On the GSM modem configuration form, select the com port (usually COM1) and click on autodetect to configure your modem

Step 4.) In the "Users and applications" menu click on "Add user", the select "Database user" and click on "install"

Step 5.) For username provide "sql1"

Step 6.) For connection string type select "OleDb" and for connection string enter:
Provider=SQLNCLI;Server=.\SQLEXPRESS;User ID=ozekiuser;password=ozekipass;Database=ozeki;Persist Security Info=True

Step 7.) In the configuration form I suggest you to turn on SQL logging. You can do this by enabling the checkboxes:
"Log SQL SELECT statements" and "Log SQL UPDATE statements".


Writing .net Application

Once the database has been perpared and the SMS Gateway has been setup you can use Visual Studio.NET to create your SMS application like in the figure above.On button click you can insert the data in the web form to the table 'ozekimessageout.'

The sms gateway wil automatically fire the sms whenever a data is inserted into the table 'ozekimessageout'


private void button1_Click(object sender, EventArgs e)
{
try
{
//Connect to the database
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=SQLNCLI;Server=.\\;"+
"User ID=ozekiuser;password=ozekipass;Database=ozeki;Persist Security Info=True";
conn.Open();
if (conn.State == ConnectionState.Open)
{
//Send the message
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
string SQLInsert =
"INSERT INTO "+
"ozekimessageout (receiver,msg,status) "+
"VALUES "+
"('"+tbSender.Text+"','"+tbMsg.Text+"','send')";
cmd.CommandText = SQLInsert;
cmd.ExecuteNonQuery();
MessageBox.Show("Message sent");
}
//Disconnect from the database
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


1 comment: