Pages: 1 2 [3] 4 5   Go Down
Author Topic: reading sms  (Read 5971 times)
0 Members and 1 Guest are viewing this topic.
South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

very sorry,ok will post all info,display on my serial monitor continuesly reading,but doesnt read any sms's
Code:
data: []
AT+CMGR
data: []
AT+CMGR
data: []
AT+CMGR
data: []
AT+CMGR
data: []
AT+CMGR
data: []
AT+CMGR
data: []
sketch
Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON


  delay(5000);


  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR");    //Reads the first SMS





  while(Serial.available()==0);

  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }
  if(strcmp(data, "off") == 0)
  {
    Serial.println("Turning LED off");
    digitalWrite(led,LOW); // Turn the pin off



  }   
}     
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  while(Serial.available()==0);

  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }
Send a command. Wait until there is a reply. Ignore the reply. Expect, magically, the data to have been read anyway.

Can anyone else see a problem with this?

What happened to the code to actually read the reply?
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok,this is my full code,does not loop when checking for sms's,

Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON

  for (int i=0;i < 5;i++){
    delay(5000);
  }

  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR");    //Reads the first SMS
 


  Serial.flush();
  for (x=0;x < 255;x++){           
    data[x]='\0';                       
  }
  x=0;
  do{
    while(Serial.available()==0);
    data[x]=Serial.read(); 
    x++;           
    if(data[x-1]==0x0D&&data[x-2]=='"'){
      x=0;
    }
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

  data[x-3]='\0';        //finish the string before the OK
   
  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }
  if(strcmp(data, "off") == 0)
  {
    Serial.println("Turning LED off");
    digitalWrite(led,LOW); // Turn the pin off



  }   
}     
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok,this is my full code,does not loop when checking for sms's,
What output do you get?

Why is that Serial.flush() back? GET RID OF IT!!!
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stops checking after ;
Code:
AT+CMGF=1
AT+CMGR

sketch
Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON

  for (int i=0;i < 5;i++){
    delay(5000);
  }

  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR=1");    //Reads the first SMS
 


  
  for (x=0;x < 255;x++){            
    data[x]='\0';                        
  }
  x=0;
  do{
    while(Serial.available()==0);
    data[x]=Serial.read();  
    x++;          
    if(data[x-1]==0x0D&&data[x-2]=='"'){
      x=0;
    }
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

  data[x-3]='\0';        //finish the string before the OK
  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }
  if(strcmp(data, "off") == 0)
  {
    Serial.println("Turning LED off");
    digitalWrite(led,LOW); // Turn the pin off



  }    
}    
« Last Edit: January 10, 2013, 08:19:48 am by kevin-se » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
stops checking after ;
You don't know that. All you know is that your reading loop never ends. So, what is happening in that loop?

Code:
 for (x=0;x < 255;x++){            
    data[x]='\0';                        
  }
If you understood C strings, you'd know why this was unnecessary.
Code:
data[0] = '\0';
is all that is needed.

