• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem in sending SMS through GSM module using 8051

iam doing a project on sms sending through gsm
using SIM 900 module and 8051
My code is:

#include<reg51.h>
unsigned char *command = "AT";
unsigned char *echo = "ATE0";
unsigned char *msgConfig = "AT+CMGF=1";
unsigned char *number = "AT+CMGS=\"8283******\"";
unsigned char *message = "hello";
unsigned char CTRLZ = 0x1A; // now as a variable
void serial_init(void);
void serial(unsigned char);
void puts(unsigned char *p );
void delay(void);

void main()
{
serial_init();
puts(command);
serial(0x0D); // /r after each command
delay(); // delay of approx 1 sec

puts(echo);
serial(0x0D);
delay();

puts(msgConfig);
serial(0x0D);
delay();

puts(number);
serial(0x0D);
delay();

puts(message);
serial(0x0D);
delay();

serial(CTRLZ); //editing here

while(1);

}
void serial_init(void)
{

TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
TH1=0xFD; //-3 to TH1 for 9600 baud rate
SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
TR1=1; // start timer
}

void puts(char *p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
serial(*temp);
temp++;
}
}

void serial(unsigned char x)
{

SBUF=x;
while(TI==0);
TI=0;

}
void delay(void) // delay for approx 1 sec
{
int i;
TMOD=0x01; // timer 0 in mode 1
for(i=0;i<142;i++)
{
TL0=0x00; // starting value from 0
TH0=0x00;
TR0=1; // sart timer
while(TF0==0); // polling TF flag for high
TR0=0; // stop timer
TF0=0; // clear flag TF0
//}
}
}

the problem here is SMS won't send by the GSM

when i use the calling function by replacing the AT commands then calling works
but SMS won't.. i think iam wrong in sending AT commands for SMS... please correct me
 
Last edited:

Cicero

Active Member
He is sending a CR after each line, separately, so its all good there.

I suspect it may be the final CTRLZ maybe?
Code:
unsigned char *CTRLZ = 0x1A;

puts(CTRLZ);
That doesn't send the 0x1A, you're sending the address. Try removing the pointer, unsigned char CTRLZ = 0x1A;

EDIT: Actually I'm wrong here, CTRLZ does contain 0x1A, its just declared as a pointer though. Which isn't quite right.
 
Last edited:
He is sending a CR after each line, separately, so its all good there.

I suspect it may be the final CTRLZ maybe?
Code:
unsigned char *CTRLZ = 0x1A;

puts(CTRLZ);
That doesn't send the 0x1A, you're sending the address. Try removing the pointer, unsigned char CTRLZ = 0x1A;

removed pointer...and i used variable... doesn't work
 

Cicero

Active Member
Theres also a problem with your puts routine.
Code:
void puts(char *p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
serial(*temp);
temp++;
}
}
This is only really for sending a string, because its waiting for your null character at the end. If you try send a single unsigned char like CTRLZ, it isn't a string, so it may not have a NULL after it, and it'll just increase its address and send random data until it finds one.

Rather send CTRLZ using your serial function, like you do for your 0x0D.

And actually, looking at the Arduino code, you will have to add in LF characters after all - you only send CR's.

Here's the Arduino code:
Code:
void sendSMS(String message)
{
  SIM900.print("AT+CMGF=1\r");                     // AT command to send SMS message
  delay(100);
  SIM900.println("AT + CMGS = \"+12128675309\"");  // recipient's mobile number, in international format
  delay(100);
  SIM900.println(message);                         // message to send
  delay(100);
  SIM900.println((char)26);                        // End AT command with a ^Z, ASCII code 26
  delay(100);
  SIM900.println();
  delay(5000);                                     // give module time to send SMS
  SIM900power();                                   // turn off module
}
println() prints both a CR and a LF character. Notice you only print a CR. Also, after printing the 0x1A, there's also a CRLF, and another CR LF when it sends a blank SIM900.println();
 
Last edited:

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
removed pointer...and i used variable... doesn't work
You have been given false info.... CTRLZ was fine as a pointer but it needed referencing as puts accepts a pointer.

puts(CTRLZ);

Should have been..
puts(*CTRLZ);

EDIT** Sorry..... but you will also have to NULL terminate...

Its best to use

serial(0x1A);
 

Cicero

Active Member
You have been given false info.... CTRLZ was fine as a pointer but it needed referencing as puts accepts a pointer.

puts(CTRLZ);

Should have been..
puts(*CTRLZ);
Actually you're wrong Ian. I admitted I was slightly mistaken.

Please reread my posts, and the edits.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
I know I know... I have just realized myself...

The CTRLZ should be used as you said.... I missed that post for some reason....

I was wrong as well with my second statement.... If he had just terminated the CTRLZ string with a NULL all this would be academic....
 

Cicero

Active Member
I know I know... I have just realized myself...

The CTRLZ should be used as you said.... I missed that post for some reason....

I was wrong as well with my second statement.... If he had just terminated the CTRLZ string with a NULL all this would be academic....
True, that would be fine.

It confused me (clearly) as well. As I understand it now...
CTRLZ we agree was a pointer. But as a pointer, it was assigned to 0x1A. So CTRLZ was in fact 0x1A. However, *CTRLZ was whatever is at address 0x1A - probably an access violation. Just feels a bit messy.

Like you said, he could've just gone:
Code:
unsigned char CTRLZ[] = {0x1A, 0x00};
And it would've been fine as well. Although I'm sure he needs LF's and CR's everywhere to match that Arduino tutorial.
 
I know I know... I have just realized myself...

The CTRLZ should be used as you said.... I missed that post for some reason....

I was wrong as well with my second statement.... If he had just terminated the CTRLZ string with a NULL all this would be academic....
Actually you're wrong Ian. I admitted I was slightly mistaken.

Please reread my posts, and the edits.
sir, i edited like you suggested... see the code above
but still got negative results
 
Repost your updated code please
#include<reg51.h>
unsigned char *command = "AT";
unsigned char *echo = "ATE0";
unsigned char *msgConfig = "AT+CMGF=1";
unsigned char *number = "AT+CMGS=\"8283******\"";
unsigned char *message = "hello";
unsigned char CTRLZ = 0x1A; // now as a variable
void serial_init(void);
void serial(unsigned char);
void puts(unsigned char *p );
void delay(void);

void main()
{
serial_init();
puts(command);
serial(0x0D); // /r after each command
delay(); // delay of approx 1 sec

puts(echo);
serial(0x0D);
delay();

puts(msgConfig);
serial(0x0D);
delay();

puts(number);
serial(0x0D);
delay();

puts(message);
serial(0x0D);
delay();

serial(CTRLZ); //editing here

while(1);

}
void serial_init(void)
{

TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
TH1=0xFD; //-3 to TH1 for 9600 baud rate
SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
TR1=1; // start timer
}

void puts(char *p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
serial(*temp);
temp++;
}
}

void serial(unsigned char x)
{

SBUF=x;
while(TI==0);
TI=0;

}
void delay(void) // delay for approx 1 sec
{
int i;
TMOD=0x01; // timer 0 in mode 1
for(i=0;i<142;i++)
{
TL0=0x00; // starting value from 0
TH0=0x00;
TR0=1; // sart timer
while(TF0==0); // polling TF flag for high
TR0=0; // stop timer
TF0=0; // clear flag TF0
//}
}
}
 

Cicero

Active Member
Yeah, you still haven't added any LF characters? You've only changed the pointer issue.

Another thing in the Arduino code, is that the number is in international format...not sure if thats necessary?

Try the code below which follows the Arduino code virtually as close as possible.
Code:
#include<reg51.h>
unsigned char *command = "AT";
unsigned char *echo = "ATE0";
unsigned char *msgConfig = "AT+CMGF=1";
unsigned char *number = "AT + CMGS =\"8283******\"";
unsigned char *message = "hello";
unsigned char CTRLZ = 0x1A; // now as a variable
void serial_init(void);
void serial(unsigned char);
void puts(unsigned char *p );
void delay(void);

