How to send SMS using AT commands in mikrobasic PRO???

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
eng.danar
Posts: 5
Joined: 27 Jan 2009 00:06

How to send SMS using AT commands in mikrobasic PRO???

#1 Post by eng.danar » 19 Oct 2009 18:44

Hiiiiiiiiii all
please help me
Does any1 know how to send SMS through a modem and read it????
im working on PIC16F887 and WaveCOM Fastrack m1206 modem.
i connected the TX and the RX throught max232
and i try this example from the site:

Code: Select all

program GSM_Test

' *
' * Project name:
'     GSM_Test
' * Copyright:
'     (c) MikroElektronika, 2009.
'  * Revision History:
'     20090511:
'       - initial release - Slavisa Zlatanovic;
' * Description:
'     This is a simple project which demonstrates the use of Simcom SIM340Z GSM/GPRS module.
'     Upon programming, connect your EasyGSM/GPRS SIM340Z to PORTC of your EasyPIC5 development board.
'     The goal of this simple example is to display some basic functions of the GSM module like
'     making and answering a call and hanging up. Note that you'll have to enter the number you wish
'     to dial to in the atc3 constant.
' * Test configuration:
'     MCU:             PIC16F887
'                      http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf#page=123
'     Dev.Board:       EasyPIC5
'                      http://www.mikroe.com/en/tools/easypic5/
'     Oscillator:      HS, 8.00000 MHz
'     Ext. Modules:    EasyGSM/GPRS SIM340Z extra board on PORTC
'                      http://www.mikroe.com/en/tools/gsm/easygsm-gprs-sim340z/
'     SW:              mikroC PRO for PIC
'                      http://www.mikroe.com/en/compilers/mikrobasic/pro/pic/
' * NOTES:
'     - Put the pull-down resistors on the PORTD
'     - Pull-up resistors on the RC6 and RC7 pins
'     - In order to connect EasyGSM/GPRS SIM340Z to EasyPIC5 dev. board you need to use 5V-3.3V voltage translator
'     - Used pins of the Simcom SIM340Z module:
'                                              1,2,3,4,5,6,7,8: VBATT
'                                              9,10,11,12,13,14,50,51: GND
'                                              34:  PWRKEY
'                                              42:  TXD   -  MCU's RC6
'                                              40:  RXD   -  MCU's RC7
'                                              44:  RTS   -  MCU's RC4
' *



' lcd module connections
dim LCD_RS as sbit at RB4_bit
    LCD_EN as sbit at RB5_bit
    LCD_D4 as sbit at RB0_bit
    LCD_D5 as sbit at RB1_bit
    LCD_D6 as sbit at RB2_bit
    LCD_D7 as sbit at RB3_bit
    LCD_RS_Direction as sbit at TRISB4_bit
    LCD_EN_Direction as sbit at TRISB5_bit
    LCD_D4_Direction as sbit at TRISB0_bit
    LCD_D5_Direction as sbit at TRISB1_bit
    LCD_D6_Direction as sbit at TRISB2_bit
    LCD_D7_Direction as sbit at TRISB3_bit
' end lcd module connections



' set of AT commands
const atc0 = "AT"                                ' every GSM comand starts with "AT"
const atc1 = "ATE0"                              ' disable echo
const atc2 = "AT+CFUN=1"                         ' set full functionality
const atc3 = "ATD0788488691;"                     ' place a call to phone number 123456789
                                                 ' instead of 123456789 insert your phone number
const atc4 = "ATH"                               ' hang up
const atc5 = "ATA"                               ' answer a call

' lcd interface messages
const LCD_MESSAGE_LENGTH = 16
const lcd1 = "Initializing..."
const lcd2 = "Power-Up GSM!!!"
const lcd3 = "RD3-Continue"
const lcd4 = "Ready!  RD0-Dial"
const lcd5 = "RD1-HNG RD2-ANSW"
const lcd6 = "Calling..."
const lcd7 = "Hanging Up!"
const lcd8 = "Answering!"
const lcd9 = "RING!!!"


' responses to parse
const GSM_OK  = 0
const GSM_RING  = 1
dim gsm_state as byte
dim response_rcvd as byte
dim responseID, response as short

dim txt_temp as string[20]                       ' temporary variable
dim temp_short as short                          ' temporary variable

' copy const to ram string
sub function CopyConst2Ram(dim const src as ^byte) as  string[20]
dim i as byte
i = 0
  while src^ <> 0

      result[i] = src^
      inc(src)
      inc(i)
   wend
   result[i] = 0
end sub