Make some changes.
  x=0;
  do{
    while(Serial.available()==0);
    data[x++]=Serial.read();  
   data[ x ] = '\0'; // Keep string NULL terminated
    if(data[x-1]==0x0D&&data[x-2]=='"')
    { // Down here where it belongs!
      x=0;
     data[ 0 ] = '\0';
    }
   Serial.print("data: [");
    Serial.print(data);
    Serial.println("]");

  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

Show us the output (and the code).
« Last Edit: January 10, 2013, 08:40:34 am by PaulS » Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

with some modifications,seems to read sms and then stops,dont know why the loop stops checkin
Code:
AT+CMGF=1
AT+CMGR=1
data: [
ON
]

]
AT+CMGR=1

Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON

  for (int i=0;i < 5;i++){
    delay(5000);
  }

  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR=1");    //Reads the incoming SMS's
  


 
  for (x=0;x < 255;x++){            
    data[x]='\0';                        
  }
  x=0;
  do{
    while(Serial.available()==0);
    data[x]=Serial.read();  
    x++;          
    if(data[x-1]==0x0D&&data[x-2]=='"'){
      x=0;
    }
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

  data[x-3]='\0';        //finish the string before the OK
  Serial.print("data: [");
  Serial.print(data);    //shows the message
  Serial.println("]");
  if(strcmp(data, "on") == 0)
  {
    Serial.println("Turning LED on");
    digitalWrite(led,HIGH);
  }
  if(strcmp(data, "off") == 0)
  {
    Serial.println("Turning LED off");
    digitalWrite(led,LOW); // Turn the pin off

   Serial.println("AT+CMGD=1,2"); //Deletes all recieved sms's only

  }    
}    
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok,additions to the code,have to concentrate on reading the serial correctly,
the code continues to check for new sms's but its not in a format i understand and
the module switches itself off after a few minutes,below the output then switches off,
Code:
data: [
NORMAüAT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
data: [ÿÿÿ]
data: [ÿÿÿÿ]
data: [ÿÿÿÿ
]
data: [ÿÿÿÿ
]
data: [ÿÿÿÿ
R]
data: [ÿÿÿÿ
RD]
data: [ÿÿÿÿ
RDY]
data: [ÿÿÿÿ
RDY
]
data: [ÿÿÿÿ
RDY
]
data: [ÿÿÿÿ
RDY

]
data: [ÿÿÿÿ
RDY

]
data: [ÿÿÿÿ
RDY

+]
data: [ÿÿÿÿ
RDY

+C]
data: [ÿÿÿÿ
RDY

+CF]
data: [ÿÿÿÿ
RDY

+CFU]
data: [ÿÿÿÿ
RDY

+CFUN]
data: [ÿÿÿÿ
RDY

+CFUN:]
data: [ÿÿÿÿ
RDY

+CFUN: ]
data: [ÿÿÿÿ
RDY

+CFUN: 1]
data: [ÿÿÿÿ
RDY

+CFUN: 1
]
data: [ÿÿÿÿ
RDY

+CFUN: 1
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

]
data: [ÿÿÿÿ
RDY

+CFUN: 1

]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+C]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CP]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPI]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN:]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: R]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: RE]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: REA]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

C]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Ca]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Cal]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call R]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Re]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Rea]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Read]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
A]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+C]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CM]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMG]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
A]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+C]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CM]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMG]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
d]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
da]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
dat]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data:]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: []
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
d]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
da]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
dat]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data:]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: []
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
d]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
da]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
dat]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]
data: [ÿÿ]
data]
data: [ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
data: [ÿ]

sketch
Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON

  for (int i=0;i < 5;i++){
    delay(5000);
  }

  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR=1");    //Reads incoming sms

  x=0;
  do{
    while(Serial.available()==0);
    data[x++]=Serial.read(); 
    data[ x ] = '\0';
    if(data[x-1]==0x0D&&data[x-2]=='"')
    {
      x=0;
      data[ 0 ] = '\0';
    }
    Serial.print("data: [");
    Serial.print(data);
    Serial.println("]");

  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

  data[x-3]='\0';        //finish the string before the OK

  Serial.println(data);    //shows the message


  delay(5000);

}   


Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  x=0;
  do{
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));
How valid do you suppose -1 and -2 are as indices into data?

You really need to ditch the do/while loop, and use some other construct to wait for data, and then read all data until OK appears. But, before you get started on that, think about what happens if the AT command fails, and the return does NOT include OK.
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi ,been trying to modify the code for many hours but no matter what i add or modify always end up with incorrect readings,tried reading up on strings and used them without any luck,all i have is this perfect code for reading sms's but not been able to use output data,
Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
    digitalWrite(onModulePin,HIGH);
    delay(2000);
    digitalWrite(onModulePin,LOW);
}

void setup(){

    Serial.begin(19200);                // UART baud rate
    delay(2000);
    pinMode(led, OUTPUT);
    pinMode(onModulePin, OUTPUT);
    switchModule();                    // switches the module ON

    for (int i=0;i < 5;i++){
        delay(5000);
    }

    Serial.println("AT+CMGF=1");         // sets the SMS mode to text
   
    delay(1000);
}

