Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C program to send AT Commands..is it correct???

Status
Not open for further replies.

truth_seeker

Member level 1
Joined
Dec 7, 2005
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,620
at commands in c

Hello all ,

my graduation project has a part where PIC microcontroller should be connected to a mobile phone , which will use the GPRS service to send the received data from the microcontroller

here is a code to send the needed AT Commands to the mobile phone to open the GPRS connection..it has no errors ..but still no response from the mobile , and the G sign which indicates that GPRS is activated doesn't appear on the mobile screen.

I use PIC 18F4550 , nokia 6610i mobile phone , and I used a zener diode instead of max232 between the microcontroller and the mobile phone.



Code:
#include <string.h>  
#include <p18f4550.h>
#include<portb.h>
#include<usart.h>	
#include<delays.h>
#define	false	0
#define true	1
#define maxsize 10

	


int i; 
char COMMAND[5] = {"AT","AT+CGATT=1","AT+CGDCONT=1,IP,INTERNET", "AT+CGACT=1,1","ATD*99#"};

char RESPONSES[5] = {"OK","OK","OK","OK","CONNECT"};

char tmpdata; // Used in receive function


int READY = false ;
int FLAG = true ;

  
void receive(int j);
void send(int k);
void initialize (void) ;



void main(void) 


    {
TRISD=0;
TRISCbits.TRISC6 =0;     
TRISCbits.TRISC7 =1; 
TRISAbits.TRISA4 =0; 
		
//configures Usart with boud rate 9600  ? (4*9600)/ 2400000 -1=624 
	
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_SYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,624);
PORTAbits.RA4= 1;

for (i=0;i<=4;++i)
{
send (i);
if(i==4) receive(7);
else   receive(2);
 while(FLAG)


        {
        if (strcmp(tmpdata,RESPONSES[i]))
   {
if (i==0)PORTDbits.RD0= 1;  // ok 
if (i==1)PORTDbits.RD1= 1;
if (i==2)PORTDbits.RD2= 1;
if (i==3)PORTDbits.RD3= 1;
if (i==4)PORTDbits.RD4= 1;
 Delay10TCYx( 5 );         
  READY = 1;
            FLAG = false;
        }

        else 


            {
if (i==0)PORTDbits.RD0= 0;  //error
if (i==1)PORTDbits.RD1= 0;
if (i==2)PORTDbits.RD2= 0;
if (i==3)PORTDbits.RD3= 0;
if (i==4)PORTDbits.RD4= 0;

Delay10TCYx( 5 );
            send(i);
            if(i==4) receive(7);
            else   receive(2);
        }

    }

    if (READY == 1)


        {
        FLAG = true;
    }

  }
  
}

void receive( int j)


    {
  
 if( DataRdyUSART( ))
     getsUSART(tmpdata,j); //read data ;
           

    }  





void send ( int k)

{
putsUSART(COMMAND[i]);
while(BusyUSART());
}



Please tell me if there is anything wrong with this code...

is this the right way to send the AT Commands using C language , or there is another way???

note: on hyper terminal ..the command ..AT+CGDCONT=1,IP,INTERNET ..is written ..AT+CGDCONT=1,"IP","INTERNET"..but the compiler gives an error when I write it this way using C language..why?? is it maybe the problem??

Thanks a lot :|
[/code]
 

snilson

Newbie level 3
Joined
May 23, 2008
Messages
4
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,307
how to send a at command through c programming

Hi,

try using this:

char COMMAND[5] = {"AT","AT+CGATT=1","AT+CGDCONT=1,\"IP\",\"INTERNET\"", "AT+CGACT=1,1","ATD*99#"};
 

truth_seeker

Member level 1
Joined
Dec 7, 2005
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,620
sending at commands using a c program

Hello ,

thank you guys for replying

as I said..the code doesn't generate any errors....but still no response ..and i don't know what the problem is!!!!!!

snilson...Thanks for your suggestion , I tried it , and you are right..this is how it should be written!!

Can anyone tell me what to do to get a response..or at least to know where the problem is??!!!

Thanks a lot
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
program to send at commands

This was my GSM project which worked perfectly. Extract only the GSM part

Good luck
 

randell_xtian

Full Member level 4
Joined
Mar 25, 2008
Messages
203
Helped
28
Reputation
56
Reaction score
15
Trophy points
1,298
Location
Philippines
Activity points
2,667
pic send at commands c

the codes seems ok! but what would be the response of the PIC to the hyperterminal? i cannot test it here becoz i'm to busy in designing some projects.

regards,
randell_xtian
:D:D:D:D
 

ckshivaram

Advanced Member level 5
Joined
Apr 21, 2008
Messages
5,070
Helped
2,149
Reputation
4,304
Reaction score
2,089
Trophy points
1,393
Location
villingen (Germany) / Bangalore
Activity points
30,086
at command + c program

