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


Image

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😉

PLAYING WITH MOD-GSM AT COMMANDS

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:

SIMCOM_Ltd
SIMCOM_SIM300D
Revision:1008B13SIM300D32_SST34HF3284

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:

Image

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

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

Image

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
+CSPN: “BG GLOBUL”,1

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

PLACING AND TAKING CALLS

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
100 IF INSTR(MSG$,”RING”) <> 0 THEN PRINT “INCOMING RING DETECTED!”
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:

AT+CMGS=”+359897123342″
message+CTRL-Z

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:

AT+CMGR=1

the result is:

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

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
250 IF ACTION = 2 THEN PRINT “COMMAND RECEIVED AND EXECUTING…”
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🙂

13 Comments (+add yours?)

  1. harianggoro
    Mar 15, 2012 @ 22:01:21

    good information … I permit bookmark your webiste

    Reply

  2. Andrew Rich
    Apr 06, 2012 @ 03:43:57

    So how do you establish a GPRS sessions and send a UDP packet ?

    Reply

  3. Olivier
    Jun 17, 2012 @ 16:11:34

    Hello

    I have an “invalid syntax” message with the line 10 OPEN “COM3:9600″ AS #1 ; do you know why ?
    My Duinomite seem to accept only com 1 and 2 (firmware is 3.1).

    Reply

    • OLIMEX Ltd
      Jun 17, 2012 @ 18:42:02

      Hi
      you are using MM firmware not Duinomite-Basic firmware

      Reply

      • Olivier
        Jun 17, 2012 @ 20:50:53

        Thanks for your response

        You mean it could work only before I made the update with the 3.1 version ?
        Is there any instruction to send data through the UEXT connector with this version ?

        and sorry for my poor english…

      • OLIMEX Ltd
        Jun 17, 2012 @ 22:13:27

        please read MM basic documentation as I’m note very familiar with this dirmware, the MM basic author Geoff Graham didn’t implement yet all features which DM-BASIC had implemented for DuinoMite so the USB mass storage, SETUP command, real UART at high speed, real SPI at high speed etc are not supported yet

  4. pici
    Feb 09, 2013 @ 00:16:39

    what’ s the voltage level of rx/tx – 5V or 3.3V ?

    Reply

  5. Panos
    Apr 06, 2013 @ 14:03:13

    This is a great help, fit to such a great hardware! Thank you guys.

    I tried everything and it all works fine. Everything but dialing.

    I type ATDxx(number)xx, but all I get after a few seconds is “NO CARRIER”. I know that I am connected to the network because I can receive calls and exchange SMS with other phones. Any ideas on my problem?

    Reply

  6. Igal
    Jul 24, 2013 @ 12:44:10

    I has similar problem resolved by changing SIM Card, something probably limited by service provider

    Reply

  7. GregCal
    Nov 02, 2013 @ 08:44:23

    I loose a lot of time trying to operate MOD-GSM with DuinoMite eMega without success; especially I could not receive information from the GSM.

    Then I note, in the photographs, that you connected MOD-GSM with only pin 1,2,3,4 of UEXT so I cut the ribbon cable and got every thing operating!!.

    I read a lot of other solution to operate DuinoMite eMega with MOD-GSM, but none of them work, just cut the ribbon cable as mentioned and enjoy!!

    I’m using:
    DMBasic Build Date: Aug 2 2012 Time:13:45:19
    > PRINT MM.VER
    2.7004

    Reply

  8. Redwane
    Feb 18, 2015 @ 00:32:14

    hello everyone,
    – I never work with the AT command, it is the software that I can use to execute “DM-BASIC code” ?
    – is these steps are verified for olimixino A20 ??
    sorry for my poor english

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: