Hi everyone,
I intend to build a sim800l gsm-based security device which is controlled by arduino nano. My goal in this scope is to check whether the received call to the gsm module is from a predefined number.
I was able to write a function to parse telephone number data from from serial. Output from sim800l is something like this.
+CLIP: "771234567",161,"",0,"",0
Below is the function I used.
String check_call(){
String recieved_call_num ;
if (sim800l.available()) {
char c = sim800l.read();
if (c == '\n') {
String response = sim800l.readStringUntil('\n');
if (response.indexOf("+CLIP:") != -1) {
Serial.println("call detected");
//Serial.println(response.indexOf(" "));
recieved_call_num = response.substring(8,16);
}
return recieved_call_num ;
}
}
}
But the problem is when i call the function it does not compare with the given number.
void loop()
{
String number ="771234567";
if(number.equals(check_call())){
Serial.println("Call received from a verified nuimber");
}
}
When i checked the "number" variable it is char[10] and the output of the check_call() function is a string. I also tried converting "number" variable to string and didn't work.
Are there any solution to this?
Thanks in advance for your help. ![]()
here is the full code
#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial sim800l(7, 3);
String my_number = "+94771234567";
void shutdown(){
while ((true))
{
/* code */
}
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
sim800l.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(sim800l.available())
{
Serial.write(sim800l.read());//Forward what Software Serial received to Serial Port
}
}
void sendSMS(String phoneNumber, String message) {
sim800l.println("AT+CMGF=1"); // Set SMS text mode
updateSerial();
sim800l.println("AT+CMGS=\""+phoneNumber+"\""); // Start SMS sending mode
updateSerial();
sim800l.println(message); // Send SMS content
updateSerial();
sim800l.write(26); // Ctrl+Z to send SMS
updateSerial();
delay(5000); // Wait for SMS to be sent
}
void call(String phone_num){
sim800l.println("ATD" + phone_num + ";"); // change ZZ with country code and xxxxxxxxxxx with phone number to dial
updateSerial();
//delay(20000); // wait for 20 seconds...
//sim800l.println("ATH"); //hang up
//updateSerial();
}
String check_call(){
String recieved_call_num ;
if (sim800l.available()) {
char c = sim800l.read();
if (c == '\n') {
String response = sim800l.readStringUntil('\n');
if (response.indexOf("+CLIP:") != -1) {
Serial.println("call detected");
//Serial.println(response.indexOf(" "));
recieved_call_num = response.substring(8,16);
}
return recieved_call_num ;
}
}
}
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
sim800l.begin(9600);
Serial.println("Initializing...");
delay(1000);
sim800l.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
sim800l.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
updateSerial();
sim800l.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
sim800l.println("AT+CREG?"); //Check whether it has registered in the network
updateSerial();
sim800l.println("AT+CMGF=1");
updateSerial();
sim800l.println("AT+CNMI=1,2,2,0,0"); // Decides how newly arrived SMS messages should be handled - correct
updateSerial();
sim800l.println("AT+CLIP=1" );
updateSerial();
//sendSMS(my_number,"Hello");
// call(my_number);
//sim800l.println("AT+CMGR=1");
}
void loop()
{
String number ="771234567";
if(number.equals(check_call())){
Serial.println("done");
shutdown();
}
//updateSerial();
}