That was a code written for GSM modem using a 8 bit controller. You can add you AT commands to that or test the modem using only hyperterminal to confirm if the modem responds properly, In which case you can suspect the code or else you cannot debug the modem.
 

truth_seeker

Member level 1
Joined
Dec 7, 2005
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,620
send at commands by c

Hello ,

ckshivaram ..thank you very much for the attachment ..the code is very good..but I haven't tried it on my microcontroller yet..hope it works on it , but I still need to understand why my code doesn't work!!!

randell_xtian , when I connected the PIC to the hypertermianl..it gave me meaningless characters and symbols on the hyperterminal..what does that mean??

thank you guys
 

TekUT

Full Member level 6
Joined
Jun 17, 2008
Messages
323
Helped
39
Reputation
78
Reaction score
15
Trophy points
1,298
Location
Italy
Activity points
3,557
at+cgdcont .c

May be there is some mismatch on the serial communication baudrate? Most of time this is the mainly reason for communication trouble between micro and modem or phone.

Cheers
F.F. a.k.a. Powermos
 

rus40

Newbie level 6
Joined
Aug 15, 2009
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Bulgaria
Activity points
1,356
send at command c

Hello i want to know what will happen if the GPRS connection suddenly fail(i mean if something happens with the GSM Cell)? Will i gonna loose data and is there any AT command to prevent this?
Thanks for the response in advance!!
 

randell_xtian

Full Member level 4
Joined
Mar 25, 2008
Messages
203
Helped
28
Reputation
56
Reaction score
15
Trophy points
1,298
Location
Philippines
Activity points
2,667
how to send at command c

the device is communicating.
see the PIC micro respond to the signal give by the Hyperterminal. the only one that i can see possible error is the message transmitted by the device. for example when i press any key in my keyboard(computer), what would be the value of the hyperterminal(message?). (connect the device to hyperterminal and press any key. see the respond of the hyperterminal).

to rus40
when their is no available signal coming from a cell site. the device should be automatically save its previous data.
yes there is a command respond on this situation.
 

saeed_pk

Full Member level 4
Joined
May 20, 2006
Messages
238
Helped
35
Reputation
68
Reaction score
27
Trophy points
1,308
Location
Islamabad, Pakistan
Activity points
2,667
at commands microcontroller c programming

can you upload the schematic you are using i suspect something is wring there
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
at commands in c

Hi every1,
I don't know which compiler will let you get away with writing
Code:
char COMMAND[5] = {"AT","AT+CGATT=1","AT+CGDCONT=1,\"IP\",\"INTERNET\"", "AT+CGACT=1,1","ATD*99#"};
but if there is one, change it because it’s no good…
Where I learnt C, they taught me to write
Code:
char *COMMAND[] = {"AT","AT+CGATT=1","AT+CGDCONT=1,\"IP\",\"INTERNET\"", "AT+CGACT=1,1","ATD*99#"};
char *RESPONSES[] = {"OK","OK","OK","OK","CONNECT"}; 
char *tmpdata; // Used in receive function
instead.
You would then access the commands like COMMAND[0] for "AT", COMMAND[1] for "AT+CGATT=1" and so on. The same goes for RESPONSES.
How could you guys miss that? …or did I totally miss something?!

Arthur
 

seadolphine2000

Advanced Member level 3
Joined
Apr 12, 2005
Messages
881
Helped
122
Reputation
244
Reaction score
86
Trophy points
1,308
Activity points
7,372
at-commands 18f4550

truth_seeker,
Check this topic, I think you will find it useful.


Please note, the HyperTerminal sends the AT-Command as a string. In C, you will need to send the Double-Quote as well so, send the ASCII of the Double-Quote before and after the command. The ASCII of the " is 34 in decimal.

I hope this helps.
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
programs in c to send at command

No seadolphine2000, it doesn't help.
The post you indicate is yet just another proof of cowboy programming.
You don’t need to send the quotation marks! It’s not the quotation marks that make the string a “string”! Quotation marks are just a way of making the compiler understand what we want from it, nothing else.
Someone in there even said
DONT send char by char
instead dump the whole string
which is complete bullsh*t! The only way to do it is to send one char at the time (whether you do it or the library does it for you).
I think you would be better off by starting with a basic course in C programming first…

The only truth that post reveals is that, depending on the device you’re talking to, you might need to send carriage return (CR = 0x0d) and/or line feed (LF = 0x0a). Refer to the device’s manual and do as indicated there.

Arthur
 

rus40

Newbie level 6
Joined
Aug 15, 2009
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Bulgaria
Activity points
1,356
gsm at commands *99# login

Thanks for the answer randell_xtian ! :)
Can you tell me the AT command which response to that situation?
 

seadolphine2000

