5
\$\begingroup\$

Hi all I am trying to interface GSM SIM900 with atmega32. I have a written a code in which if a user press a push button, a call will be made to a pre defined number. If we want to change the pre defined number, we can send message like CHANGE1234567890 where 1234567890 is the new number so that if anyone presses the push button, call will be made to this new number.

In my code I have made serial_Tx function which send string serially. serial_Rx which receive data and uart_put which transmit single character.

CODE:

 //serial initialize
    void serial_Init()
   {
    UCSRB = (1<<TXEN) | (1<<RXEN);
    UCSRC = (1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
    UBRRL = 51;

  }
//serial transmission function
void serial_Tx(char *str)
{
    for (unsigned int i=0;str[i]!=0;i++)
    {
        UDR=str[i];
        while(!(UCSRA&(1<<UDRE))); 

    }

}
//serial transmission single char
void uart_put(char data)
 {
   while(!(UCSRA & (1<<UDRE)))
   {

   }
   UDR=data;
 }
//serial receive function
char serial_Rx()
 {                              

    while(!(UCSRA & (1<<RXC)))
    {

    }
return UDR;
 }

char newnum[10];

 char smscontent[200];
 int len=0; ;
 unsigned char slot;
 char smscmd[16];
 char delcmd[16];
 char cfmcall[16];
 char call[20];
 char smsindi[14];
  char data[15];
 char calldata[10];
 char confirmdata[10];
 char deldata[10];

int main(void)
{
    DDRA = 0x00;
    DDRC = 0xFF;
    PORTA = 0xFF;
    PORTC = 0x00;
    serial_Init();
   PORTA=0xFF;
  while(1)
    {
      //********FUNCTION TO GET SLOT NUMBER of received msg***********//
        if (serial_Rx() != NULL) 
        {
            //serial_Tx("done\n");

            PORTC = 0x01;
            for (int i=0;i<=14;i++)
            {
                data[i] = serial_Rx();
                if(isdigit(data[i]))
                {
                    //serial_Tx("enter\n");
                    PORTC = 0x02;
                    slot = data[i]; 
                }

            }



        }
        else
        {
            serial_Tx("nothing to read \n");
        }
        //**************LOOP END***********************//

     //************FUNCTION TO SEND SMS COMMAND**********************//
        uart_put(slot); //printing the SMS slot number
        sprintf(smscmd,"AT+CMGR=%c\r\n",slot);

        serial_Tx(smscmd);
        for(int k =0; k<=200;k++)
        {
            smscontent[k] = serial_Rx();
            uart_put(smscontent[k]);
        }
        //*****************LOOP END*******************************//

      //***************FUNCTION TO EXTRACT NEW NUMBER*************//
        for(int k =0;smscontent[k]!=NULL;k++) //smscontent[k]!=NULL
        {
            if (smscontent[k] == 'C')
            {
                if (smscontent[k+1] == 'H')
                {
                    if (smscontent[k+2] == 'A')
                    {
                        if (smscontent[k+3] == 'N')
                        {
                            if (smscontent[k+4] == 'G')
                            {
                                if (smscontent[k+5] == 'E')
                                {

                                    if(isdigit(smscontent[k+6]))
                                    {
                                        k=k+6;
                                        serial_Tx("MATCH\n");
                                        for (int i=0;i<=9;i++,k++)
                                        {

                                            newnum[i] = smscontent[k];

                                        }


                                        serial_Tx(newnum); // new number is stored in newnum


                                    }




                                }
                            }

                        }
                    }
                }


            }
        }


        //*****************LOOP END******************************//

      //*******************CALL function **********************//
        if(PINA==0xFE) // if switch pressed then:
        {


            sprintf(call,"ATD%s;\r\n",newnum);
            serial_Tx(call);

            for(int j=0;j<=9;j++)
            {
                calldata[j] = serial_Rx();

            }

            if(strstr(calldata,"OK") != NULL)
            {

                for (int i = 0;i<=20;i++)
                {
                    PORTC = 0x01; // led blink
                    _delay_ms(150);
                    PORTC = 0x00;
                    _delay_ms(150);
                }

            }
            else
            {
                PORTC = 0x02; // led 2 on

            }

        }

        //*************************LOOP END***********************************//
 }
}

I am able to get the slot number of received message and I have also extracted the number from my msg. The problem is that now I have to replace the old number with new number in call function but its not working properly and even when I press the push button it doesnt function. Please help.

\$\endgroup\$
6
  • \$\begingroup\$ Maybe start with this page and see if you are issuing the same commands in the same time frames: tronixstuff.com/2014/01/08/… \$\endgroup\$
    – Secto Kia
    Aug 1, 2015 at 4:54
  • \$\begingroup\$ Thanks Secto for sharing the link. But that link is for arduino. In arduino there are many libraries pre build so that you can directly use them. But I'll definetly read it. \$\endgroup\$ Aug 1, 2015 at 5:00
  • \$\begingroup\$ Does your call function work for a predefined number? Also, I'm not sure PINA==0xFE is the proper way to check a button, you should check that as atleast something should've happened? \$\endgroup\$
    – Paul
    Aug 17, 2015 at 7:46
  • 1
    \$\begingroup\$ The point isn't to use their code. My point is that their code clearly contains the details the information exchanged and the timing involved. You should compare its logical communication algorithm to yours. \$\endgroup\$
    – Secto Kia
    Aug 25, 2015 at 11:15
  • \$\begingroup\$ Is the button a momentary switch? Configure the button press input to be an interrupt. The way the loop is currently written doesn't ensure that the press will even be seen unless it's held down. \$\endgroup\$ May 19, 2016 at 18:40

1 Answer 1

0
\$\begingroup\$

If you have another UART to USB dongle available you can connect its RX pin to your TX pin on your atmega32 to see what is actually being sent over in the code you have (also need to connect the GND on your dongle to your system GND). I have seen that when using a SIM800 module if I try to transmit while the system is waiting to complete another command it will just ignore that command or generate an error. Also I have seen problems if you send too many commands too quickly.

So basically watching what it does in a controlled manner via another UART will really help a ton to figure out what is going on. Just make sure your UART dongle is expecting 3.3V or something similar for its logic levels to connect up to the UART logic level for the atmega32 and SIM900 module.

You might consider making a strcmp function or using one from a library someplace rather than doing what you are doing with single character comparisons. The strcmp function is very simple function to make or get from someplace. I haven't checked all of your array logic, but with so many different arrays, its easy to make a mistake in your logic. Another thing to mention is that you have arrays that are like length of 10, but then your for loop uses <=9 as your comparison. That is a bad practice since the two numbers you need to keep track of separately. Better to #define the length and use

\$\endgroup\$
1
  • \$\begingroup\$ Did you accidentally submit your answer too early? You seem to have lost at least part of the last sentence, if not more content. \$\endgroup\$ Jul 22, 2016 at 21:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.