void loop(){
   
   
        Serial.println("AT+CMGR=1");    //Reads the first SMS
         
         
       
        x=0;
        do{
            while(Serial.available()==0);
            data[x]=Serial.read(); 
            x++;           
            if(data[x-1]==0x0D&&data[x-2]=='"'){
                x=0;
            }
        }while(!(data[x-1]=='K'&&data[x-2]=='O'));

        data[x-3]='\0';        //finish the string before the OK

        Serial.println(data);    //shows the message
       

        delay(5000);

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
all i have is this perfect code
Code:
        x=0;
        do{
        }while(!(data[x-1]=='K'&&data[x-2]=='O'));
When x is 0, as it is initially, it gets incremented to 1 inside the body of the do/while statement. Then, at then end you check the values in data[ 0 ] and data[ -1 ], to see if they are O and K. And, you call code that references an invalid element of the array "perfect". I don't think so.

Add the if(strcmp(data, "on") == 0) block to that code. After the if block, print data again. With the '[' before and ']' after it. Does data get corrupted by the strcmp() call?
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi sir, not sure if its done correctly but there's my attempt,
yes the data does get corrupted.
Code:
int led = 13;
int onModulePin = 9;        // the pin to switch on the module (without press on button)

int timesToSend = 1;        // Numbers of SMS to send
int count = 0;

int n_sms,x,sms_start;
char data[256];

void switchModule(){
  digitalWrite(onModulePin,HIGH);
  delay(2000);
  digitalWrite(onModulePin,LOW);
}

void setup(){

  Serial.begin(19200);                // UART baud rate
  delay(2000);
  pinMode(led, OUTPUT);
  pinMode(onModulePin, OUTPUT);
  switchModule();                    // switches the module ON

  for (int i=0;i < 5;i++){
    delay(5000);
  }

  Serial.println("AT+CMGF=1");         // sets the SMS mode to text

  delay(1000);
}

void loop(){


  Serial.println("AT+CMGR=1");    //Reads the first SMS



  x=0;
  do{
    while(Serial.available()==0);
    data[x]=Serial.read(); 
    x++;           
    if(data[x-1]==0x0D&&data[x-2]=='"'){
      x=0;
    }
      if(strcmp(data, "on") == 0)
      Serial.print("data: [");
      Serial.print(data);
      Serial.println("]");
   
  }
  while(!(data[x-1]=='K'&&data[x-2]=='O'));

  data[x-3]='\0';        //finish the string before the OK

  Serial.println(data);    //shows the message


  delay(5000);

}
Code:
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ]
ÿÿÿÿ
]
ÿÿÿÿ
]
ÿÿÿÿ
R]
ÿÿÿÿ
RD]
ÿÿÿÿ
RDY]
ÿÿÿÿ
RDY
]
ÿÿÿÿ
RDY
]
ÿÿÿÿ
RDY

]
ÿÿÿÿ
RDY

]
ÿÿÿÿ
RDY

+]
ÿÿÿÿ
RDY

+C]
ÿÿÿÿ
RDY

+CF]
ÿÿÿÿ
RDY

+CFU]
ÿÿÿÿ
RDY

+CFUN]
ÿÿÿÿ
RDY

+CFUN:]
ÿÿÿÿ
RDY

+CFUN: ]
ÿÿÿÿ
RDY

+CFUN: 1]
ÿÿÿÿ
RDY

+CFUN: 1
]
ÿÿÿÿ
RDY

+CFUN: 1
]
ÿÿÿÿ
RDY

+CFUN: 1

]
ÿÿÿÿ
RDY

+CFUN: 1

]
ÿÿÿÿ
RDY

+CFUN: 1

+]
ÿÿÿÿ
RDY

+CFUN: 1

+C]
ÿÿÿÿ
RDY

+CFUN: 1

+CP]
ÿÿÿÿ
RDY

+CFUN: 1

+CPI]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN:]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: R]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: RE]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: REA]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

C]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Ca]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Cal]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call R]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Re]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Rea]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Read]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
A]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+C]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CM]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMG]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
A]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+C]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CM]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMG]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ]]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
ÿÿÿÿ]
]
ÿÿÿÿ
RDY

+CFUN: 1

+CPIN: READY

Call Ready
AT+CMGF=1
AT+CMGR=1
ÿ]
ÿÿ]
ÿÿÿ]
Logged

South Africa
Offline Offline
Newbie
*
Karma: 0
Posts: 33
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
found a sketch for sim900,that should activate led pin output when receives an sms with on but does not,my serial monitors shows the sketch reading the sms without any problems but no led lights up.Got the gsm shield set on software serial and using uno R3,also tried using it in hardware serial mode,did not work,in hardware serial mode pin1 and 2 are (txrx),not sure how to re-configure in the sketch and not sure why led is not high when sms with ,on is sent.

[code]#define _SS_MAX_RX_BUFF 128 // RX buffer size
#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(7, 8);

// EN: String buffer for the GPRS shield message
// FR: Mémoire tampon de type string pour les messages du shield GPRS
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
// FR: Est mis à 1 quand le prochain message du shield GPRS contiendra le contenu du SMS
int SmsContentFlag = 0;

// EN: Pin of the LED to turn ON and OFF depending on the received message
// FR: Pin de la LED a allumer/éteindre en fonction du message reçu
int ledPin = 13;