' uart rx interrupt handler
sub procedure interrupt()
dim tmp as byte

  if (PIR1.RCIF = 1) then                        ' do we have uart rx interrupt request

     tmp = UART1_Read()                          ' get received byte

' process reception through state machine
' we are parsing only: "OK", "RING" responses
select case gsm_state


        case 0  response = -1                    ' clear response
               if (tmp = "O") then               ' we have "O", it could be "OK"
                 gsm_state = 1                   ' expecting "K"
               end if
               if (tmp = "R") then               ' we have 'R', it could be "RING"
                 gsm_state = 10                  ' expecting 'I' or 'e'
               end if


        case 1  if (tmp = "K") then
                 response = GSM_OK               ' we have "OK" response
                 gsm_state = 50                  ' expecting CR+LF
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case 10 if (tmp = "I") then              ' we have 'I', it could be "RING"
                 gsm_state = 11                  ' expecting 'N'
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case 11 if (tmp = "N") then              ' we have 'N', it could be "RING"
                 gsm_state = 12                  ' expecting 'G'
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case 12 if (tmp = "G") then              ' we have 'G' ->
                 response = GSM_RING             ' we have "RING" response
                 gsm_state = 50                  ' expecting CR+LF
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case 50 if (tmp = 13) then               ' we have 13, it could be CR+LF
                 gsm_state = 51                  ' expecting LF
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case 51 if (tmp = 10) then               ' we have LF, response is complete
                 response_rcvd = 1               ' set reception flag
                 responseID = response           ' set response ID
                else
                 gsm_state = 0                   ' reset state machine
                end if


        case else                                ' unwanted character
                 gsm_state = 0                   ' reset state machine


    end select
 end if
end sub

' send ATC command
sub procedure send_atc(dim const s as ^char)
' send command string
   while(s^ <> 0 )
      UART1_Write(s^)
      inc(s)
   wend
' terminate command with CR
      UART1_Write(0x0D)
end sub

' get GSM response, if there is any
sub function get_response() as short

    if (response_rcvd <> 0) then

      response_rcvd = 0
      result = responseID

    else
      result = -1
    end if
end sub


' wait for GSM response
sub procedure wait_response(dim rspns as short)
  while (get_response() <> rspns)
   nop
  wend
end sub


' pause
sub procedure wait()
 Delay_ms(1000)
end sub


main:
   gsm_state = 0                                 ' initialization of state mashine parameters
   responseID = -1
   response = -1


' all pins as digital I/Os
  ANSEL  = 0
  ANSELH = 0

' set PORTD inputs
  TRISD0_bit = 1                                 ' place a call input
  TRISD1_bit = 1                                 ' hang up input
  TRISD2_bit = 1                                 ' answer a call input
  TRISD3_bit = 1                                 ' continue with PIC program

' set RTS pin to zero, we will use only RX i TX
  TRISC.B4 = 0
  PORTC.B4 = 0

' enable uart rx interrupt
  PIE1.RCIE   = 1
  INTCON.PEIE = 1
  INTCON.GIE  = 1

' setup lcd module
  Lcd_Init()
  Lcd_Cmd(_LCD_CURSOR_OFF)

' LCD_Out: power-up gsm
  txt_temp = CopyConst2Ram(@lcd2)
  LCD_Out(1,1,txt_temp)
  txt_temp = CopyConst2Ram(@lcd3)
  LCD_Out(2,1,txt_temp)

  UART1_init(9600)                              ' initialize USART module


' wait for start
while(PORTD.B3 = 0)
   nop
wend

  Lcd_Cmd(_LCD_CLEAR)

' LCD_Out: Initializing...
  txt_temp = CopyConst2Ram(@lcd1)
  LCD_Out(1,1,txt_temp)

  Delay_ms(5000)                                 ' wait for the GSM module to initialize it self

' negotiate baud rate
  while TRUE
    send_atc(@atc0)                              ' send "AT" string until gsm sets up its baud rade
    Delay_ms(100)                                ' and gets it correctly
    if (get_response() = GSM_OK) then break      ' if gsm says "OK" on our baud rate than we got it
    end if
  wend

' disable command echo
   send_atc(@atc1)
   temp_short = GSM_OK
   wait_response(temp_short)

