'how to compare array of chars with arduino
i want to compare the text that i receive from gsm board in arduino with the word Misure and Reset and reply in different case depending on request but the arduino jump on ams.flush() without reply nothing. please help me thanks
//Message REceiving
void receivemsg(float temperature){
char c;
char d[200];
int i;
{
Serial.println("Message received from:");
// Get remote number
sms.remoteNumber(senderNumber, 20);
Serial.println(senderNumber);
// An example of message disposal
// Any messages starting with # should be discarded
if(sms.peek()=='#')
{
Serial.println("Discarded SMS");
sms.flush();
}
// Read message bytes and print them
while(c=sms.read()){
d[i]=c;
Serial.print(c);
// for (i=0;i<200;i++){
// d[i]=c;}
}
Serial.println("\nEND OF MESSAGE");
// interpreter of the message
for (i=0;i<200;i++){
if (d[i]=='Misure')
// part of reply message
{
String t="Hello i'm Arduino: Umidità del terreno attuale (0-50): "+ String(sensorValue);
String f= " Temeratura attuale: ";
String d= ftoa(temperature,2,6);
String txt=t+f+d;
char txtMsg[200];
txt.toCharArray(txtMsg,140);
sms.beginSMS(senderNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");}}
for (i=0;i<200;i++){
if (d[i]=='Reset'){
char txtMsg[200]={"Reset Received... i'm resetting now please be patient thanks"};
sms.beginSMS(senderNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
//calling watchdog
Reset_AVR();}}
// Delete message from modem memory to prevent full memory space error
sms.flush();
Serial.println("MESSAGE DELETED");
delay(1000);
return;
}}
Solution 1:[1]
Declare new array with your word
char name[6]={'M','i','s','u','r','e'}
then
int x=0
for(int i=0;i<7;i++){
if(name[i]=d[i]){
int x=1;
}else{
x=0;
break;
}
}
Solution 2:[2]
You can use subString() function
substring() with only one parameter looks for a given substring from the position given to the end of the string. It expects that the substring extends all the way to the end of the String. For example:
String stringOne = "Content-Type: text/html";
// substring(index) looks for the substring from the index position to //the end:
if (stringOne.substring(19) == "html") {
}
is true, while
String stringOne = "Content-Type: text/html";
// substring(index) looks for the substring from the index position to the end:
if (stringOne.substring(19) == "htm") {
}
is not true, because there's an l after the htm in the String.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lukas |
| Solution 2 | Marco Aurelio Silva |
