Discussion in "Project Help" started by    satish@iiit    Apr 2, 2015.
Thu Apr 02 2015, 11:33 pm
#1
Sir, my Project is Development of Android based Automatic Street Light Controller. In my project i want to control street lights with the help of android application. I developed the app.I am using 8051 and gsm modem for communication. My question is , If i send a message from app like 'ON A(i.e. street light)' to modem, i want to read this msg by 8051.I read a project in your website, "GSM based online noticeboard", but i am not getting anything from that program. Can you give me a brief idea to do read the sms from modem by 8051 so that i can give control signal to street lights.
Fri Apr 03 2015, 04:25 pm
#2
I am assuming you know how to use AT commands and what are the required AT commands for sms read and send. Forget about that project as of now. There are two ways you can implement SMS based projects:
1. You can use polling method
2. Use interrupt to get new message indication

First method is very simple, You fix a location which you want to monitor most of the cases people try to use location #1. so you keep on sending sms read command (AT+CMGR=1) to modem to get new sms, If there is a new sms you will get a reply starting from +CMGR otherwise you will only get "OK" no CMGR. after you read and process that sms you will delete that SMS using AT+CMGD=1 so that new sms comes to same location. this way you do polling method.

Second method is complex but very useful as you can do lot of things side by side apart from monitoring just single location. This method is used in that GSM notice board project. Whenever a new message comes Modem sends a notification response starting with +CMTI which tells a new message has arrived along with memory and location where message is stored. Usually SIM memory is used and new location index is passed along with the response e.g.
+CMTI: "SM",2
where SM is memory location -> SIM
and 2 is the index of new sms
So you can read new sms with command AT+CMGR=2
Now there is no defined time when this notification comes so this has to be done in interrupt context as soon as new byte comes from modem you do compare and check if its CMTI, if it matches then get the index of new message and read it in main loop.

Take a look at this post which explains the same method:
http://www.8051projects.net/t59304/embedded-gsm-development/gsm-modem-read-sms-example-using-interrupts.htm

Do post here if you have any question.
 satish@iiit like this.
Sun Apr 05 2015, 03:13 pm
#3
Thank u sir for your reply.
Now i understood the code some what and still i have some doubts in code. Those are
1. what is the use of " unsigned char abc,apt=0,count=0; ", "unsigned char rec_no[10]; ", and "unsigned char time_date[10]; ".
2. what is the use of variables ," bit flag1=0; ", " bit TI_flag = 0; " , " bit flag=0; "
2. u said that u will store response in mybuff, but what char array " msg1 " stores. i.e i am asking that is the msg1 array stores entire response?
Mon Apr 06 2015, 10:58 am
#4
You're now referring back to the project, he has written code for his own use.
1. Abc is used as a counter. Apt and count seems unused variable.
2. All flag variables seems unused.
3. Msg1 stores entire response, yes
Tue Apr 14 2015, 02:25 pm
#5
Sir, i am sending my project source code, please tell me where i did mistakes.
Wed Apr 15 2015, 02:46 pm
#6
Did you forget to attach file? Please check and attach again.
Wed Apr 15 2015, 06:34 pm
#7
sorry sir.... Here is the code. Could please give me the reply fast because i need to complete the project tomorrow.
#include<reg51.h>
#include<string.h>
#include<absacc.h>
#include<stdlib.h>
#include<ctype.h>
#define buffer 110

sbit rs = P3^5;
sbit e = P3^4;

void control_signal_to_mc();
void delay(unsigned int i);
void tx0(unsigned char);
void SMSString(unsigned char*text) ;
void init();
void clear(void);
void read_notification();
void clear_mybuff();
void delete_msg(void);
void read_no(unsigned char msg_array[]);
void store_msg(unsigned char msg_array[]);
void password();
void read_nd_check_password(unsigned char msg_array[]);
unsigned char abc,apt=0,count=0;
unsigned char idata msg1[buffer];
unsigned char rec_no[10];
unsigned char time_date[10];
unsigned char code Response[] = "+CMTI";
// Moblie no.of Owner
unsigned char code Mob_no[]= "9603658488";
unsigned char MyBuff[20], k = 0;
bit flag1=0;
unsigned char store[10];
bit NewMessage = 0;
bit TI_flag = 0;
bit flag=0;
bit No_correct=0;
void init()
{
P1=0xFF;
p2=0x00;
abc=0;
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}
void main ()
{
clear();
delay(10);
init();
IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2)
SMSString("AT\r"); // AT commands to initialize gsm modem
delay(50);

