GSM sms parsing problems

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi everyone,

im trying to receive a sms on a gsm modem and then forward the text to another number.

when i check the working between comp and microcontroller it seems to work. but when i connect the modem it sends out the sms via (sendsms()) but i cannot find the new messages reaching the modem (CheckSms() routine).

any ideas.

rodney almeida

int main (void)
{
DDRD |= ((1 << 4) | (1 << 5));
UART_init(); 
sei();
SetupSMS();
SendSMS();
DeleteAllSms();
while(1)
	{
	CheckForSms();
	}
}








void SendSMS()
{
	UART_RBUF_CLEAR();
	{
		UART_puts_P(PSTR("AT+CMGS=\"+919850009469"));
		UART_puts_P(PSTR("\"\r\n"));
		_delay_ms(1000);
		UART_puts_P(PSTR("bootup"));
		UART_putc(0X1A);
		_delay_ms(2000);
	}
}


void SetupSMS()
{
	unsigned char s[80];
    int n;
//	char ser[]="OK"; AT+CGSMS=1

	UART_puts_P(PSTR("AT\r\n"));
	_delay_ms(500);
	UART_puts_P(PSTR("AT\r\n"));
	_delay_ms(500);
	UART_RBUF_CLEAR();
	UART_puts_P(PSTR("ATE0\r\n"));
	UART_puts_P(PSTR("AT&V\r\n"));
	_delay_ms(1000);
	UART_RBUF_CLEAR();
	UART_puts_P(PSTR("AT+CMGF=1\r\n"));
	UART_puts_P(PSTR("AT+CGSMS=1\r\n"));
	_delay_ms(500);
}

void DeleteAllSms()
	{
	int a;
	char b;
	char c[3];
	UART_puts_P(PSTR("AT\r\n"));
	for (a=1;a<=5;a++)
		{
		UART_puts_P(PSTR("AT+CMGD="));
		itoa(a,c,10);
		//b= (0x30+a);
		UART_puts(c);
		UART_puts_P(PSTR("\r\n"));
		_delay_ms(500);
		}
	}
	

void CheckForSms()
{
	
	char ser[]="+CMTI";
	char s[64];
	int n;
	n = UART_gets(s, 6);
	if (n>0)
		{
		s[strlen(s)-1] = 0;
		if (strstr(ser, s))
			{
			//UART_puts_P(PSTR("\r\nFOUND at index "));
			PORTD |= (1<<5);        ///TURN LED1 ON TO INDICATE THAT NEW SMS RECEIVED
			n = UART_gets(s, 6);
			n = UART_gets(s, 5);
			UART_puts_P(PSTR("\r\n"));
			UART_puts_P(PSTR("\r\nAT+CMGR="));
			UART_puts(s);
			UART_puts_P(PSTR("\r\n"));
			n = UART_gets(s, 63);
			//n = UART_gets(s, 63);
			n = UART_gets(s, 20);
			if (n>0)
				{
				PORTD |= (1<<4);  //TURN LED2 TO INDICATE THAT PASSED IF ROUTINE
				s[strlen(s)-1] = 0;
				UART_puts_P(PSTR("AT+CMGS=\"+919850009469"));
				UART_puts_P(PSTR("\"\r\n"));
				_delay_ms(1000);
				UART_puts(s);
				UART_putc(0X1A);
				_delay_ms(2000);
				}
			}
		}
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

just thinking do i have to reset the receive buffers after initialisation?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the test case where the AVR is talking to the PC the PC is simulating the GSM modem if I understand it correctly?

It might be as simple as you needing to swap the TxD and RxD lines at one end.

Eg if you, in the AVR-PC case, have a cable which has the lines crossed (because both the AVR and the PC are DTEs) then you need to un-cross them (because the GSM modem is a DTE).

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nice use of comments by the way ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Nice use of comments by the way

Why, Cliff.. If I didn't know better I'd say that you where ironic right there! :D

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Johan.. The modem is communicating with the avr. The avr can send sms messages via the modem, which i happen to receive on my mobile.

Whats with the comments guys.. the english may be a tad off.. thats what happens when i think and write at the same time. :-)

