Keil Logo

Problem with GSM switch and 89c51

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
Nick D
Posted
12-Apr-2011 01:25 GMT
Toolset
C51
New! Problem with GSM switch and 89c51

I have to make this project for my school but i can't make it run. I've problems with message extraction and with the interrupt. Please help.
This is my code

#include <reg51.h>
#include <string.h>

sbit Water=P2^0;
sbit Window=P2^1;

//declarations

void clear(void);
void tx0(unsigned char);
void delay_sms (unsigned int);
void SMSString(char*text) ;
void init();
void read_text(unsigned char * , unsigned char * , unsigned char *);

unsigned char j,abc;
unsigned char idata msg1[150];
unsigned char rec_no[20];
unsigned char time_date[20];
unsigned char choice=0;
unsigned char idata tmp10[16];

void serial () interrupt 4
{
msg1[abc]=SBUF;
abc++;
RI=0;
}

void main (void)
{
clear();
init();

Window=0;
Water=0;


SMSString("AT\r"); // AT commands to initialize gsm modem
delay_sms(1000);

SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);

SMSString( "AT&W\r"); // save settings
delay_sms(1000);

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

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

SMSString( "AT+CMGR=1\r"); // AT command to read sms
delay_sms(1000);

do
{
IE=0X90;   // Enable serial interrupt
delay_sms(1000);

// i get a response like this

// +CMGR: "REC UNREAD","+3069********",,"11/04/09,11:26:48+12"
// Window on
// OK
// I think that's the format the 8051 get +CMTI: "SM",1\r+CMGR: "REC UNREAD","+306982037789",,"11/04/09,11:26:48+12"\rStatus\n\rOK\r
// read sms and store in buffer msg1

read_text(msg1,rec_no,time_date);

if(strncmp("Water on",msg1,8)==0)
{
Water=1;
strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");
delay_sms(1000);
SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}
else  if(strncmp("Water off",msg1,9)==0)
{
Water=0;
strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");
delay_sms(1000);
SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}
else  if(strncmp("Window on",msg1,9)==0)
{
Window=1;
strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");
delay_sms(1000);
SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}
else  if(strncmp("Window off",msg1,10)==0)
{
Window=0;
strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");
delay_sms(1000);
SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}
else  if(strncmp("Status",msg1,6)==0)
{
strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");
delay_sms(1000);
SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}
else
{
SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>\r");
delay_sms(1000);
SMSString("Wrong Command");
SMSString("\n\r");
delay_sms(1000);

SMSString(rec_no);
delay_sms(1000);

SMSString(msg1);
delay_sms(1000);
}
SMSString("AT+CMGD=1\r");
delay_sms(1000);

IE=0X00;   // Disable serial interrupt
delay_sms(1000);

}
while(1);
}

void init(void)
{
j=0;
abc=0;
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}

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 delay_sms (unsigned int count)
{
unsigned int i;
    while(count) {
        i = 115;
                while(i>0) i--;
        count--;
}
}

void read_text( unsigned char *msg,unsigned char *no ,unsigned char *time)
{
unsigned char *temp;
temp=msg;

do
msg++;
while(*msg!='+');

do
msg++;
while(*msg!='+');

do
msg++;
while(*msg!='+');         //  reaching number

do
*no++=*msg++;
while(*msg!='"');         // reaching time

*no++='\0';
msg++;
msg++;
msg++;
do
*time++=*msg++;
while(*msg!='"');         // reaching message
*time='\0';
do
msg++;
while(*msg!='\r');
msg++;
do
*temp++=*msg++;
while(*msg!='\n');       // reaching the end of message
*temp='\0';
}

void clear(void)
{
unsigned char a;
for(a=0;a<100;a++)
msg1[a]=0x00;
}
Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 08:01 GMT
Toolset
C51
New! RE: I have to make this project for my school

Note that debugging is an inherent part of the development process.

It is time for you to start debugging what you've written.

Here are some debugging tips & techniques:

http://www.8052.com/faqs/120313

http://www.techonlineindia.com/article/09-09-23/Developing_a_good_bedside_manner.aspx

You haven't mentioned what specific processor you are using.

If your processor doesn't have on-chip debug, then the easiest way to debug it is to run it in the Simulator, redirect the UART IO to the COM port, and connect your GSM modem to the COM port.