' set full functionality
  send_atc(@atc2)
  wait_response(temp_short)

  while TRUE
    txt_temp = CopyConst2Ram(@lcd4)
    LCD_Out(1,1,txt_temp)
  ' LCD_Out: RB1-HNG RB2-ASW
    txt_temp = CopyConst2Ram(@lcd5)
    LCD_Out(2,1,txt_temp)

     if Button(PORTD, 0, 10, 1) then
      ' mobile originated call to specified number
        send_atc(@atc3)
        temp_short = GSM_OK
        wait_response(temp_short)
        Lcd_Cmd(_LCD_CLEAR)

      ' LCD_Out: Calling...
        txt_temp = CopyConst2Ram(@lcd6)
        LCD_Out(1,1,txt_temp)
        wait()
        wait()
     end if


     if Button(PORTD, 1, 10, 1) then
      ' disconnect existing connection
        send_atc(@atc4)
        temp_short = GSM_OK
        wait_response(temp_short)
        Lcd_Cmd(_LCD_CLEAR)
       
      ' LCD_Out: Hanging Up!
        txt_temp = CopyConst2Ram(@lcd7)
        LCD_Out(1,1,txt_temp)
        wait()
     end if


     if Button(PORTD, 2, 10, 1) then
      'answer a call              `
        send_atc(@atc5)
      ' wait_response(GSM_OK);
        temp_short = GSM_OK
        wait_response(temp_short)
        Lcd_Cmd(_LCD_CLEAR)
      
      ' LCD_Out: Answering!
        txt_temp = CopyConst2Ram(@lcd8)
        LCD_Out(1,1,txt_temp)
        wait()
     end if


       ' process gsm response
     if (response_rcvd <> 0 ) then
        response_rcvd = 0                        ' clear response received flag

        select case responseID


               case GSM_OK   nop                 ' do nothing

               case GSM_RING
                         'LCD_Out:RING!!!
                      txt_temp = CopyConst2Ram(@lcd9)
                      LCD_Out(1,1,txt_temp)
                      wait()
               case else  nop                    ' process illegal responses

        end select
     end if
  wend
end.

And its work very well i can make a call and hang up and answer a call.
i just want to know how to send a SMS and read it back.
[/quote]

MAN
Posts: 437
Joined: 11 Jan 2006 18:32
Location: Brasil

Re: How to send SMS using AT commands in mikrobasic PRO???

#2 Post by MAN » 20 Oct 2009 16:00

Hi;
eng.danar wrote:
i just want to know how to send a SMS and read it back.
At the same way that you use specifics commands to make a call, receive call, you can send
receive SMS too, the commands is specific to it.
Try that, maybe it helps you to start.

http://www.experts-exchange.com/Program ... 40368.html
http://www.developershome.com/sms/atCommandsIntro.asp
Working with you, for you!
MAN

eng.danar
Posts: 5
Joined: 27 Jan 2009 00:06

Thanks...But!

#3 Post by eng.danar » 20 Oct 2009 20:07

Thanks for the replay
i saw these before i try many things but didnt work
cause 1st you have to send this (")
and i try the hexa in this and ctrl+z
i try this:

Code: Select all

UART1_Write(0x41)              'A
  UART1_Write(0x54)            'T
  UART1_Write(0x2B)           '+
  UART1_Write(0x43)            'C
  UART1_Write(0x4D)           'M
  UART1_Write(0x47)            'G
  UART1_Write(0x53)            'S
  UART1_Write(0x3D)           '=
  UART1_Write(0x22)            ' "
  UART1_Write(0)                  'my no. : ..........
  UART1_Write(7)
  UART1_Write(8)
  UART1_Write(8)
  UART1_Write(4)
  UART1_Write(8)
  UART1_Write(8)
  UART1_Write(6)
  UART1_Write(9)
  UART1_Write(1)
  UART1_Write(0x22)              ' "
  UART1_Write(0x0D)             '<CR>
  delay_ms(1000)
  UART1_Write_Text("hi")        ' my text: hi
  delay_ms(1000)
  UART1_Write(0x26)              'Ctrl+Z
but didnt work dont know where is the mistake

BarryP
Posts: 517
Joined: 02 Apr 2007 03:57
Location: New Zealand

#4 Post by BarryP » 20 Oct 2009 20:22

Hi
Shouldn't The Number be In ascii ??

Code: Select all

  UART1_Write("0")                  'my no. : ..........
  UART1_Write("7")
  UART1_Write("8")
  UART1_Write("8")
  UART1_Write("4")
  UART1_Write("8")
  UART1_Write("8")
  UART1_Write("6")
  UART1_Write("9")
  UART1_Write("1")

eng.danar
Posts: 5
Joined: 27 Jan 2009 00:06

Problem Solved :)

#5 Post by eng.danar » 20 Oct 2009 20:38

i solved the problem by this code:
and its worked:

Code: Select all

UART1_Write_Text("AT+CMGS=")                           'AT+CMGS=
  Delay_ms(1000)
  UART1_Write(0x22)                                              ' "
  'Delay_ms(2000)
  UART1_Write_Text("0788488691")                         ' my no.....
  'Delay_ms(2000)
  UART1_Write(0x22)                                              ' "
  UART1_Write(0x0D)                                              ' <CR> mean (Enter)
  Delay_ms(2000)
  UART1_Write_Text("hi")                                        ' my Text
  UART1_Write(0x0D)                                             ' <CR> mean (Enter)
  Delay_ms(2000)
  UART1_Write(chr(26))                                          ' Ctrl+Z
  Delay_ms(2000)

  UART1_Write(0x0D)                                             ' <CR> mean (Enter)
  Delay_ms(2000)
Thanks for the problem who try to help me

eng.danar
Posts: 5
Joined: 27 Jan 2009 00:06

LoL

#6 Post by eng.danar » 20 Oct 2009 20:40

Thanks for the problem who try to help me
Lol sorry i mean:

Thanks for the ppl who tried to help me

geev8
Posts: 6
Joined: 15 Jun 2009 23:35

Re: Problem Solved :)

#7 Post by geev8 » 21 Jan 2010 20:47

eng.danar wrote:i solved the problem by this code:
and its worked:

Code: Select all

  UART1_Write(chr(26))                                          ' Ctrl+Z

is there any one can tell what char(26) contains, i want to know how do you send Ctlz+Z to modem.

suwanggoh
Posts: 15
Joined: 20 Jan 2010 07:08

#8 Post by suwanggoh » 29 Jan 2010 18:28

may i know at what baud rate u using?

thedemme
Posts: 5
Joined: 05 Feb 2010 04:57

AT COMMANDS IN mikroC via UART: using SET230 phone

#9 Post by thedemme » 05 Feb 2010 07:55

The codes in here are in microC environment... I am using P16F877A and a SONY ERICSON T230 phone...

I connected the 'tx' pin of the P16F877A to the 'rx' of SONY ERICSON T230 phone... It was able to call using AT via UART of P16F877A, but it was not able to send a message...

The line of codes for the call command using AT commands are actually working, but the codes for message sending using AT are not...
I thought, it was just in the manipulation of delays after each line of AT Commands. And I've already tried manipulating the delays, but still, it didn't work..


It would be of great help for me in my project if you could help me have even a simple codes in microC environment for sending message using AT commands.





Code: Select all


unsigned char* GSM_MSG1 = "AT\n";   //"ATQ0V1E1S0=0\r\n";
unsigned char* GSM_MSG16 = "\r";   //"ATQ0V1E1S0=0\r\n";
unsigned char* GSM_MSG6 = "0x0D\n";               //13   for enter
unsigned char* GSM_MSG20 = "ATD09098725653\n";
unsigned char* GSM_MSG8 = "AT+CFUN=0\n";
unsigned char* GSM_MSG9 = "AT+CFUN=1\n";
unsigned char* GSM_MSG10 = "AT+CMGF=1\n";
unsigned char* GSM_MSG11 = "AT+CMGS=\"09098725653\"\n";
unsigned char* GSM_MSG12 = "0x1a\n";
unsigned char* GSM_MSG13 = "ok 0X1a\n";
unsigned char* GSM_MSG14 = "AT+CMGS=1\n";
unsigned char* GSM_MSG15 = "AT+CMGS=1\n";

void main() {
int ctr;
TRISD= 0xff;
TRISB=0x0;
PORTB=0;
PORTD=0;

 while(1)
 {
if(PORTD==0x01)        // For the call command using AT commands.  The codes here are working...
{

      PORTB = 0X01;             // Test to see if programme started running.
      Delay_ms(1000);


      UART1_Init(9600);            // Initialize UART module at 9600 bps
      Delay_ms(1000);
  
      UART1_Write_Text(GSM_MSG1);
      Delay_ms(3000);
      
      UART1_Write_Text(GSM_MSG16);
      Delay_ms(2000);
      
      UART1_Write_Text(GSM_MSG20);
      Delay_ms(5000);
      
      UART1_Write_Text(GSM_MSG16);
      Delay_ms(15000);
      
      UART1_Write_Text(GSM_MSG6);
      Delay_ms(10000);

      UART1_Write_Text(GSM_MSG16);
      Delay_ms(5000);

      PORTB=0x00;
      Delay_ms(5000);
}
else if(PORTD==0x02)    //For the message sending using AT commands via UART... The codes here are not working...
{
      PORTB=0x02;

      UART1_Init(9600);                         // Initialize UART module at 9600 bps
      Delay_ms(1000);

     UART1_Write_Text(GSM_MSG1);
      Delay_ms(2000);

      UART1_Write_Text(GSM_MSG16);
      Delay_ms(3000);

      UART1_Write_Text(GSM_MSG10);
      Delay_ms(3000);

      UART1_Write_Text(GSM_MSG16);
      Delay_ms(3000);

      UART1_Write_Text(GSM_MSG11);
      Delay_ms(3000);

      UART1_Write_Text(GSM_MSG16);
      Delay_ms(4000);

      UART1_Write_Text(GSM_MSG13);
      Delay_ms(6000);


      PORTB=0x00;
      Delay_ms(5000);
}


else
{
PORTB=0;
}
 }

}  [quote][/quote]

