Friday 1 November 2013

A C program in Linux to send an SMS using SIM900 GSM Modem


Hello,

This is a tutorial which will show you, how you can send a Text SMS to a mobile number using a GSM Modem. Here I used GSM Modem with SIM900 inside it. If you don't know what the hell this is... then follow the link for brief description SIM900 and here is the modem which I used SIM900 GMS Modem. Sending SMS is not big deal, but it can be tricky for beginners (like it was for me when I did this).


Modems like this one works on a command list called "AT Commands". You can find a AT command list very easily by Google-ing. I will describe only those commands which are necessary to send an SMS. But, other commands (like calling, or reading SMS) are very similar so after knowing this, you will be able to do anything you want with it.


The modem as shown in ebay link has an RS232 interface. So to connect it with my computer, I used an RS232 to USB converter. Now, let's start going through the implementation.


I am a Linux user. So, to send command to GSM Modem in C (considered as Writing to a Device in Linux), you need to open it with permission to Write in it. Done by:




Okay, several things for a Linux newbie : open is a system call which opens any file in C program to read from it or write in it. The string in double quotes is not gibberish :D. When you attach any device to a Linux system, to communicate with it, Linux creates a specific file. That file is located in /dev directory. In this case, the name of file created is ttyUSB0. Now, after specifying the file to be opened, it's time to specify permissions. O_RDWR indicates that the file should be opened with Read/Write permissions. This open function returns a variable called "File Descriptor", and that will be used throughout the program to access this file.


Now, to make sure that connections are all right and devices are powered on and other stuff like that, write simple "AT" to the device. Can be done like this:




This is another system call, which writes to the already opened device, which is described by fd. Here, it first writes "AT" and then gives a carriage return, denoted by '\r'. Carriage specifies end of the command.




Here it tries to read 5 bytes from the Modem. There is something called an "Echo Mode" in modem, in this mode whatever you send in it, it will send back (and also appropriate reply to command will also be sent by the Modem). If everything is all right, it will reply with "OK". If you don't get this output, go and check things like Buad Rate, Power Levels etc.


After getting an OK signal from Modem, first thing we need to do it is set it in text mode. This is done by command "AT+CMFG=1". Done by:




Now, specify the mobile number (to which you want to send SMS).



This command will give you a prompt to write the content of SMS. In this prompt, you need to write the content and then press CTRL+Z to terminate it. At this termination entered text will be sent as SMS to the specified number. This can be done by:




'\x1A' is a backslash character which is CTRL+Z, which terminates the prompt, and sends entered text. So, our SMS is now sent. 

Here is the source code.



EDIT : Here is a full term project based on this. :)

I hope it was helpful. Thanks for reading. Leave comments.
Hardik Madhu

1 comment:

  1. Nice blog.
    An important point after buying a modem (or even before the purchase) is to read its user manual.You want read visit here evask.ru

    ReplyDelete