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

Status
Not open for further replies.

#### truth_seeker

##### Member level 1
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 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);
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 );
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 (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]

#### the_risk_master

##### Advanced Member level 2
send at commands c

seems to be no problem in your SC, BTW what error messages does your compiler giving?

digirke

### digirke

Points: 2

#### snilson

##### Newbie level 3
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#"};

digirke

### digirke

Points: 2

#### truth_seeker

##### Member level 1
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
program to send at commands

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

Good luck

V
Points: 2

Points: 2

### Dhruv@aks

Points: 2

#### randell_xtian

##### Full Member level 4
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

#### ckshivaram

##### Advanced Member level 5
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
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
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
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
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
at commands microcontroller c programming

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

#### arthur0

##### Full Member level 2
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
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

##### Advanced Member level 3
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
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
gsm at commands *99# login

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

##### Advanced Member level 3
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
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
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 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);
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 );
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 (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.