and idea if there is a way where i can sniff whats going on between the avr and modem? i have no LCD display or secondary UART.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
and idea if there is a way where i can sniff whats going on between the avr and modem?
You can connect Rx or Tx (one at a time) to a MAX232 chip and send to a PC terminal(like Bray's terminal or Hyper terminal).

I believe it was the lack of comments that was commented upon, so no need to worry about spelling...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Whats with the comments guys.. the english may be a tad off.. thats what happens when i think and write at the same time

To be serious, I was commenting on your C code - it's almost impossible to follow what's going on as there's no comments explaining the purpose of any part of it. The only "comments" I see are a few lines that have been disabled using "//" - that actually makes it even less readable.

You don't need a comment per line:

a = b + c;

does not benefit from a comment like:

// add b to c and put in a
a = b + c;

but it would benefit from:

// ambient temperature is the sum of the base temperature and the change just measured
a = b + c;

though this would be more readable as:

ambient = base + change;

(in which case it's almost self documenting and the comment is not required any more). Sensible variable names aid the readability of code.

BTW comments aren't just there for other readers of your code (especially the maintainer looking at in two years time when you've left the company) or even to help problem solvers on a board like Freaks. They are also there for YOUR benefit when you try to reuse this code in 9 months time and wonder "what on earth was I doing here?".

If as you're programming you think "next I'll need to add the change to the base to get the ambient" it doesn't hurt to actually type that into the source too!

Another way of working is to design the software first - maybe start with some comments:

// first we need to initialise the ADC
// and the IO ports
// then start the timer interrupts
// and loop waiting for the next reading to be displayed

then you use this "algorithm" as a guide to your actual implementation:

// first we need to initialise the ADC
ADC_init();
// and the IO ports
GPIO_init();
// then start the timer interrupts
Timer1_start();
// and loop waiting for the next reading to be displayed
while (1) {
  if (reading_available) {
    LCD_display(reading);
    reading_available = 0;
  }
}

Cliff

PS this is just a general moan - not totally directed at you but everyone who posts code that's tricky to follow here.

PPS but just as one example:

UART_putc(0X1A);

Now you may know what the significance of 0x1A is here but will you in 9 months? (I know it's ASCII EOF or Ctrl-Z but that still doesn't help this reader understand why you are sending it to a GSM module)

Last Edited: Thu. Aug 12, 2010 - 12:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, just to show that everyone has different opinions.

I think that Rodney has used concise and appropriate comments. He has used descriptive names for his functions. The albeit terse local variables are pretty obvious. It is neatly layed out and displayed with CODE tags.

Yes. The first job is connecting the modem cable round the right way.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i respect everyones comments. i was under the impression that my code was easily readable. no issues.

In a desperate way to see whats going on between the avr and the modem, i hooked the Tx line of the Avr simultaneously to another com port. i also added another line of code in the CheckForSms() routine to echo what the avr actually buffered after being received.

void CheckForSms()
{
	
	char ser[]="+CMTI";
	char s[64];
	int n;
	n = UART_gets(s, 6);
	if (n>0)
		{
		s[strlen(s)-1] = 0;
		UART_puts(s); //new line added here
		if (strstr(ser, s))
			{
			//UART_puts_P(PSTR("\r\nFOUND at index "));
			PORTD |= (1<<5);        ///TURN LED1 ON TO INDICATE THAT NEW SMS RECEIVED
			n = UART_gets(s, 6);
			n = UART_gets(s, 5);
			UART_puts_P(PSTR("\r\n"));
			UART_puts_P(PSTR("\r\nAT+CMGR="));
			UART_puts(s);
			UART_puts_P(PSTR("\r\n"));
			n = UART_gets(s, 63);
			//n = UART_gets(s, 63);
			n = UART_gets(s, 20);
			if (n>0)
				{
				PORTD |= (1<<4);  //TURN LED2 TO INDICATE THAT PASSED IF ROUTINE
				s[strlen(s)-1] = 0;
				UART_puts_P(PSTR("AT+CMGS=\"+919850009469"));
				UART_puts_P(PSTR("\"\r\n"));
				_delay_ms(1000);
				UART_puts(s);
				UART_putc(0X1A);
				_delay_ms(2000);
				}
			}
		}
}

I was shocked to notice it was echoing but with some characters missing. Therby the comparison wound never be sucessfuly made.

i think i have to check my UART routines.