http://www.keil.com/support/man/docs/uv4/uv4_db_dbg_serialwin.htm

See comments here about arbitrary delays: http://www.keil.com/forum/18684/
See also: http://www.keil.com/forum/18696/

See comments here about delay loops in 'C': http://www.8052.com/forum/read/181806
http://www.8052.com/forum/read/181789

Your code would be easier to read, maintain, and debug with proper indentation, meaningful names (not "abc" and magic numbers), and a good deal more descriptive comments.

Read-Only
Author
Per Westermark
Posted
12-Apr-2011 08:30 GMT
Toolset
C51
New! RE: Problem with GSM switch and 89c51
void serial () interrupt 4 {
    msg1[abc]=SBUF;
    abc++;
    RI=0;
}


abc being a meaningful name "advanced buffer counter" or what?

It most definitely seems to be the insert position in your receive buffer for serial data. So why name it something like that?

And msg1 seems to be your receive buffer for serial data, so why not name it something like that?

You have msg1 defined as 150 characters large - your interrupt handler doesn't check where you are. What happens if you receive 151 characters? Would that be good?

You initialize abc to zero. But how do you get your receive interrupt to reuse msg1 for receiving data after you have processed some received data?

strncpy(tmp10,"Water   Window  ",16);
tmp10[6]=Water;
tmp10[15]=Window;


You are mixing ASCII and binary data in the same string. Do you think that is good? What about instead doing sprintf() to create a fully ASCII string with the values of "Water" and "Window" inserted?

Your read_text() function is more or less a random noise.

Don't you think you should process your answers line-by-line, and then look at the start of each received line to figure out what command you are decoding? Then it would be much better to split the parameters on the commas (while remembering that a comman can exist within a "" string too) when trying to locate the time and message.

SMSString("AT+CMGS=\"");
SMSString(rec_no);
SMSString("\"+<CR>");


On one hand - don't you think some things deserves to be a specific function, that does know to send a command and also does know to check for expected - or unexpected - answers? And what is "<CR>"?

What is delay_sms(1000)? Wouldn't it be better to implement a delay function that has a known property like creating delays in ms resolution, and then do delay_ms(POWER_ON_SETTLE_TIME) or similar? In your case, you have zero documentation what delay_sms() does that makes it meaningful to be called or to have "sms" as part of the name.

And why do you have so many hard-coded delays? Don't you think you should be more dynamic - send an SMS command and then listen for the characters being received from the modem on the fly? All you need is a timeout value when to give up. But if you get the answer much faster, then you don't have any reason to continue waiting. After all - how do you know what is a reasonable time to wait? Will that wait time always be true? Even with another operator or with overloaded network or with bad signal strength?

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 08:57 GMT
Toolset
None
New! RE: What happens if you receive 151 characters?

Especially considering that the maximum length of an SMS is - what?

Read-Only
Author
PerT Westermark
Posted
12-Apr-2011 09:17 GMT
Toolset
None
New! RE: What happens if you receive 151 characters?

In processors with limited memory, you normally don't have large buffers for storing worst-case multiline data. The normal way to handle things is to have a state machine that processers received characters on-the-fly (either directly from the serial port but often using a smaller ring-buffer to reduce the tight time coupling between serial port and processing).

The state machine can then figure out what is the start of a new text line, what is echo, what is answer, etc. And when processing answers, it can on-the-fly decode the individual fields in the answer to extract time, message etc. And such state machines can also take care of any unexpected unsolicited messages you may receive from a modem, such as a ring message if someone happens to ring the subscriber number while you think you are about to check for reception of an SMS - the modem promises that any unsolicited messages will not be inlined inside other text l ines so it's trivial for a state machine to handle them while handling all other answers.

So you can constantly check if your state machine have found something interesting you want to handle - like an unsolicited message that the modem has an overtemperature or other interesting messages it may be capable to report. And you can send commands to the modem and within a timeout interval check that you get expected responses - any enabled echo strings, ok/error strings, any specific answer string. When you have received what you should recieve or an error message, you can then figure out what to do next, i.e. if you have more strings to send to the modem.

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 10:16 GMT
Toolset
None
New! RE: State Machines

Yes, State Machines are ideal for this kind of problem!

The use of State Machines is mentioned in one of the threads previously linked: http://www.keil.com/forum/18696/