// EN: Code PIN of the SIM card (if applied)
// FR: Code PIN de la carte SIM (si applicable)
String SIM_PIN_CODE = String( "XXXX" );
 
void setup()
{
  mySerial.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);                 // the GPRS baud rate

  // Initialize la PIN
  pinMode( ledPin, OUTPUT );
  digitalWrite( ledPin, LOW );
}
 
void loop()
{
    char SerialInByte;
     
    if(Serial.available())
    {
       mySerial.print((unsigned char)Serial.read());
     } 
    else  if(mySerial.available())
    {
        char SerialInByte;
        SerialInByte = (unsigned char)mySerial.read();
       
        // EN: Relay to Arduino IDE Monitor
        // FR: Relayer l'information vers le moniteur Serie Arduino
        Serial.print( SerialInByte );
       
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // FR: Le programme écoute également les messages issus du GPRS Shield.
        // -------------------------------------------------------------------
       
        // EN: If the message ends with <CR> then process the message
        // FR: Si le message se termine par un <CR> alors traiter le message
        if( SerialInByte == 13 ){
          // EN: Store the char into the message buffer
          // FR: Stocké le caractère dans le buffer de message
          ProcessGprsMsg();
         }
         if( SerialInByte == 10 ){
            // EN: Skip Line feed
            // FR: Ignorer les Line Feed
         }
         else {
           // EN: store the current character in the message string buffer
           // FR: stocker le caractère dans la mémoire tampon réservé au message
           msg += String(SerialInByte);
         }
     }   
}

// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
// FR: Execute une action sur base du contenu d'un SMS.
//     Notez que le contenu du SMS est le résultat du traitement de plusieurs messages du shield GPRS.
void ProcessSms( String sms ){
  Serial.print( "ProcessSms for [" );
  Serial.print( sms );
  Serial.println( "]" );
 
  if( sms.indexOf("on") >= 0 ){
    digitalWrite( ledPin, HIGH );
    Serial.println( "LED IS ON" );
    return;
  }
  if( sms.indexOf("off") >= 0 ){
    digitalWrite( ledPin, LOW );
    Serial.println( "LED IS OFF" );
    return;
  }
}

// EN: Send the SIM PIN Code to the GPRS shield
// FR: Envoyer le code PIN de la carte SIM au shield GRPS
void GprsSendPinCode(){
  if( SIM_PIN_CODE.indexOf("XXXX")>=0 ){
    Serial.println( "*** OUPS! you did not have provided a PIN CODE for your SIM CARD. ***" );
    Serial.println( "*** Please, define the SIM_PIN_CODE variable . ***" );
    return;
  }
  mySerial.print("AT+CPIN=");
  mySerial.println( SIM_PIN_CODE );
}

// EN: Request Text Mode for SMS messaging
// FR: Demande d'utiliser le mode Text pour la gestion des messages
void GprsTextModeSMS(){
  mySerial.println( "AT+CMGF=1" );
}

void GprsReadSmsStore( String SmsStorePos ){
  // Serial.print( "GprsReadSmsStore for storePos " );
  // Serial.println( SmsStorePos );
  mySerial.print( "AT+CMGR=" );
  mySerial.println( SmsStorePos );
}

// EN: Clear the GPRS shield message buffer
// FR: efface le contenu de la mémoire tampon des messages du GPRS shield.
void ClearGprsMsg(){
  msg = "";
}

