Switch on an LED via SMS

Hi everyone,

my project is to send a SMS to the SIM800L module which is connected with an Arduino Nano. The Nano should compare the SMS to a saved phone number and if the SMS is from the number it should switch on an LED.

I have achieved, that i can see the SMS in the Serial Monitor, when in the void loop only is updateSerial.

Can someone pls help me, because i don´t get it, where my mistake is.

#include <SoftwareSerial.h>
#include <String.h>

#define PIN_TX 8
#define PIN_RX 9

SoftwareSerial mySerial(PIN_RX, PIN_TX);

int LED = 5;                        //Set Led to Pin 5
char num_1 = "+491525342xxx";      //String with first mobilenumber
String textSms;                     //String to store text

void setup()
{
  pinMode (LED, OUTPUT);            //Set Pin 5 as Output
  digitalWrite (LED, LOW);
  
  Serial.begin(38400);              //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  mySerial.begin(38400);            //Begin serial communication with Arduino and SIM800L
  delay (1000);

  Serial.println("Initializing...");

  // Once the handshake test is successful, it will back to OK
  mySerial.println("AT");
  delay (1000);
  updateSerial();

  // Configuring TEXT mode
  mySerial.println("AT+CMGF=1");
  delay (1000);
  updateSerial();

  // Decides how newly arrived SMS messages should be handled
  mySerial.println("AT+CNMI=1,2,0,0,0");
  updateSerial();
}

void loop()
{
  updateSerial();
  textSms = Serial.readString();                              //this will save the whole data to the String variable d..                             
  
  if(strcmp(textSms.indexOf("+4915253426039"), num_1) >= 0)  // checking whether the msg(that is saved in d) has got "num_1" in it
   {
   digitalWrite(LED, HIGH);                                  // turning on led
   }
}

void updateSerial()
{
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
  }
}

This won't read the SMS message from the SIM800 because you most likely already read it in the updateSerial routine... that just executed.

ok. so how can i then compare the updateserial with the saved number?
or must i work around without the updateSerial?

Try this...

char buffer[256];
uint8_t idx = 0;


void loop()
{
  updateSerial();

  if (idx > 0)
  {
    char *ptr = strstr(buffer, "+491525342xxx");

    if (ptr)
      Serial.println("Received SMS from known number");
    else
      Serial.println("Received something else");  

    idx = 0;  
  }
}

void updateSerial()
{

  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  
  while(mySerial.available()) 
  {
    char c = mySerial.read();    
    Serial.write(c);              //Forward what Software Serial received to Serial Port
    buffer[idx++] = c;
  }
}

Thanks a lot. Now i get back that i have recieved it, from a known number.

Initializing...
AT

OK
AT+CMGF=1

OK
Received something else
AT+CNMI=1,2,0,0,0

OK
Received something else

+CMT: "+491525342xxx","","22/05/29,16:16:19+08"
A
Received SMS from known number

Do i have to delete the update serial by the AT commands, because now i get the "Received somthing else" there?

Where does the pointer on ptr goes? that i haven´t figuerd out yet.

It's abandoned on the call stack when the loop function executes a return. That is because you declared it local to the loop() function.

thanks for the explination

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.