SMSString( "ATe0\r"); // turn off echo
delay(50);

SMSString( "AT&W\r"); // save settings
delay(50);

SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay(50);

SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
while(1)
{

IE=0x91;
k=0;
delay(10);
flag=0;
while(!NewMessage);
{
IE=0X00;
delay(10);
NewMessage = 0;
read_notification();
}
//New msg indication.
// +CMTI: "SM",3
}
}
void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
while (*text)
{
tx0(*text++);
}
}
void tx0(unsigned char x) //send data to serial port 0
{
EA=0;
SBUF=x;
while(TI==0);
TI=0;
EA=1;
}
void read_notification()
{
clear();
SMSString("AT+CMGR=");
tx0(MyBuff[12]);
SMSString("\r");
IE=0x90;
delay(200);
read_no(msg1);
IE=0x00;
if(No_correct==1)
{

store_msg(msg1);
control_signal_to_mc();
}
clear();

if(MyBuff[12]>='9') //After 9 msgs recieved it clears the Msg index from SIM
{
delete_msg();
}

clear_mybuff();

return;
}
void serial () interrupt 4 {
unsigned char ch;
if(RI){
ch = SBUF; //character is read
RI = 0; //clear flag
if((ch == Response[k]) || (k > 4))
{
if(ch != 0x0D) // 0x0D means data received..
{
MyBuff[k++] = ch;
}
else
{
MyBuff[k] = '\0';
NewMessage = 1;
k = 0;
}
}
else
{
msg1[abc]=ch;//received msg is stored.
abc++;
k = 0;
}
}
}
void read_no(unsigned char msg_array[]) // reads the no from received msg and compares it with stored msg, If found wrong then send a msg to owner.
{
unsigned char i;
if(Mob_no[0]==msg_array[23]&&
Mob_no[1]== msg_array[24]&&
Mob_no[2]==msg_array[25]&&
Mob_no[3]== msg_array[26]&&
Mob_no[4]==msg_array[27]&&
Mob_no[5]== msg_array[28]&&
Mob_no[6]==msg_array[29]&&
Mob_no[7]== msg_array[30]&&
Mob_no[8]==msg_array[31]&&
Mob_no[9]== msg_array[32])
{
No_correct=1;
}
else // Sends a msg to stored no if some unauthoriesd tries to send a msg.
{
No_correct=0;
SMSString("AT+CMGS=\"");
SMSString(Mob_no);
SMSString("\"\r");
delay(1);
SMSString("Msg from Unknown number:( ");
for(i=23;i<=32;i++)
{
tx0(msg_array[i]);
delay(0);
}
SMSString(") is received.");
tx0(0x1A); // Ctrl+z
delay(100);
}
return;
}
void read_nd_check_password(unsigned char msg_array[])
{
if(Pass_array[0]==msg_array[59]&&
Pass_array[1]== msg_array[60]&&
Pass_array[2]==msg_array[61]&&
Pass_array[3]== msg_array[62])
{

}
else
{
SMSString("AT+CMGS=\"");
SMSString(Mob_no);
SMSString("\"\r");
delay(1);
SMSString("Password Incorrect!!");
tx0(0x1A); // Ctrl+z
delay(10);
}
return;
}
void control_signal_to_mc()
{
if(strcmp(store,"ON A")==0){
unsigned char i = p1_0;
while(i){
p2_0 = 1;
}
}
else if(strcmp(store,"ON B")==0){
p2_1 = 1;
}
else if(strcmp(store,"ON C")==0){
p2_2 = 1;
}
return;
}
void clear(void)
{
unsigned int a;
for(a=0;a<buffer;a++)
msg1[a]=0x00;
abc=0;
}
void clear_mybuff()
{
unsigned int a;
for(a=0;a<25;a++)
MyBuff[a]=0x00;
}
void delete_msg(void)
{
SMSString("AT+CMGD=9\r");
delay(1);
SMSString("AT+CMGD=8\r");
delay(1);
SMSString("AT+CMGD=7\r");
delay(1);
SMSString("AT+CMGD=6\r");
delay(1);
SMSString("AT+CMGD=5\r");
delay(1);
SMSString("AT+CMGD=4\r");
delay(1);
SMSString("AT+CMGD=3\r");
delay(1);
SMSString("AT+CMGD=2\r");
delay(1);
SMSString("AT+CMGD=1\r");
delay(1);
return;
}
void delay(unsigned int i)
{
unsigned int k, l;
for(k=0;k<i;k++)
for(l=0;l<1000;l++);
}
Wed Apr 15 2015, 10:34 pm
#8


sorry sir.... Here is the code. Could please give me the reply fast because i need to complete the project tomorrow.

satish@iiit


you will probably need a few days !

Someone might spot a simple error, but rather than just wait,
start debugging in stages.

Start by connecting your GSM modem to a PC.
Use voltage level shifters if needed.
Make sure it can receive messages and record exactly what is
is being received, then post it here.
Wed Apr 15 2015, 11:17 pm
#9
Sir, I run the code in keil and it shows 0 errors and then i converted it into hex, dumped in to mc AT89S52 by using dumper. The problem is that when i insert the mc in to pcb, the power is not coming to the circuit but is there when there is no mc. one more thing is that i am using a pcb that uses AT89C52. I connected relays to port 2.0, 2.1,2.2(I am using relays to act as switches for bulbs i.e. for street lights) and i connected p1.0 to ldr. i think we can understand better if you read the "control_signal_to_mc()" function first. Sir, could you please give reply as fast as possible.
The source code is this one ( sorry previous code not exact code what i need):
#include<reg51.h>
#include<string.h>
#include<absacc.h>
#include<stdlib.h>
#include<ctype.h>
#define buffer 110

sbit rs = P3^5;
sbit e = P3^4;
sbit ldr = P2^0;
sbit um = P2^1;
sbit tm = P2^2;