Read-Only
Author
Nick D
Posted
12-Apr-2011 10:52 GMT
Toolset
C51
New! RE: State Machines

First of all i want to thank you for you time. I have an Philips 89C51RD+.
The delay_sms() creates a delay in ms, e.x. delay_sms(1000) creates 1sec delay.
I'll try to fix the things you recommended but i really don't understand state machines.
I'll sent you the new code when i done it.

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 12:03 GMT
Toolset
None
New! RE: i really don't understand state machines

As the linked article says, you really need to learn about State Machines!

http://en.wikibooks.org/wiki/Serial_Programming/Modems_and_AT_Commands#Welcome_to_the_World_of_State-Machines

They are an (almost) essential tool in this type of problem, and an invaluable tool in many other types of problem.

Time invested now in learning about State Machines will reap bountiful rewards in the future...

Here's a simple tutorial: http://www.8052.com/forum/read/47505

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 12:07 GMT
Toolset
None
New! RE: delay_sms(1000) creates 1sec delay

Only under a very limited set of circumstances - some of which you haven't specified, and some of which are not within your control as a 'C' programmer!!

See: http://www.8052.com/forum/read/162556

Read-Only
Author
erik malund
Posted
12-Apr-2011 14:40 GMT
Toolset
C51
New! RE: State Machines

The delay_sms() creates a delay in ms, e.x. delay_sms(1000) creates 1sec delay.
maybe today, maybe tomorrow, maybe not next week

NOBODY knows what a compiler will do to a delay routine, delays MUST be done in assemble or with timers

Also, why is there no indentions in your code, it is very tough to read

Erik

Read-Only
Author
Nick D
Posted
12-Apr-2011 15:10 GMT
Toolset
C51
New! Code with some adjustments

I fix some of the errors you indicated me but still the code is not running properly.
I can receive whatever i sent with the SMSString() but i can't receive the sms to the microcontroller and of course i can't get the phone number and the message from it.
This is my fixed code

#include <reg51.h>
#include <string.h>

sbit Water=P2^0;
sbit Window=P2^1;

//declarations

void clear(void);
void tx0(unsigned char);
void delay_sms (unsigned int);
void SMSString(char*text) ;
void init();
void Status();
void init_modem();
void process_modem_character(unsigned char *);

unsigned char idata phone[15];               // Store received phone number
unsigned char idata msg1[100];               // Store received message
unsigned char j,abc;
//unsigned char idata tmp10[27];

void serial () interrupt 4 using 1
{
if(RI)            //receive mode
{
RI=0;
abc=SBUF;
if(abc<100)
{
msg1[abc++]=SBUF;
}
else
{
RI=1;
}
}
}

void main (void)
{
clear();
init();
init_modem();

do
{
IE=0X90;   // Enable serial interrupt
delay_sms(1000);

// i get a response like this
// +CMGR: "REC UNREAD","+3069********",,"11/04/09,11:26:48+12"
// Window on
// OK
// I think that's the format the 8051 get
// \+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\"+306982037789\"\,\,\"11\/04\/09\,11\:26\:48\+12\"\rStatus\n\rOK\r

process_modem_character(msg1);    // read sms and store it

if(strncmp("Water on",msg1,8)==0)
{
Water=1;
Status();
}
else  if(strncmp("Water off",msg1,9)==0)
{
Water=0;
Status();
}
else  if(strncmp("Window on",msg1,9)==0)
{
Window=1;
Status();
}
else  if(strncmp("Window off",msg1,10)==0)
{
Window=0;
Status();
}
else  if(strncmp("Status",msg1,6)==0)
{
Status();
}
else
{
SMSString("AT+CMGS=\"");
SMSString(phone);
SMSString("\"+<CR>\r");
delay_sms(1000);
SMSString("Wrong Command");
SMSString("\n\r");
delay_sms(1000);

SMSString(phone);
delay_sms(1000);

SMSString(msg1);
delay_sms(1000);
}
SMSString("AT+CMGD=1\r");
delay_sms(1000);

IE=0X00;   // Disable serial interrupt
delay_sms(1000);

}
while(1);
}

void init(void)
{
j=0;
abc=0;
TL1=0XFD; //9600 @ 11.0592
TH1=0xFD;
TMOD=0x20;
SCON=0x50;
TR1=1;
}

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 delay_sms (unsigned int count)
{
unsigned int i;
    while(count) {
        i = 115;
                while(i>0) i--;
        count--;
}
}