thedemme
Posts: 5
Joined: 05 Feb 2010 04:57

Re: How to send SMS using AT commands in mikrobasic PRO???

#10 Post by thedemme » 05 Feb 2010 08:20

Acttually, we have the same problem... But, I'm using P16F877A and it does make a call using AT Commands, but it can't send and receive message...

capitancafe
Posts: 1
Joined: 10 Feb 2010 00:08

Re: How to send SMS using AT commands in mikrobasic PRO???

#11 Post by capitancafe » 10 Feb 2010 00:14

Your phone support "plain" text or is a PDU format for SMS?
If PDU, you need convert your text at this format, and reverse process to receive.
I´m in same project, and finally i store the sms in memory of phone (i need 3 o 4 messages only) and i send need stored sms.
I write my project in assembler, but not totally, and i´m begginer with microbasic.

ZASto
Posts: 44
Joined: 13 Mar 2007 21:17
Location: Serbia
Contact:

Re: How to send SMS using AT commands in mikrobasic PRO???

#12 Post by ZASto » 11 Feb 2010 15:57

Unfortunately, all code examples that are given for GSM modules are only for using modules as sort of mobile phone. Much more relevant sort of communication such as SMS, GPRS, .... despite bragging that they are a "Official Competence Center of Telit" :mrgreen:

As for sending SMS, here is some code written in Proton+ (snippets only)

Code: Select all

' Telit GM862 GPS module initialization
	ModuleInit:
	HRSOut "AT&K0", CR			' No handshake
	GoSub Delay100
	HRSOut "ATE0", CR			' ECHO Off
	GoSub Delay100
	HRSOut "ATV0", CR			' Numeric responses
	GoSub Delay100
	HRSOut "AT+ICF=3", CR       ' 
	GoSub Delay100
	HRSOut "AT+IPR=4800", CR
	GoSub Delay100
	HRSOut "AT+CFUN=1", CR
	GoSub Delay100
	HRSOut "AT#SELINT=2", CR
	GoSub Delay100
	HRSOut "AT#SMSMODE=1", CR
	GoSub Delay100
	HRSOut "AT+CMGF=1", CR
	GoSub Delay100
	HRSOut "AT+CSMP=17,167,0,0", CR     ' Message parameters
	GoSub Delay100
	HRSOut "AT+COPS=0", CR
	GoSub Delay100
	HRSOut "AT+CPMS=", 34, "SM", 34, ",", 34, "SM", 34, ",", 34, "SM", 34, CR
	GoSub Delay100
	Low SMSLED
	Low NoFixLED
	Low AlarmLED
Return

CheckSMS:
	HRSOut "AT+CPMS?", CR
	HRSIn Wait ("SM"), dummy, dummy, NoOfMessages
	HRSIn Wait (CR, LF, "0"), dummy
.
.
.
Return

' Composing SMS
SendNo:
	HRSOut "AT+CMGS=", CStr Telefon1, CR
	GoSub Delay2000
Return

SendMessage:
	High SMSLED
	GoSub SendTelNo
	GoSub Delay2000
	HRSOut "Some string here", LF
	GoSub EndOfMessage
	GoSub Delay50
	Low SMSLED
Return

EndOfMessage:
	HRSOut 26
	HRSIn dummy
	If dummy = "4" Then
		Print At 2, 1, "ERROR Returned"
	EndIf
	HRSIn Wait ("+CMGS: "), dummy
	GoSub Delay2000
	Cls
	Low SMSLED
Return
and so on...
Make no mistake between my personality and my attitude.
My personality is who I am.
My attitude depends on who you are.

Post Reply

Return to “mikroBasic PRO for PIC General”