Sending SMS using GSM modem

A GSM modem is a specialised type of modem which accepts a SIM card, and operates over a subscription to a mobile operator, just like a mobile phone. These modems are more frequently connected to computers which allow the computers to communicate with the mobile network. They are most probably used for sending/receiving SMS. This article explains how to connect a GSM modem with the ZKit-51 and how to program it to send SMS.

Theory

Unlike mobile phones, a GSM modem doesn’t have a keypad and display to interact with. It just accepts certain commands through a serial interface and acknowledges for those. These commands are called as AT commands. There are a list of AT commands to instruct the modem to perform its functions. Every command starts with "AT". That’s why they are called as AT commands. AT stands for attention.

Table 1. SMS Related AT Commands

AT

It is used to test the connection.

AT+CMGF=1

It is used to instruct the modem to operate in text mode. AT+CMGF=0 will instruct the modem to operate in PDU mode.

AT+CMGS="mobile number"

It is used to send a text message. It accepts the recipient mobile number. As soon as this command is accepted the modem waits for the message content. The text message has to be sent sequentially and terminated by the char 0x1A.

AT+CMGW="mobile number"

It is used to store a message in the memory. After execution it returns an index for the message stored. Eg: AT+CMGW=1 . Here 1 is the index for the saved message. Later this index is used to process the message like deleting it or forwarding to the recipient number.

AT+CMGD=2

It is used to delete a message from the storage. The index of the stored message is used to delete it. Above command deletes the message with index 2.

In our simple project, the program waits for the mobile number to be entered through the keyboard. When a ten digit mobile number is provided, the program instructs the modem to send the text message using a sequence of AT commands

Testing your GSM modem

  • The GSM modem can be tested by connecting it with a PC. The modem is equipped with a RS232 cable. Just use a Serial to USB converter and connect it with the PC.

  • Now you can proceed with sending the commands to the modem using any serial communication program like Hyperterminal, minicom etc. Ensure the serial paramters are configured to 8N1 and the baudrate is set to 9600bps.

  • For each command you send the modem acknowledges with a message. Example: Just try sending "AT" to the modem. It sends back a result code "OK" which states that the modem is responding. If it’s not working fine, it sends "ERROR".

What you need

  • ZKit-51 Motherboad

  • ZKit-51 RS232 Transceiver

  • GSM modem - AG-GSM-2303R from Adaptek

  • SIM card

  • ZKit-51 Keypad

  • 20-pin FRC cable

  • DCE-DCE null modem cable (optional)

Hardware

  • Insert the SIM card into the GSM modem.

  • The modem has a DB-15 pin out facilitated with a DB-9 converter.

  • Connect the ZKit-51 RS232 Transceiver with Serial IO header of ZKit-51

  • Both the Modem and ZKit-51 RS232 Transceiver are DCE devices and have a female DB-9 connector. The devices can be connected using DCE-DCE null modem cable or the devices can be wired as given below

    1. Connect the Tx of the modem with Rx of the transceiver

    2. Connect the Rx of the modem with Tx of the transceiver

    3. Connect the ground of the modem with ground of the transceiver

  • Connect the ZKit-51 Keypad with ZKit-51 using the FRC cable

http://www.zilogic.com/static/images/gsm-circuit.png
Figure 1. Connection Diagram

Software

#include <board.h>
#include <keypad.h>
#include <lcd.h>
#include <serial.h>
#include <delay.h>
#include <stdio.h>

#define SIZE 24
#define FIFO_SIZE 15
#define MOBILE_NUM_MAX 10

/* send_sms() : Sends a text message to the reciepient number */
void send_sms(unsigned char * num, unsigned char * message )
{
        char buffer[SIZE];
        char send_to[] = "AT+CMGS=\"";
        lcd_clear();

        serial_puts("AT\r");
        mdelay(30);

        /* Operates the modem in text mode */
        serial_puts("AT+CMGF=1\r");
        mdelay(30);

        sprintf(buffer, "%s%s\"\r", send_to, num);
        serial_puts(buffer);
        mdelay(30);

        serial_puts(message);
        serial_puts("\r");
        mdelay(30);

        serial_putc(0x1A); /* Termination character which
                            * indicates the end of the message
                            */
}

/* Gets the mobile number from the keyboard.
 * When ten digits are collected, it invokes
 * the function send_sms to send a text message.
 */
void get_number()
{
        static char i;
        unsigned char number[11];
        number[i] = keypad_getc();
        lcd_putc(number[i]);
        i++;
        if (i == MOBILE_NUM_MAX) {
                send_sms(number, "Hello world");
                lcd_puts("Message sent");
        }
}

int main()
{
        char fifo[FIFO_SIZE];

        board_init();
        lcd_init();
        delay_init();
        serial_init(&fifo, FIFO_SIZE);
        keypad_init();

        lcd_puts("Enter number:");
        lcd_goto(1,0);

        /* Sets the call back function to get keystrokes */
        keypad_setcb(get_number);
        event_poll();
        return 0;
}