void control_signal_to_mc();
void delay(unsigned int i);
void tx0(unsigned char);
void SMSString(unsigned char*text) ;
void init();
void clear(void);
void read_notification();
void clear_mybuff();
void delete_msg(void);
void read_no(unsigned char msg_array[]);
void store_msg(unsigned char msg_array[]);
unsigned char abc,
unsigned char idata msg1[buffer];
unsigned char rec_no[10];
unsigned char time_date[10];
unsigned char code Response[] = "+CMTI";
// Moblie no.of Owner
unsigned char code Mob_no[]= "9603658488";
unsigned char MyBuff[20], k = 0;
unsigned char store[10];
bit NewMessage = 0;
bit No_correct=0;
void init()
{
P1=0xFF;
p2=0x00;
abc=0;
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}
void main ()
{
clear();
delay(10);
init();
IT0 = 1; // Configure interrupt 0 for falling edge on /INT0 (P3.2)
SMSString("AT\r"); // AT commands to initialize gsm modem
delay(50);

SMSString( "ATe0\r"); // turn off echo
delay(50);

SMSString( "AT&W\r"); // save settings
delay(50);

SMSString( "AT+CMGF=1\r"); // select text mode for sms
delay(50);

SMSString( "AT+CNMI=2,1,0,0,0\r"); // notification of new sms
while(1)
{

IE=0x91;
k=0;
delay(10);
while(!NewMessage);
{
IE=0X00;
delay(10);
NewMessage = 0;
read_notification();
}
//New msg indication.
// +CMTI: "SM",3
}
}
void SMSString(unsigned char* text) //function to send SMS using GSM modem
{
while (*text)
{
tx0(*text++);
}
}
void tx0(unsigned char x) //send data to serial port 0
{
EA=0;
SBUF=x;
while(TI==0);
TI=0;
EA=1;
}
void read_notification()
{
clear();
SMSString("AT+CMGR=");
tx0(MyBuff[12]);
SMSString("\r");
IE=0x90;
delay(200);
read_no(msg1);
IE=0x00;
if(No_correct==1)
{

store_msg(msg1);
control_signal_to_mc();
}
clear();

if(MyBuff[12]>='9') //After 9 msgs recieved it clears the Msg index from SIM
{
delete_msg();
}

clear_mybuff();

return;
}
void serial () interrupt 4 {
unsigned char ch;
if(RI){
ch = SBUF; //character is read
RI = 0; //clear flag
if((ch == Response[k]) || (k > 4))
{
if(ch != 0x0D) // 0x0D means data received..
{
MyBuff[k++] = ch;
}
else
{
MyBuff[k] = '\0';
NewMessage = 1;
k = 0;
}
}
else
{
msg1[abc]=ch;//received msg is stored.
abc++;
k = 0;
}
}
}
void read_no(unsigned char msg_array[]) // reads the no from received msg and compares it with stored msg, If found wrong then send a msg to owner.
{
unsigned char i;
if(Mob_no[0]==msg_array[23]&&
Mob_no[1]== msg_array[24]&&
Mob_no[2]==msg_array[25]&&
Mob_no[3]== msg_array[26]&&
Mob_no[4]==msg_array[27]&&
Mob_no[5]== msg_array[28]&&
Mob_no[6]==msg_array[29]&&
Mob_no[7]== msg_array[30]&&
Mob_no[8]==msg_array[31]&&
Mob_no[9]== msg_array[32])
{
No_correct=1;
}
else // Sends a msg to stored no if some unauthoriesd tries to send a msg.
{
No_correct=0;
SMSString("AT+CMGS=\"");
SMSString(Mob_no);
SMSString("\"\r");
delay(1);
SMSString("Msg from Unknown number:( ");
for(i=23;i<=32;i++)
{
tx0(msg_array[i]);
delay(0);
}
SMSString(") is received.");
tx0(0x1A); // Ctrl+z
delay(100);
}
return;
}
void control_signal_to_mc()
{
if(strcmp(store,"ON A")==0){
unsigned char i = P1^0;
while(i){
ldr = 1;
}
}
if(strcmp(store,"ON B")==0){
um = 1;
}
if(strcmp(store,"ON C")==0){
tm = 1;
}
return;
}
void clear(void)
{
unsigned int a;
for(a=0;a<buffer;a++)
msg1[a]=0x00;
abc=0;
}
void clear_mybuff()
{
unsigned int a;
for(a=0;a<25;a++)
MyBuff[a]=0x00;
}
void delete_msg(void)
{
SMSString("AT+CMGD=9\r");
delay(1);
SMSString("AT+CMGD=8\r");
delay(1);
SMSString("AT+CMGD=7\r");
delay(1);
SMSString("AT+CMGD=6\r");
delay(1);
SMSString("AT+CMGD=5\r");
delay(1);
SMSString("AT+CMGD=4\r");
delay(1);
SMSString("AT+CMGD=3\r");
delay(1);
SMSString("AT+CMGD=2\r");
delay(1);
SMSString("AT+CMGD=1\r");
delay(1);
return;
}
void delay(unsigned int i)
{
unsigned int k, l;
for(k=0;k<i;k++)
for(l=0;l<1000;l++);
}
Thu Apr 16 2015, 01:09 am
#10


The problem is that when i insert the mc in to pcb, the power is not coming to the circuit but is there when there is no mc.

satish@iiit


There are three likely causes.
1 the power supply or its connections are faulty. Check all joints.
2 The chip is faulty
3 The chip is upside down.. and probably faulty.
Post your circuit diagram.

The AT89S52 can be programmed while still on the
board, this way is much easier to develop a program.
Can you get a serial programmer ?

Where is this latest code from , has it been used before ?
Has your hardware ever worked ?
Do you have simple test code to just flash a LED ?

In future just zip your program and post as an attachment to
save space.

I get errors compiling your code.

Where is the body of call "void store_msg(unsigned char msg_array[])"




[ Edited Thu Apr 16 2015, 01:31 am ]

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

TimmyJup
Wed May 08 2024, 12:22 am
Shawnarows
Tue May 07 2024, 10:16 pm
GlennVet
Tue May 07 2024, 04:46 pm
RonaldJoump
Tue May 07 2024, 10:15 am
Jasonkam
Mon May 06 2024, 10:00 pm
JamesroW
Mon May 06 2024, 09:37 am
Chrispes
Mon May 06 2024, 07:34 am
ArktiTic
Sun May 05 2024, 07:06 pm