This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • 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!

Children
  • You receive interrupt doesn't do anything predictable, loads current character into a random location, doesn't advance any pointer.

    You send one text message, may be, you ignore anything from the modem, and expect the characters that do come back to be nicely arranged as what every ON/OFF signal you might send. There are AT commands to check and read SMS messages, you should perhaps use them.

    In your main loop you don't initiate a new SMS, but if you did you'd likely send yourself thousands of SMS messages as it barrels around the loop.

    Tips: Read the manual for your modem, learn how to parse responses for it.

  • if we give the AT command ATE0 at first,there will be no responses from the gsm right ? should we really care about gsm responses in this project ? i don't have much knowledge in gsm programming.please excuse me if i am wrong.

  • 2)

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

    is this what you meant ?

  • No ATE0 turns off the echo from the modem, where you see the commands you sent to it. The modem will STILL respond to the commands sent to it, in the manner describe in it's AT command manual. The response will also likely return OK, ERROR or NO CARRIER depending on it's validity, and the modem state.

  • Modem communication is a dialogue. So yes, you should care about the responses even if this is just a school project. Caring about responses and return codes from function calls is the way you catch problems and can fix them, instead of wasting hours or days or weeks just wondering why some things doesn't work at all, or only works sometimes.

    It's the responses from the modem that is the indication that you should go ahead with more communication with the modem. Or do you put plugs in your ears and just talk out in the air at home, and expect someone to listen and care what you are saying? If you do - does it really work?