0

Hello i want to get and compare incomming call number, it works but not as I want. When im calling from good number it print that I was calling form saved number when the number is stop calling i need to do it on start. And else is printing when nobody is calling. How can i do it? Here is my code:

String d = "";
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial1.println("AT");
  delay(500);
  Serial1.println("AT+CLIP=1");
  delay(1000);
  Serial1.println("AT+CPIN=1439");
}
void loop()
{ 
 d = Serial1.readString();
 if(d.indexOf("xxxxxxxxx") >=0)
   {
 Serial.println("call is from saved number");
    } else {
       Serial.println("call is not from saved number");
      }
}
  • 2
    Hint#1: "readString()" has a timeout. What should your code do then? arduino.cc/en/Serial/ReadString – Mikael Patel Feb 9 at 10:30
  • @MikaelPatel When it's recognised number I will be sending HTTP POST to my api. – Jasiek Piaskowy Feb 9 at 10:40
  • You need to check that a string has received and not returned due to a timeout. It is only when a string with a number, a call, has been received that the valid phone number should be checked. Please fix the control logic of your sketch. – Mikael Patel Feb 9 at 16:41
  • Could you help me with it? I'm new in C but I need it to build my system to mobile app – Jasiek Piaskowy Feb 9 at 18:17
  • You could for instance add "if (d.length() == 0) return;" after "d = Serial1.readString();". – Mikael Patel Feb 9 at 22:23
1

The Arduino Serial class member function readString() has a builtin timeout. The function will return on timeout. The default timeout period is 1000 ms. The timeout can be set with setTimeout().

The sketch needs to filter out empty lines (timeout). This can be done by adding a check of the string length. See below for an example of checking:

void loop()
{ 
  String d = Serial1.readString(); 
  if (d.length() == 0) return;
  Serial.print(d);
  if (d.indexOf("xxxxxxxxx") >= 0) {
    Serial.println(": call is from saved number");
  } 
  else {
    Serial.println(": call is not from saved number");
  }
}

Cheers!

  • It still doesn't work – Jasiek Piaskowy Feb 10 at 18:48
  • Try the above and check the output to the serial monitor. – Mikael Patel Feb 10 at 21:16
  • I have tried but the biggest problem is that serial print print call is from saved number after I decline calling – Jasiek Piaskowy Feb 11 at 15:05
  • Please add an example of the output to the serial monitor. That would help understand the issue. – Mikael Patel Feb 11 at 15:27
  • imgur.com/a/PKHDP84 – Jasiek Piaskowy Feb 11 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.