void init_modem(void)
{
Window=0;
Water=0;


SMSString("AT\r"); // AT commands to initialize gsm modem
delay_sms(1000);

SMSString( "ATe0\r"); // turn off echo
delay_sms(1000);

SMSString( "AT&W\r"); // save settings
delay_sms(1000);

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

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

SMSString( "AT+CMGR=1\r"); // AT command to read sms
delay_sms(1000);
}

void Status(void)
{
//tmp10 = sprintf( SBUF, "Water is %s and Window is %s\n" , Water, Window);
SMSString("AT+CMGS=\"");
SMSString(phone);
SMSString("\"+<CR>");
delay_sms(1000);
//SMSString(tmp10);
SMSString("\n\r");
delay_sms(1000);
}

enum States {
    STATE_IDLE,
    STATE_FIND_CMT,
    STATE_CMT_GET_NUMBER,
    STATE_CMT_GET_COMMA,
    STATE_CMT_GET_MSG_START,
    STATE_CMT_GET_MSG,
} state = STATE_IDLE;
const char* pattern;
int ch;

void process_modem_character(unsigned char*msg) {
    switch (state) {
        case STATE_IDLE:
            if (msg == '+') {
                state = STATE_FIND_CMT;
                pattern = "\+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\"";
            }
            break;
        case STATE_FIND_CMT:
            if (*pattern != msg) {
                state = STATE_IDLE;
            } else {
                pattern++;
                if (*pattern == '\0') {
                    state = STATE_CMT_GET_NUMBER;
                    ch = 0;
                }
            }
            break;
        case STATE_CMT_GET_NUMBER:
            if (msg == '\"') {
                state = STATE_CMT_GET_COMMA;
            } else if (ch >= sizeof(phone)) {
                // Too long phone number :(
                state = STATE_IDLE;
            } else {
                                ch++;
                phone[ch] = *msg;
                phone[ch] = '\0';
            }
            break;
        case STATE_CMT_GET_COMMA:
            if (msg == ' ') {
                ;    // Ignore white space
            } else if (msg == ',') {
                state = STATE_CMT_GET_MSG_START;
            } else {
                // Invalid character received
                state = STATE_IDLE;
            }
            break;

        default:
            // Invalid state.
            state = STATE_IDLE;
    } // switch
} // process_modem_character
void clear(void)
{
unsigned char a;
for(a=0;a<100;a++)
msg1[a]=0x00;
}

Please help

Read-Only
Author
PerT Westermark
Posted
12-Apr-2011 15:42 GMT
Toolset
C51
New! RE: Code with some adjustments

This is not how to receive data:

void serial () interrupt 4 using 1 {
    if (RI) {            //receive mode
        RI=0;
        abc=SBUF;
        if(abc<100) {
            msg1[abc++]=SBUF;
        } else {
            RI=1;
        }
    }
}


Seems more like random changes.

