I asking for help to compare two values "data" or "s" with an incomming SMS. I have an incomming SMS contain a "1". The Serial monitor shows '1' from data, same for s shows '1' . But nothing happens. I want to jump into the if{ } when true (incomming data equal to '1'.)
I already tried different setups, but still same. Any idea why?
char data[256];
char s;
void setup()
{
Serial.begin(9600);
cell.begin(9600);
delay(2000);
}
void loop()
{
cell.println("AT+CMGR=1"); //Reads the first SMS
Serial.flush();
for (x=0;x < 1;x++)
{
data[x]='\0';
}
x=0;
do
{
while(cell.available()==0);
data[x]=cell.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
char s = data[1];
Serial.println(s);
if(data[1] == 1)
{
Serial.println("TURN ON");
}
delay(5000);
}
I'd prefer to see code that can compile, or the disclaimer "pseudo code"
if(data[x-1]==0x0D&&data[x-2]=='"')
Is your space bar broken?
Do you think it OK to read data before your array start?
The code is not fully shown. It compile without errors and shows a '1' in the serial monitor when receive the SMS. So everything works. The problem is, I can't compare the incomming SMS first character date[1] with 1 in the if statement. I don't know why.
We can't know why, either, without seeing all your actual code.
-br
The code is not fully shown.
My answer isn't either.if(data[1] == 1)
for (x=0;x < 1;x++)
{
data[x]='\0';
}
Why?
Here is my full code.
#include <SoftwareSerial.h>
#include <string.h>
SoftwareSerial cell(2, 3);
int n_sms,x,sms_start;
char data[256];
char s;
void setup()
{
Serial.begin(9600);
cell.begin(9600);
delay(2000);
for (int i=0;i < 5;i++) //Wait until module is running
{
Serial.print(".");
delay(3000);
}
Serial.println();
cell.println("AT+CMGF=1"); // sets the SMS mode to text
delay(1000);
cell.println("AT+CMGD=1"); //Delete the first SMS after reading it
Serial.println("System is running");
}
void loop()
{
cell.println("AT+CMGR=1"); //Reads the first SMS
Serial.flush();
for (x=0;x < 25;x++)
{
data[x]='\0';
}
x=0;
do
{
while(cell.available()==0);
data[x]=cell.read();
x++;
if(data[x-1]==0x0D&&data[x-2]=='"')
{
x=0;
}
}
while(!(data[x-1]=='K'&&data[x-2]=='O')); // Skip the OK from SMS
data[x-3]='\0'; //finish the string before the OK
Serial.println(data); //shows the message
char s = data[1]; // Move first characters of incomming data[1] to s.
Serial.println(s); // print the value of s
if(data[1] == 1) // Compare if data[1] is equal to '1'
{
Serial.println("TURN ON"); // If value from data[1] is true (=1) then print.
}
delay(5000);
}
PROBLEM SOLVED!
I changed the if statement to following:
if ((( char) data[1] ) == '1');
It works!
There's no need to cast something to the same type it already is.
That semicolon doesn't belong there, so I'm assuming you haven't fixed it.
You also need to revisit the code that reads before the start of your array.
That sort of stuff bites you on the derrière sooner or later.