DuinoMite Project: Implementing GSM remote control in BASIC with few lines of code


Today I will show you how easy is to use MOD-GSM module to place phone calls, to take calls from other phones, to send and receive SMS and how to add cellular remote control to any of your embedded projects.

MOD-GSM is quad band 850/900/1800/1900 MHz GSM module and cover all GSM frequencies used in the world. It’s based on SIM340DZ module and have build in antenna on board so you do not need to connect external antenna.

All you need is SIM card for your GSM operator, note the card should be with UNLOCKED PIN, you place it in the SIM connector, apply 12VDC and press the wake up button. If everything is OK the red LED will start blinking once per 2-3 seconds which means that the module is registered to the GSM network and ready for use.

MOD-GSM is controlled with UART commands at 9600,8,1,N settings and you can connect MOD-GSM directly to DuinoMite on the UEXT connector.

The control of MOD-GSM is made with AT commands, the full list of the AT commands is in the SIM340 user manual located here.

To play with the different commands and evaluate their action I wrote small DM-BASIC code:

10 MSG$=”” ‘MOD-GSM returned message
20 CMD$ = “” ‘MOD-GSM command
30 OPEN “COM3:9600” AS #1 ‘Open UEXT UART to talk to MOD-GSM
40 PRINT “CMD:”; ‘wait for command
50 C$ = INKEY$ ‘get character
60 IF C$ = “” THEN 100 ‘if no character is entered check if MOD-GSM didnt sent message
70 IF C$=CHR$(13) THEN PRINT #1,CMD$ : CMD$=””: PAUSE 250: ? : GOTO 40 ‘ send command
80 CMD$ = CMD$ + C$: PRINT C$; ‘otherwise add the character to the command
90 GOTO 50
100 IF EOF(1) THEN 50 ‘check if something is in the UART receive buffer
110 M$ = INPUT$(1,#1); ‘read character from the receive buffer
120 IF M$ = CHR$(13) THEN 150 ‘if this is CR print the message
130 IF M$ <> CHR$(10) THEN MSG$ = MSG$+M$ ‘else add the character to the message
140 GOTO 50 ‘go back check for keys on the keyboard
150 ? “(“;LEN(MSG$);”)”;MSG$: MSG$=”” ‘here you can print or process MOD-GSM message
160 GOTO 40

When you run this small program it waits you to enter commands and send to MOD-GSM when you press ENTER, meantime listening if MOD-GSM send back messages to us and display them. This is good when you want to check how MOD-GSM respond to different commands.

What I noticed is that SIM340D do not respond to low case commands like “at” at start, once you write “AT” then it accept also lower case commands, but good idea is to not use lower case to prevent bugs 😉


interesting commands which I saw in the PDF:

ATD<number>; – call phone <number>
ATA – answers incoming RING
ATH – hang up the line

AT+GSN returns the modem IMEI when I execure it I got back “353358014969443” message which is my MOD-GSM IMEI number

AT+ATI returns the SIM340 info, what I got when executed is:


notice it says the modem is SIM300D although the hardware is SIM340D I guess these two modules share same firmware and this is why the response is as SIM300, the only difference between SIM300 and SIM340 is that later have 4 bands but SIM300 only 3 bands coverage

AT+CSQ returns the quality of the GSM signal – when I execute it I got response:

+CSQ: 25,0

I decided to hack a little bit to see what these numbers means. I covered the module with two metal pans:


the CSQ value decreased to +CSQ: 12,0 🙂

when I removed the top cover it recovered to +CSQ: 21,0


so generally you can use this to test your signal quality and the higher this value is the better is the signal reception, the values according to SIM340 PDF are in range 0-31

AT+COPS? return the name of the operator to whom SIM card you put belongs
+COPS: 0,0,”GLOBUL” operator name

i.e. the SIM card I put belongs to GLOBUL GSM operator

AT+CSPN? show your SIM card operator to which network you are connected, when I executed this is what I got

i.e. the MOD-GSM works in BG GLOBUL GSM network

SIM340 have internal temperature sensor which you can use to measure the temperature

AT+CMTE? will return the ambient temperature of the module:
+CMTE: 1,25

which means my room remperature is 25 C


to place phone call with DuinoMite and MOD-GSM is quite simple here is the code to do this:

10 OPEN “COM3:9600” AS #1
20 PRINT #1, “ATD+35932626259;”

if you run this code you will ring to Olimex phone number with your MOD-GSM (please do not test your MOD-GSM with our phone number 🙂 )

quite simple huh?

you can use this phone call as ALERT for instance if you detect something and want to warn the phone owner – just place call with this two line code

now how we can “receive” calls?

10 OPEN “COM3:9600” AS #1
20 MSG$=””
30 IF EOF(1) THEN 20 ‘wait MOD-GSM to tell us something
40 M$ = INKEY$(1,1) ‘read one character
50 IF M$ = CHR$(13) THEN 100 ‘process incoming message
60 IF M$<> CHR$(10) MSG$ = MSG$ + M$
70 GOTO 30 ‘wait for new message
110 GOTO 20

so with 2 lines of code we can place phone call and with 8 lines of code we can detect incoming call.

Now tell me how many lines of code you would use to do same in C 🙂

Perhaphs many of you know these GSM garage door openers, what they do is to open your door when you call them. The trick is that they detect the incoming call, and if the call comes from known number the GSM module Hang-up the call so the calling phone is no charged, then open the door.

Let see how we can implement this!

There is two ways MOD-GSM to respond to incoming call: one is by simply returning “RING” the other is to respond with “RING” and caller number. The default state is simple RING so you have to turn on Caller Identification with “AT+CLIP=1” command, let’s modify above code so it detects incoming RINGs and check if they are from “secure” number and only if such number is detected to do the action.

5 PHONE$ = “+359897286123” ‘this is the phone number which trigger our module with incoming call
10 OPEN “COM3:9600” AS #1
20 MSG$=””
30 IF EOF(1) THEN 20 ‘wait MOD-GSM to tell us something
40 M$ = INKEY$(1,1) ‘read one character
50 IF M$ = CHR$(13) THEN 100 ‘process incoming message
60 IF M$<> CHR$(10) THEN MSG$ = MSG$ + M$
70 GOTO 30 ‘wait for new message
100 IF INSTR(MSG$,PHONE$) <> 0 THEN PRINT “SECURE INCOMING RING DETECTED!”: PRINT #1,”ATH” ‘if the call is from secure number, print message and hang-out the line so no charge
110 GOTO 20

So with 10 lines we implemented these expensive garage door openers? Not bad!

Sending and receiving SMS with DuinoMite and MOD-GSM

Now let’s see the commands for SMS. The SMS is very convenient way to send status info, small amount of data, like “DOOR IS OPEN”, “PIR SENSOR IS ACTIVATED” etc.

Also by receiving SMS commands like “TURN HEATER ON” or “CUT ELECTRICITY” you can easily add remote control to any of your embedded devices.

There are few commands to operate with SMS:

AT+CMGF – this set the SMS mode we will use TEXT mode so the command should be “AT+CMGF=1” when you start using SMS

AT+CMGDA=”DEL ALL” this command deletes all SMS messages, it’s good to execute it after you receive SMS to not full the MOD-GSM SMS memory with many SMS messages

Sending SMS:

send sms command is with this format:


so let’s send SMS, the code is:

10 OPEN “COM3:9600” AS #1
20 PRINT #1,”AT+CMGF=1″: PAUSE 250 ‘select SMS message format as TEXT mode
30 PRINT #1,”AT+CMGS=”+CHR$(34)+”+359897123342″+CHR$(34)+CHR$(13)+”HELLO WORLD”+CHR$(26) ‘send SMS to phone +359897123342 with message HELLO WORLD

only 3 lines of code are necessary! and you can put in the send string your temperature, parameters you want to monitor etc etc etc.

you can combine this code with code above and to send back SMS with status only when the SECURE phone number call you first.

Receiving SMS:

OK I send now SMS with content “Led=on” to MOD-GSM

when MOD-GSM receive SMS it notify me with message:

+CMTI: “SM”,1

so I have to monitor in my code for this in the received messages, then I can read the message with the command:


the result is:

+CMGR: “REC UNREAD”,”+359897123342″,,”12/02/01,20:01:51+08″

With this information I can execute SMS commands only when they come from my known SECURE number.

Here is the code to receive commands by SMS and execute them:

5 PHONE$ = “+359897286123” ‘this is the phone number which SMS we will process
6 CMD$ = “LED=ON” ‘ this is the command which to trigger my action
7 ACTION = 0 ‘ listen
10 OPEN “COM3:9600” AS #1
15 PRINT #1,”AT+CMGF=1″ : PAUSE 250 ‘ set TEXT SMS mode
20 MSG$=””
30 IF EOF(1) THEN 20 ‘wait MOD-GSM to tell us something
40 M$ = INKEY$(1,1) ‘read one character
50 IF M$ = CHR$(13) THEN 100 ‘process incoming message
60 IF M$<> CHR$(10) THEN MSG$ = MSG$ + M$
70 GOTO 30 ‘wait for new message
100 IF INSTR(MSG$,”+CMTI:”) <> 0 THEN 200 ‘ new SMS arrived
105 IF INSTR(MSG$,PHONE$) <> 0 THEN 230 ‘ the SMS is from secure number
106 IF INSTR(MSG$,CMD$) <> 0 THEN 250 ‘ we recognized command
110 GOTO 20
200 ACTION=1 ‘ SMS is ready to be read
210 PRINT #1, “AT+CMGR=1” : PAUSE 200 ‘ read the SMS in memory
220 GOTO 20
230 ACTION=2 ‘ SMS comes from secure phone
240 GOTO 20
260 ACTION = 0: PRINT #1, “AT+CMGD=1” ‘delete the SMS
270 GOTO 20

so this 23 lines code receives and executes commands from secured phone number.

With this code above I hope you understood how easy is to control embedded devices vis GSM cellular network and DuinoMite. It took me two hours to write the code and this article, you can try to do same in other language and tell me the result 🙂