// EN: interpret the GPRS shield message and act appropiately
// FR: interprete le message du GPRS shield et agit en conséquence
void ProcessGprsMsg() {
  Serial.println("");
  Serial.print( "GPRS Message: [" );
  Serial.print( msg );
  Serial.println( "]" );

/*The following code used only when you have set pin code to your SIM card*/
//  if( msg.indexOf( "+CPIN: SIM PIN" ) >= 0 ){
//     Serial.println( "*** NEED FOR SIM PIN CODE ***" );
//     Serial.println( "PIN CODE *** WILL BE SEND NOW" );
//     GprsSendPinCode();
//  }

  if( msg.indexOf( "Call Ready" ) >= 0 ){
     Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
     GprsTextModeSMS();
  }
 
  // EN: unsolicited message received when getting a SMS message
  // FR: Message non sollicité quand un SMS arrive
  if( msg.indexOf( "+CMTI" ) >= 0 ){
     Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     // FR: Rechercher la position de la virgule dans le message complet (+CMTI: "SM",6)
     //     Dans l'exemple, le SMS est stocké à la position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
     Serial.print( "SMS stored at " );
     Serial.println( SmsStorePos );
     
     // EN: Ask to read the SMS store
     // FR: Demande de lecture du stockage SMS
     GprsReadSmsStore( SmsStorePos );
  }
 
  // EN: SMS store readed through UART (result of GprsReadSmsStore request) 
  // FR: Lecture du stockage SMS via l'UART (résultat de la requete GprsReadSmsStore)
  if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    // FR: Le prochain message contiendra le contenu du SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    // FR: Les ligne suivantes sont essentielle pour ne pas effacer le flag!
    ClearGprsMsg();
    return;
  }
 
  // EN: +CMGR message just before indicate that the following GRPS Shield message
  //     (this message) will contains the SMS body
  // FR: le message +CMGR précédent indiquait que le message suivant du Shield GPRS
  //     (ce message) contient le corps du SMS
  if( SmsContentFlag == 1 ){
    Serial.println( "*** SMS MESSAGE CONTENT ***" );
    Serial.println( msg );
    Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
 
  ClearGprsMsg();
  // EN: Always clear the flag
  // FR: Toujours mettre le flag à 0
  SmsContentFlag = 0;
}

Code:
GPRS Message: [0Àÿÿÿÿÿÿÿÿ]

RDY

GPRS Message: [
RDY]



GPRS Message: [
]

+CFUN: 1

GPRS Message: [
+CFUN: 1]



GPRS Message: [
]

+CPIN: READY

GPRS Message: [
+CPIN: READY]



GPRS Message: [
]

Call Ready

GPRS Message: [
Call Ready]
*** GPRS Shield registered on Mobile Network ***

AT+CMGF=1

GPRS Message: [
AT+CMGF=1]



GPRS Message: [
]

OK

GPRS Message: [
OK]



GPRS Message: [
]

+CMTI: "SM",1

GPRS Message: [
+CMTI: "SM",1]
*** SMS Received ***
SMS stored at 1

AT+CMGR=1

GPRS Message: [
AT+CMGR=1]



GPRS Message: [
]

+CMGR: "REC UNREAD","+27764569***","","13/01/11,22:24:02+08"

GPRS Message: [
+CMGR: "REC UNREAD","+27764569***","","13/01/11,22:24:02+08"]



GPRS Message: [
]
*** SMS MESSAGE CONTENT ***


*** END OF SMS MESSAGE ***
ProcessSms for [
]

OK

GPRS Message: [
OK]



GPRS Message: [
]

+CMTI: "SM",2

GPRS Message: [
+CMTI: "SM",2]
*** SMS Received ***
SMS stored at 2

AT+CMGR=2

GPRS Message: [
AT+CMGR=2]



GPRS Message: [
]

+CMGR: "REC UNREAD","+27764569***","","13/01/11,22:04:23+08"

GPRS Message: [
+CMGR: "REC UNREAD","+27764569***","","13/01/11,22:04:23+08"]



GPRS Message: [
]
*** SMS MESSAGE CONTENT ***


*** END OF SMS MESSAGE ***
[/code]
« Last Edit: January 11, 2013, 03:53:23 pm by kevin-se » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
hi sir, not sure if its done correctly but there's my attempt,
yes the data does get corrupted.
No, it doesn't. If it did, the output would be correct before the if statement, and corrupt after. It is not correct at any point.

The fact that you are trying to talk to the phone and the PC on the same serial port is the problem.

Quote
found a sketch for sim900,that should activate led pin output when receives an sms with on but does not,my serial monitors shows the sketch reading the sms without any problems but no led lights up.Got the gsm shield set on software serial and using uno R3,also tried using it in hardware serial mode,did not work,in hardware serial mode pin1 and 2 are (txrx),not sure how to re-configure in the sketch and not sure why led is not high when sms with ,on is sent.

Look through the output you showed. Where do you see "on" or "off" in  that output?
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8915
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
hi sir, not sure if its done correctly but there's my attempt,

The below web code shows how to capture an incomming string and see if it contains an "on' or "off", then act appropriately. Might have something useful for you.

Code:
//zoomkat 12-08-12
//get submit box code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html or use a '
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84);; //server port

String readString;

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print
  Serial.begin(9600);
  Serial.println("server text box test1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on' or 'off': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'><BR>");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

          client.println("</FORM>");

          client.println("<BR>");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("on") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pages: 1 2 [3] 4 5   Go Up
Jump to: