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.

[51] Problem in sending SMS through GSM module using 8051

Status
Not open for further replies.

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
iam doing a project on sms sending through gsm
using SIM 900 module and 8051
My code is:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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;
void serial_init(void);
void serial(unsigned char);
void puts(unsigned char *p );
void delay(void);
 
void main()
{
serial_init();
puts(command);
delay(); // delay of approx 1 sec
puts(echo);
delay();
puts(msgConfig);
delay();
puts(number);
delay();
puts(message);
delay();
puts(CTRLZ);
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 by a moderator:

hobbyckts

Advanced Member level 1
Joined
Mar 12, 2015
Messages
481
Helped
88
Reputation
176
Reaction score
85
Trophy points
28
Activity points
2,457
Can you test the GSM separately first instead of testing along with the microcontroller? Then it will be easy to analyse the problem.

Or else try to monitor the GSM for each and every command sends by the microcontroller
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Show the void main() code. Commands should end with \r or \r\n. Try sending "AT\r\n". Also applies for other commands.
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
I have tried at commands for calling.. Its working

- - - Updated - - -

Show the void main() code. Commands should end with \r or \r\n. Try sending "AT\r\n". Also applies for other commands.
I did the same AT commands on arduino and its working...
One addition I did is.. After sending each command I send \r (13) hex code 0x0D(maybe)

It works in arduino not in 8051
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Maybe UART baudrate problem. It will be good if you zip and post the complete project files.
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
We have to check the project settings in IDE. So, zip and post the complete Keil project files. We have to check if correct crystal frequency is used in the IDE.
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
We have to check the project settings in IDE. So, zip and post the complete Keil project files. We have to check if correct crystal frequency is used in the IDE.

here it is...

- - - Updated - - -


i followed your code (only of the sms sending)
problem here is iam receiving the blank sms

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include"lcd.h"                                             
#include<reg51.h>
#include<intrins.h>
#include<string.h>
void gsm_send(unsigned char *);
unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
unsigned char *ch3="Hi world\r\n";
unsigned char *ch9="+918283046191";
unsigned char a[2],cha,ph[14],msg[10],count=0,count1=0;
void main()
{
    
unsigned char j=0,cha;
    init();
P1=0x00;
P2=0x00;
 
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';     // dont know what is this please explain
a[1]='\0';   // and this also
gsm_send();
while(1);
}
void data_send(unsigned char *p)
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}
void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;
while(RI==0);
ch=SBUF;
i++;
}
}
void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);              // dont know this 1
data_send(ch9);
data_send(a);              // and this 1 also
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}

 

Attachments

  • GSM.rar
    22.3 KB · Views: 15
Last edited by a moderator:

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138
does your msg sending part is working?did you get success in sending sms with the code? a[2] is used for sending the null char probably used for the start and stop recognition for the mobile number while retrieving the msg from gsm.
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
does your msg sending part is working?did you get success in sending sms with the code? a[2] is used for sending the null char probably used for the start and stop recognition for the mobile number while retrieving the msg from gsm.

i studied your code.. some parameters i did not got.. so i just copied your code.. edit it for SMS only
but the problem iam getting is the msg is empty

code is

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<reg51.h>
#include<intrins.h>
#include<string.h>
 
void gsm_send(unsigned char *);
void data_res();
 
 
unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
unsigned char *ch3="Hi world\r\n";
 
unsigned char *ch9="+918283046191";
unsigned char a[2];
void main()
{
 
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send();
 
while(1);
}
 
void data_send(unsigned char *p)
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}
 
 
void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);
data_send(ch9);
data_send(a);    // this may be problem for blank text, but when i removed it , there is no text msg
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}
 
void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;
while(RI==0);
ch=SBUF;
i++;
}
}


iam getting SMS
but its blank
Hello world supposed to be in the text

- - - Updated - - -

Try adding this.

Code:
P3 = 0x03;

Did'nt helped sir
 
Last edited by a moderator:

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
Try this code. Compile for 11.0592 MHz. Test in Proteus before testing in hardware. Proteus doesn't have 89S52 so compile for AT89C52 for Proteus.

Code:
#include <reg51.h>
#include <intrins.h>
#include <string.h>

char cmd1[] = "AT\r\n";
char cmd2[] = "AT+CMGF=1\r\n";
char cmd3[] = "AT+CMGS=\"8283046191\"\r\n";
char msg[] = "Hi from AT89S52";

void UART_Write(char);
void UART_Write_Text(char *);
void Delay_ms(unsigned long);
	
void main() {

	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x03;

	SCON = 0x50;
	TMOD = 0x20;
	TH1 = 0xfd;
	TR1 = 1;

	while(1) {
		
				UART_Write_Text(cmd1);
				Delay_ms(1000);
				UART_Write_Text(cmd2);
				Delay_ms(1000);
				UART_Write_Text(cmd3);
				Delay_ms(1000);
				UART_Write_Text(msg);
				Delay_ms(500);
				UART_Write(0x1A);
				Delay_ms(8000);		
	}
}


void UART_Write(char s) {	
	SBUF = s;
	while(TI == 0);
	TI = 0;	
}

void UART_Write_Text(char *s) {
	
	while(*s)
			UART_Write(*s++);	
}	

void Delay_ms(unsigned long value) { 
	unsigned long x, y;
		
	for(x = 0; x < value; x++)
			for(y = 0; y < 1275; y++);
}
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
Try this code. Compile for 11.0592 MHz. Test in Proteus before testing in hardware. Proteus doesn't have 89S52 so compile for AT89C52 for Proteus.

Code:
#include <reg51.h>
#include <intrins.h>
#include <string.h>

char cmd1[] = "AT\r\n";
char cmd2[] = "AT+CMGF=1\r\n";
char cmd3[] = "AT+CMGS=\"8283046191\"\r\n";
char msg[] = "Hi from AT89S52";

void UART_Write(char);
void UART_Write_Text(char *);
void Delay_ms(unsigned long);
	
void main() {

	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x03;

	SCON = 0x50;
	TMOD = 0x20;
	TH1 = 0xfd;
	TR1 = 1;

	while(1) {
		
				UART_Write_Text(cmd1);
				Delay_ms(1000);
				UART_Write_Text(cmd2);
				Delay_ms(1000);
				UART_Write_Text(cmd3);
				Delay_ms(1000);
				UART_Write_Text(msg);
				Delay_ms(500);
				UART_Write(0x1A);
				Delay_ms(8000);		
	}
}


void UART_Write(char s) {	
	SBUF = s;
	while(TI == 0);
	TI = 0;	
}

void UART_Write_Text(char *s) {
	
	while(*s)
			UART_Write(*s++);	
}	

void Delay_ms(unsigned long value) { 
	unsigned long x, y;
		
	for(x = 0; x < value; x++)
			for(y = 0; y < 1275; y++);
}


Thanks sir that helped..
but i am still wondering what makes your code different from my code..
you are sending /r /n after each command so am i..
you sending each char of array one by one.. so i do
And your code supposed to be sending commands infinite times( between while(1))
i didn't seen that.. my mistake.. that costs me
i got 3 messages.. but the delivery is slow...(i dont think its the network issue)..previously when iam doing it with arduino it works and the delivery system is fast.. is that because of the code??
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138
Code:
#include<reg51.h>
#include<intrins.h>
#include<string.h>

void gsm_send(unsigned char *);
void data_res();


unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
unsigned char *ch3="Hi world\r\n";

unsigned char *ch9="+918283046191";
unsigned char a[2];
void main()
{

SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send("MOTOR OFF");

while(1);
}

void data_send(unsigned char *p)
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}


void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);
data_send(ch9);
data_send(a); 
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}

void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;
while(RI==0);
ch=SBUF;
i++;
}
}

this is the difference in your code and reason for having blank msg because you sending blank msg.if speed is issue then try with higher baud rate and reduce the delay in milan code.
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
Code:
#include<reg51.h>
#include<intrins.h>
#include<string.h>

void gsm_send(unsigned char *);
void data_res();


unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
unsigned char *ch3="Hi world\r\n";

unsigned char *ch9="+918283046191";
unsigned char a[2];
void main()
{

SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send("MOTOR OFF");

while(1);
}

void data_send(unsigned char *p)
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}


void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);
data_send(ch9);
data_send(a); 
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}

void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;
while(RI==0);
ch=SBUF;
i++;
}
}

this is the difference in your code and reason for having blank msg because you sending blank msg.if speed is issue then try with higher baud rate and reduce the delay in milan code.

sir, which lines of the code you have changed? what you've edited??
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138
your code at post#12
Code:
void main()
{
 
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send();
 
while(1);
edited part or code see last line of main and check your gsm_send ();function. read the sample code again you can understand.
Code:
void main()
{

SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send("MOTOR OFF"); //edited line

while(1);
}
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
In my code the last line inside while(1) loop is an 8 second delay. Reduce it to 3 sec if you want to send messages faster. Also reduce the 1 sec delays to 500 ms delays and try.
 

phrbob93

Member level 1
Joined
Mar 28, 2014
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Location
Jalandhar, punjab
Activity points
343
edited part or code see last line of main and check your gsm_send ();function. read the sample code again you can understand.
Code:
void main()
{

SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send("MOTOR OFF"); //edited line

while(1);
}


here what i have edited

Code:
#include<reg51.h>
#include<intrins.h>
#include<string.h>

void gsm_send(unsigned char *);
void data_res();


unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
//unsigned char *ch3="Hi world\r\n";

unsigned char *ch9="+918283046191";
unsigned char a[2];
void main()
{

SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
gsm_send("hello");    // edited

while(1);
}

void data_send(unsigned char *p)
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}


void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);
data_send(ch9);
data_send(a);
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}

void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;
while(RI==0);
ch=SBUF;
i++;
}
}

still getting blank sms

- - - Updated - - -

In my code the last line inside while(1) loop is an 8 second delay. Reduce it to 3 sec if you want to send messages faster. Also reduce the 1 sec delays to 500 ms delays and try.



nope it didn't worked..
i have tried with different delays.. but it seems that your code works with only particular delay that u gave first
 

ud23

Advanced Member level 3
Joined
Apr 20, 2011
Messages
926
Helped
184
Reputation
368
Reaction score
180
Trophy points
1,323
Activity points
6,138

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void gsm_send(unsigned char *ch3)
{
data_send(ch);
//data_res();
delay(100);
data_send(ch1);
delay(100);
//data_res();
data_send(ch2);
delay(100);
//data_res();
data_send(a);
data_send(ch9);
data_send(a);
data_send("\r\n");
delay(100);
data_send(ch3);
delay(100);
SBUF=0x1a;
while(TI==0);
TI=0;
}



sorry try with this edited function.use your delay function in above code.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top