Advanced Member level 3
Joined
Apr 12, 2005
Messages
881
Helped
122
Reputation
244
Reaction score
86
Trophy points
1,308
Activity points
7,372
at+command+c+program

You don’t need to send the quotation marks! It’s not the quotation marks that make the string a “string”! Quotation marks are just a way of making the compiler understand what we want from it, nothing else.

Thanks Arthur0 for the correction, I'm sure that you are right regarding the C-Language. However, I'm talking about something I tested it myself and it works this way, at least in both "MikroBasic" and "Visual Basic".

Thanks again.
 

thand-s

Newbie level 4
Joined
Sep 17, 2009
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
South Africa
Activity points
1,314
Hi all.

I'm trying to interface the hex keypad with my GSM project.display the out put oc the LCD.the issue I'm having is that I cant see what I'm typing on the one LCD.

Please advise .I'm using the 8051 for this project.
 

kunal123

Advanced Member level 4
Joined
Jan 24, 2011
Messages
105
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
mumbai
Activity points
1,961
Re: at commands in c

hello all,
i want to interface gsm cell phone(nokia c5) to avr micrcontroller using usb cable is i require any level converter to send & receive text message for home automation.
my connectinon is as follows
microcontroller TTL TX ----> cell phone RX(through USB cabel)
microcontroller TTL RX ----> cell phone TX(through USB cabel)
microcontroller VCC ----> cell phone VCC(through USB cabel)
microcontroller GND ----> cell phone GND(through USB cabel)

is my connection right?
if yes, when i coneect cell phone to controller(using above connection) & transmit at command, but no responce from cell phone.

can anyone post a schematic.

thanks

Hello all ,

my graduation project has a part where PIC microcontroller should be connected to a mobile phone , which will use the GPRS service to send the received data from the microcontroller

here is a code to send the needed AT Commands to the mobile phone to open the GPRS connection..it has no errors ..but still no response from the mobile , and the G sign which indicates that GPRS is activated doesn't appear on the mobile screen.

I use PIC 18F4550 , nokia 6610i mobile phone , and I used a zener diode instead of max232 between the microcontroller and the mobile phone.



Code:
#include <string.h>  
#include <p18f4550.h>
#include<portb.h>
#include<usart.h>	
#include<delays.h>
#define	false	0
#define true	1
#define maxsize 10

	


int i; 
char COMMAND[5] = {"AT","AT+CGATT=1","AT+CGDCONT=1,IP,INTERNET", "AT+CGACT=1,1","ATD*99#"};

char RESPONSES[5] = {"OK","OK","OK","OK","CONNECT"};

char tmpdata; // Used in receive function


int READY = false ;
int FLAG = true ;

  
void receive(int j);
void send(int k);
void initialize (void) ;



void main(void) 


    {
TRISD=0;
TRISCbits.TRISC6 =0;     
TRISCbits.TRISC7 =1; 
TRISAbits.TRISA4 =0; 
		
//configures Usart with boud rate 9600  ? (4*9600)/ 2400000 -1=624 
	
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_SYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_LOW,624);
PORTAbits.RA4= 1;

for (i=0;i<=4;++i)
{
send (i);
if(i==4) receive(7);
else   receive(2);
 while(FLAG)


        {
        if (strcmp(tmpdata,RESPONSES[i]))
   {
if (i==0)PORTDbits.RD0= 1;  // ok 
if (i==1)PORTDbits.RD1= 1;
if (i==2)PORTDbits.RD2= 1;
if (i==3)PORTDbits.RD3= 1;
if (i==4)PORTDbits.RD4= 1;
 Delay10TCYx( 5 );         
  READY = 1;
            FLAG = false;
        }

        else 


            {
if (i==0)PORTDbits.RD0= 0;  //error
if (i==1)PORTDbits.RD1= 0;
if (i==2)PORTDbits.RD2= 0;
if (i==3)PORTDbits.RD3= 0;
if (i==4)PORTDbits.RD4= 0;

Delay10TCYx( 5 );
            send(i);
            if(i==4) receive(7);
            else   receive(2);
        }

    }

    if (READY == 1)


        {
        FLAG = true;
    }

  }
  
}

void receive( int j)


    {
  
 if( DataRdyUSART( ))
     getsUSART(tmpdata,j); //read data ;
           

    }  





void send ( int k)

{
putsUSART(COMMAND[i]);
while(BusyUSART());
}



Please tell me if there is anything wrong with this code...

is this the right way to send the AT Commands using C language , or there is another way???

note: on hyper terminal ..the command ..AT+CGDCONT=1,IP,INTERNET ..is written ..AT+CGDCONT=1,"IP","INTERNET"..but the compiler gives an error when I write it this way using C language..why?? is it maybe the problem??

Thanks a lot :|
[/code][/QUOTE]
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top