Look at standard example code for receiving serial data in interrupt handler.
But abc (which you haven't still renamed to something meaningful) should not be assigned from SBUF - for every character received on the UART you may only read one (1) time from SBUF.

And you call process_modem_character(msg1) without caring if msg1 contains any characters.
And your state machine doesn't consume any characters.
And you have code like:

if (*pattern != msg) ...


but pattern is a pointer to char and msg is a pointer to unsigned char. Besides mixing char and unsigned char - what does the star do? *pattern would be the character pointed to by pattern. But msg is not a character but a pointer - comparing a character and a pointer isn't meaningful.

Try to implement a ring buffer or incomming data, and have your processing check if the ring buffer have data available. Then eat data and switch states depending on what you find. And keep track of pauses where you have no data or line endings to synchronize your state machine.

You don't get anything to work for the simple reason that you have neither tried to debug this code by running it and comparing what happens with what you expected, nor have you tried to just in your head evaluate these lines and figure out what values the different variables might have and what would happen if you step the code in your head. The major part of writing code is actually stepping the code in the head - we can't run a debugger until we have code, but we must be able to read and understand without debugger to be able to write the code in the first place.

Read-Only
Author
erik malund
Posted
12-Apr-2011 16:03 GMT
Toolset
C51
New! RE: Code with some adjustments
void serial () interrupt 4 using 1
    {
    if (RI)
        {            //receive mode
        RI=0;
        abc=SBUF;
        if(abc<100)
        {
            msg1[abc++]=SBUF;
        }
        else
        {
            RI=1; /////////// this will get you right back into the ISR
        }
    }
}


I had to reformat your code to verufy this, what is wrong with lining the braces up???, look at the comment above.

Erik

Read-Only
Author
erik malund
Posted
12-Apr-2011 16:06 GMT
Toolset
C51
New! pray explain
        abc=SBUF;
....
            msg1[abc++]=SBUF;


storing in a buffer indexed by the hex value of the character received?????

It looks to me as if you do not believe in the adage "think before you do"

Erik

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 16:35 GMT
Toolset
None
New! RE: what is wrong with lining the braces up???,

Brace positioning is very much a matter of strongly-held personal preference.

Many people like their braces on the end of the "controlling" line:

   if( test ) {
      // stuff
   } else {
      // alternative stuff
   }

Some people (like me) like their braces alone on a line, aligned with the "controlling" line

   if( test )
   {
      // stuff
   }
   else
   {
      // alternative stuff
   }

Some people (quite inexplicably, IMO) like their braces alone on a line, aligned with the "controlled" line(s)

   if( test )
      {
      // stuff
      }
   else
      {
      // alternative stuff
      }

To name but three.

The important thing is, whatever you choose, to be consistent

Read-Only
Author
Andrew Neil
Posted
12-Apr-2011 16:46 GMT
Toolset
None
New! RE: To name but three

plenty more here: http://en.wikipedia.org/wiki/Indent_style#K.26R_style

Read-Only
Author
Nick D
Posted
12-Apr-2011 17:06 GMT
Toolset
None
New! RE: what is wrong with lining the braces up???,

I modified my braces and changed the serial interrupt but there is still a lot work to be done.

#include <reg51.h>
#include <string.h>
#include <stdio.h>

sbit Water=P2^0;
sbit Window=P2^1;

//declarations
void clear(void);
void tx0(unsigned char);
void delay_sms (unsigned int);
void SMSString(char*text) ;
void init();
void Status();
void init_modem();
void process_modem_character(unsigned char *);

unsigned char idata phone[15];               // Store received phone number
unsigned char idata msg1[100];               // Store received message
unsigned char j,bufcounter;

void serial () interrupt 4 using 1
{
    if (RI)
        {
        while(!RI)
                {
                RI=0;
                if(bufcounter<100)
                {
                msg1[bufcounter++]=SBUF;
                }
                }
    }
}
void main (void)
{
        clear();
        init();
        init_modem();
        do
                {
                        IE=0X90;   // Enable serial interrupt
                        delay_sms(1000);

                // i get a response like this
                // +CMGR: "REC UNREAD","+3069********",,"11/04/09,11:26:48+12"
                // Window on
                // OK
                // I think that's the format the 8051 get
                // \+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\"+3069********\"\,\,\"11\/04\/09\,11\:26\:48\+12\"\rStatus\n\rOK\r

                        process_modem_character(msg1);    // read sms and store it

                        if(strncmp("Water on",msg1,8)==0)
                        {
                                Water=1;
                                Status();
                        }
                        else  if(strncmp("Water off",msg1,9)==0)
                        {
                                Water=0;
                                Status();
                        }
                        else  if(strncmp("Window on",msg1,9)==0)
                        {
                                Window=1;
                                Status();
                        }
                        else  if(strncmp("Window off",msg1,10)==0)
                        {
                                Window=0;
                                Status();
                        }
                        else  if(strncmp("Status",msg1,6)==0)
                        {
                                Status();
                        }
                        else
                        {
                        SMSString("AT+CMGS=\"");
                        SMSString(phone);
                        SMSString("\"\n\r");
                        delay_sms(1000);
                        SMSString("Wrong Command");
                        SMSString("\n\r");
                        delay_sms(1000);
                        }
                SMSString("AT+CMGD=1\r");
                delay_sms(1000);

                IE=0X00;   // Disable serial interrupt
                delay_sms(1000);
                }
        while(1);
}
void init(void)
{
        j=0;
        bufcounter=0;
        TL1=0XFD; //9600 @ 11.0592
        TH1=0xFD;
        TMOD=0x20;
        SCON=0x50;
        TR1=1;
}
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 delay_sms (unsigned int count)
{
        unsigned int i;
    while(count)
        {
       i = 115;
       while(i>0) i--;
       count--;
        }
}
void init_modem(void)
{
        Window=0;
        Water=0;
        SMSString("AT\r"); // AT commands to initialize gsm modem
        delay_sms(1000);

        SMSString( "ATe0\r"); // turn off echo
        delay_sms(1000);

        SMSString( "AT&W\r"); // save settings
        delay_sms(1000);

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

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

        SMSString( "AT+CMGR=1\r"); // AT command to read sms
        delay_sms(1000);
}
void Status(void)
{
        unsigned char tmp10 = sprintf(msg1 ,"Water is %s and Window is %s\n" , Water, Window);
        SMSString("AT+CMGS=\"");
        SMSString(phone);
        SMSString("\"\n");
        delay_sms(1000);
        //SMSString(tmp10);
        SMSString("\n\r");
        delay_sms(1000);
}
enum States
{
    STATE_IDLE,
    STATE_FIND_CMT,
    STATE_CMT_GET_NUMBER,
    STATE_CMT_GET_COMMA,
    STATE_CMT_GET_MSG_START,
    STATE_CMT_GET_MSG,
}
state = STATE_IDLE;
const unsigned char* pattern;
int ch;

void process_modem_character(unsigned char*msg) {
    switch (state) {
        case STATE_IDLE:
            if (msg == '+')
                        {
                state = STATE_FIND_CMT;
                pattern = "\+CMTI\: \"SM\",1\r+CMGR: \"REC UNREAD\"\,\"";
            }
            break;
        case STATE_FIND_CMT:
            if (*pattern != msg)
                        {
                state = STATE_IDLE;
            }
                        else
                        {
                pattern++;
                if (*pattern == '\0')
                                {
                    state = STATE_CMT_GET_NUMBER;
                    ch = 0;
                }
            }
            break;
        case STATE_CMT_GET_NUMBER:
            if (msg == '\"')
                        {
                state = STATE_CMT_GET_COMMA;
            }
                        else if (ch >= sizeof(phone))
                        {
                // Too long phone number :(
                state = STATE_IDLE;
            }
                        else
                        {
                                ch++;
                phone[ch] = *msg;
                phone[ch] = '\0';
            }
            break;
        case STATE_CMT_GET_COMMA:
            if (msg == ' ')
                        {
                ;    // Ignore white space
            }
                        else if (msg == ',')
                        {
                state = STATE_CMT_GET_MSG_START;
            }
                        else
                        {
                // Invalid character received
                state = STATE_IDLE;
            }
            break;

        default:
            // Invalid state.
            state = STATE_IDLE;
    } // switch
} // process_modem_character
void clear(void)
{
        unsigned char a;
        for(a=0;a<100;a++)
        msg1[a]=0x00;
}
Read-Only
Author
Nick D
Posted
12-Apr-2011 17:07 GMT
Toolset
None
New! RE: what is wrong with lining the braces up???,

Also i get error C214: illegal pointer conversion

void Status(void)
{
        unsigned char tmp10 = sprintf(msg1 ,"Water is %s and Window is %s\n" , Water, Window);
        SMSString("AT+CMGS=\"");
        SMSString(phone);
        SMSString("\"\n");
        delay_sms(1000);
        SMSString(tmp10);
        SMSString("\n\r");
        delay_sms(1000);
}


and i don't really understand why.

Read-Only
Author
PerT Westermark
Posted
12-Apr-2011 17:36 GMT
Toolset
None
New! RE: what is wrong with lining the braces up???,

Stop writing random noise and try to think about everything you write.

tmp10 is a character. Do SMSString expect a character? Doesn't it expect a pointer to one or more characters?

Water and Window are bits. So why do you think %s is good formatting parameters for the sprintf() call? Don't you think bits have some form of integer values? %s is used to print strings, not integers.

Don't post questions as soon as you get stuck. Read the manual. Make sure you have understood the relevant sections in it. Post questions if you can't understand a specific part of the manual or you think you have fulfilled the requirements given in the manual but still get errors. But do not post questions for every compilation error you get or you will never get done with this assignment.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.