By WW Kong (RH2T Magazine Vol.7, Dec10)
In volume 5, we discussed about Smart Home technology. One of the features of Smart Home is that we can control the household appliances and monitor the condition of our house remotely by just using a mobile phone. This is no rocket science and can be easily achieved by using a GSM modem.
GSM modem is specialized type of modem that operates over subscription based wireless networks which is similar to a mobile phone. A GSM modem accepts a SIM card, and basically acts like a mobile phone for the computer. Traditional modem is attached to computers for ‘dial-up’ to connect with other computer systems. A GSM modem operates in a similar fashion, except that it sends and receives data through radio waves rather than a telephone line.
- Besides the dial-up connection, GSM modem can also be used for sending and receiving SMS which is also one of the key features of GSM modem. In this issue, we discuss on how to set up a simple home automation where we can control the fan and monitor the alarm status by using SMS. All the examples given are based on the MOD 9001D GSM Modem available from Cytron Technologies. The User’s Manual and GSM modem command set can be obtained from the product page here. Some of the features of this GSM Modem are as follow:
- · RS-232 Interface
- · Tri-Band: GSM900, GSM1800 and GSM1900
- · Support TCP/IP
- · Support standard extended open AT commands
- · Support GPRS class 10
- · Accept supply voltage from 5 to 12V
- · Support PDU and Text mode for SMS
MOD 9001D RS232 GSM/GPRS Modem from Cytron Technologies
2.0 Quick Start – Interfacing GSM Modem with PC
In order to get familiar with the AT commands, we can hook up the GSM modem to the PC serial port to test the functionality of the GSM Modem. Hyper Terminal is used to send the AT command to and from the modem.
2.1 Sending SMS
1. Insert a valid SIM card from any mobile provider into the SIM slot.
2. Connect the GSM Modem to the supplied AC-DC adapter and also to the serial port of the PC.
3. Select the correct COM Port in Hyper Terminal and set up the parameters as follows.
4. Power on the GSM Modem and wait for it to initialize. While waiting, we can use the following command to make sure we can communicate with the GSM Modem.
The GSM Modem should respond with “OK”.
5. Once it’s ready, the following response will be received from the modem.
6. Now we can start sending the AT command to the GSM Modem. Start by configure the modem to use Text Mode instead of PDU mode for the SMS. This mode allows us to send SMS using AT commands without the need to encode the binary PDU field of the SMS. Use the following command to put the modem in text mode and it should respond with “OK”.
7. After that, we can send the SMS message with the following command:-
Replace the above phone number with your own number. The modem will respond with:-
We can now type the message and send the message using the <CTRL+Z> key.
After few seconds, the modem will respond with the message ID of the message, indicating that the message has been sent correctly.
AT Commands to send a SMS
2.2 Receiving SMS.
1. Set up the GSM Modem following step 1 – 6 in the previous section.
2. Once the GSM Modem is ready, we can use a mobile phone to send a SMS to the modem.
3. The following response received from the modem indicates a new SMS is received.
The last character indicates the memory index number, we use this index to read or delete this message. In this case, the index number is 1.
4. To list out all the available SMS in the SIM card, we can use the following command:
And the modem will respond with a list of SMS messages in the SIM card.
The responding messages are formatted like this:-
· Index – The memory index number, use this index to read or delete this message.
· Status – The status of this message. There might be “REC READ” or “REC UNREAD” for received messages depending on whether the messages have been read or listed before.
· from_address – The subscriber number of the sender.
· mr – The reference number of this message. Most modems keep this field empty.
· Scts – The time when the message is forwarded to this phone or modem.
· data – The actual message data in plain text.
5. To read a single message, we have to use the read command. We must first know the index of the message. For instance, to read the message on memory location 1, we use
The modem will respond as follows:-
Please note that the status of the message is now “REC_READ” instead of “REC_UNREAD” because we have read the message.
6. Once we have read a message, we can free the memory by deleting the message from the storage. For instance, to delete the message on memory location 1, we use:
The modem will delete the message and return to “OK”.
AT Commands to read and delete a received SMS
3.0 Interfacing GSM Modem with MCU
After having the basic idea on how to use a GSM modem for sending and receiving SMS, we can now start to make a mini project – GSM Enabled Smart Home. In this project, we use the SK40C together with PIC16F877A as the main controller. The onboard LED will act as the lighting in the house and the push button is the alarm status. This main controller is connected to the GSM modem via a MAX232 circuit as shown in the schematic below. Don’t forget to short the DSR and DTR, also the CTS and RTS, those are pin required for hand-shacking. Without shorting these pairs of pin, your system might not work as expected.
Schematic for interfacing the SK40C with a GSM modem
Components for GSM Enabled Smart Home
Before we go further to discuss the coding part, let’s take a look at how this thing works.
When the SK40C is turned on, we need to wait until the GSM modem to initialize and “Ready” to be shown on the LCD.
“Ready” will be shown when the GSM modem has been initialized.
When SW1 on SK40C is pressed, it will send a SMS i.e. “Warning: Switch 1 has been triggered!” to the owner’s mobile phone.
· The owner can also control the LED on the SK40C. To turn on the LEDs, type “on” and send the SMS to the GSM modem. Type “off” to turn off the LEDs.
In this section, we will look at the firmware to interface with the GSM modem. Only the important parts will be discussed. The full working source code can be downloaded from here.
Let’s start with two of the most important functions in this example. The first one is the function to send an AT command to the GSM modem and the second one is to read a line of string from the modem.
Function to send an AT command to the GSM modem
Function to receive a line of string from the GSM modem.
Right after powering up the SK40C, we need to initialize the GSM modem and wait until the “Call Ready” string is received from the modem. We need to make sure that we power up the GSM modem first so that it’s ready to communicate when we turn on the SK40C. The following figure is the code snippet to initialize the GSM modem.
Code snippet to initialize the GSM modem
If the GSM modem has been set up successfully, “Ready” will be shown on the LCD. Now the program is waiting for the user input either from the push button or the SMS. If SW1 is pressed, the following code will be executed to send an SMS to notify the house owner. Please remember to change the highlighted number to your own mobile number.
Code snippet to send a SMS to the house owner when SW1 is triggered
If there is a new SMS received by the GSM modem, the program will read the content of the SMS and turn on/off the LEDs accordingly.
Code snippet to read a SMS and turn on/off the LEDs accordingly
In short, GSM modem makes remote controlling and monitoring possible without needing long range wireless transceiver provided the GSM modem is located within the coverage area of any mobile provider. Besides smart home, GSM modem is also widely used together with the GPS module as a security system for automobile. When the car is stolen, the GSM modem can send the GPS location of the car to the owner and the owner can shut off the engine of the car by just sending a SMS. If you face any problem, please do come to our technical forum to discuss.