recieving messages from gsm

i am working on a embedded project using GSM SIM300.i am using Atmel at89s52, GSM module ,temperature sensor LM35.

Working of project- Temp sensor will sense the temperature and sent a message to my cell phone only if the temperature is above 30^c,,and if i sent a SMS to the cell phone(for eg-OK),the relay connected to the microcontroller should turn ON.My code is as follows-

#include<reg51.h>
#include<stdio.h>
#define MYDATA P1;
sbit ALE=P2^4;
sbit OE=P2^5;
sbit SC=P2^6;
sbit EOC=P2^7;
sbit ADDR_A=P2^0;
sbit ADDR_B=P2^1;



sbit MOTOR=P2^2;
unsigned char rx[20],msg[6];
unsigned char sensor1,adc1,r;

void clear(void)
{
unsigned char r;
for(r=0;r<100;r++)
rx[r]=0x00;
}


void clear1(void)   //user msg
{
unsigned char n;
for(n=0;n<=5;n++)
msg[n]=0x00;
}

void serialcomm()
{

 TMOD=0x20;        // Mode=2
 TH1=0xfd;         // 9600 baud
 SCON=0x50;        // Serial mode=1 ,8-Bit data,1 Stop bit ,1 Start bit  , Receiving on
 TR1=1;           // Start timer

}
void delay(unsigned int value)
   {
     unsigned int i,j;
     for(i=0;i<value;i++)
     for(j=0;j<1275;j++);
   }

void serial(unsigned char x)
{
        SBUF=x;
        while(TI==0);
        TI=0;
}

void Send2Gsm(char *aaa)
{
     while(*aaa!='\0')
                 {
                         serial(*aaa);
                         aaa++;
     }
}



unsigned char convert(void)
{       unsigned char value;
        delay(1);
        ALE=1;
        delay(1);
        SC=1;
        delay(1);
        ALE=0;
        SC=0;
        while(EOC==1);
        while(EOC==0);
        OE=1;
        delay(1);
        value=MYDATA;
        OE=0;
        return(value);
}


void receive_data()  interrupt 4     // Function to recieve data serialy from RS232 into uc
 {
 unsigned char r;
  rx[r]=SBUF;                       // Read sbuf
  RI=0;

 }
void main()
{
        char string1[10};
        int temp;
  serialcomm();
  Send2Gsm("AT\r\n");
  delay(2);
  Send2Gsm("AT+CMGF=1\r\n");
  delay(2);
  Send2Gsm("AT+CMGS= +918762357075\r\n");
  delay(2);
  Send2Gsm("hello");
  serial(0x1a);

  while(1)
  {
  ADDR_B=0;
        ADDR_A=0;
        sensor1=convert();

        if(sensor1>60 )
        {
                temp=(adc1*0.489);
                sprintf(string1,"%d",temp);

                delay(500);
                Send2Gsm("Temperature=");
                Send2Gsm(string1);
                Send2Gsm("*c");
                delay(500);

                serial(0x1A);
                delay(500);
                IE=0x90;
                while(rx[r]=='?')
   {
     IE=0x00;   //disable interrupt


     if(rx[0]=='*' && rx[1]=='O' && rx[2]=='N' )
    {

     MOTOR=1;
     clear();
     clear1();

    }

   else if(rx[0]=='*' && rx[1]=='O' && rx[2]=='F')
    {

       MOTOR=0;
       clear();
       clear1();

    }
    }


}
}
}

i am not getting output with this code.i am having doubt with the recieving sms to the gsm modem.can anyone point out the mistakes in my code ??

Parents
  • Too many big issues.

    1) You just blind-send data to the GSM - you are expected to care about the responses too.

    2) You start with "preparing" the GSM for sending an SMS. Then you start to run your loop looking at temperatures. Wait until you see a too high temperature and _then_ call some function SendSMS(<number>,<msg>);

    3) Don't use "clever" variable names like "aaa" - Care to tell the meaning of that variable name? Anonymous Adaptive Array???

    4) A function clear() and a function clear1() - did it hurt to give them names that indicated _what_ they cleared?

    5) Shouldn't your receive interrupt care about if there is room in the input buffer?

    6) Shouldn't your receive interrupt care about indicating that a character has been received?

    7) delay(500) - 500 what? 500 hours? 500 random anything? Do you even yourself know how long time delay(500) is? Why not write a delay that actually delays for a meaningful time interval?

    8) What make you think that this is meaningful "if(rx[0]=='*' && rx[1]=='O' && rx[2]=='N' )" - have you seen any indication that there is likely to be any data there to check? How have you even decided if your code is interested in sending an SMS or receiving an SMS?

    9) You think anyone should care to look more at your code when you haven't spent your own time debugging it?

    You really have to spend way more own energy if you want to get this assignment accepted by your teacher!

Reply Children
More questions in this forum