void main()
{
serial_init();
/* Not running this as its not in the Arduino code */
#ifdef ECHO_CMD 
  puts(command);
  serial(0x0D); // /r after each command
  delay(); // delay of approx 1 sec

  puts(echo);
  serial(0x0D);
  delay();
#endif

puts(msgConfig);  // SIM900.print("AT+CMGF=1\r");
serial(0x0D);
delay();

puts(number); // SIM900.println("AT + CMGS = \"+12128675309\"");
serial(0x0D);
serial(0x0A);
delay();

puts(message); //SIM900.println("Hello, world. This is a text message from an Arduino Uno.");
serial(0x0D);
serial(0x0A);
delay();

serial(CTRLZ); // SIM900.println((char)26);
serial(0x0D);
serial(0x0A);
delay();

serial(0x0D); // SIM900.println();
serial(0x0A);
while(1);

}
void serial_init(void)
{

TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
TH1=0xFD; //-3 to TH1 for 9600 baud rate
SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
TR1=1; // start timer
}

void puts(char *p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
serial(*temp);
temp++;
}
}

void serial(unsigned char x)
{

SBUF=x;
while(TI==0);
TI=0;

}
void delay(void) // delay for approx 1 sec
{
int i;
TMOD=0x01; // timer 0 in mode 1
for(i=0;i<142;i++)
{
TL0=0x00; // starting value from 0
TH0=0x00;
TR0=1; // sart timer
while(TF0==0); // polling TF flag for high
TR0=0; // stop timer
TF0=0; // clear flag TF0
//}
}
}
 
Yeah, you still haven't added any LF characters? You've only changed the pointer issue.

Another thing in the Arduino code, is that the number is in international format...not sure if thats necessary?

Try the code below which follows the Arduino code virtually as close as possible.
Code:
#include<reg51.h>
unsigned char *command = "AT";
unsigned char *echo = "ATE0";
unsigned char *msgConfig = "AT+CMGF=1";
unsigned char *number = "AT + CMGS =\"8283******\"";
unsigned char *message = "hello";
unsigned char CTRLZ = 0x1A; // now as a variable
void serial_init(void);
void serial(unsigned char);
void puts(unsigned char *p );
void delay(void);

void main()
{
serial_init();
/* Not running this as its not in the Arduino code */
#ifdef ECHO_CMD
  puts(command);
  serial(0x0D); // /r after each command
  delay(); // delay of approx 1 sec

  puts(echo);
  serial(0x0D);
  delay();
#endif

puts(msgConfig);  // SIM900.print("AT+CMGF=1\r");
serial(0x0D);
delay();

puts(number); // SIM900.println("AT + CMGS = \"+12128675309\"");
serial(0x0D);
serial(0x0A);
delay();

puts(message); //SIM900.println("Hello, world. This is a text message from an Arduino Uno.");
serial(0x0D);
serial(0x0A);
delay();

serial(CTRLZ); // SIM900.println((char)26);
serial(0x0D);
serial(0x0A);
delay();

serial(0x0D); // SIM900.println();
serial(0x0A);
while(1);

}
void serial_init(void)
{

TMOD=0x20; //timer 1, mode 2(8-bit autoreload) to set baud rate
TH1=0xFD; //-3 to TH1 for 9600 baud rate
SCON=0x50; // 8 bit txion, 1 start 1 stop bit, REN enable for both txfr and rxve
TR1=1; // start timer
}

void puts(char *p)
{
char *temp = p; /*temp pointer so that the actual pointer is not displaced */
while(*temp != 0x00)
{
serial(*temp);
temp++;
}
}

void serial(unsigned char x)
{

SBUF=x;
while(TI==0);
TI=0;

}
void delay(void) // delay for approx 1 sec
{
int i;
TMOD=0x01; // timer 0 in mode 1
for(i=0;i<142;i++)
{
TL0=0x00; // starting value from 0
TH0=0x00;
TR0=1; // sart timer
while(TF0==0); // polling TF flag for high
TR0=0; // stop timer
TF0=0; // clear flag TF0
//}
}
}


not getting what you have edited..
after each command i added LF Characters

serial(0x0D); // /r
serial(0x0A); // /n
 

Latest threads

EE World Online Articles

Loading

 
Top