Store received SMS in a variable ofr comarison

My first post here and very much a beginner.

I have the MKRNB1500 board and, using the example scripts, I have it sending and receiving SMS messages.

I need to be able to compare the revived SMS with a constant, and if it matches, turn on an output.

Here's the example that I've been playing with:

void loop() {
  int character;

  // If there are any SMSs available()
  if (sms.available()) {
    Serial.print("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 ((character = sms.read()) != -1)
    {
      Serial.println((char)character);
    }

    Serial.println(userNumber);
    Serial.println(senderNumber);

    Serial.println("\nEND OF MESSAGE");

    // Delete message from modem memory
    sms.flush();
    Serial.println("MESSAGE DELETED");
  }

  // delay(1000);

}

This part:

    while ((character = sms.read()) != -1)
    {
      Serial.println((char)character);
    }

reads and prints the SMS one character at a time to the serial monitor but I don't know how to get it into a variable as a string instead of printing to the serial monitor. It also fails to print the first character of the SMS.

Any ideas?

I haven't been able to receive SMS with my MKRNB1500, I'd like to know how you got that to work, what SIM you are using etc.

To make a string one way is to assemble it character by character like this:

txt = "";
txt.reserve(160);
while ((character = sms.read()) != -1)
 {
      Serial.println((char)character);
      txt +=  (char)character;
 }
Serial.println(txt);

I haven't tried this since I can't receive any SMS. Hope this helps to get you going.

Thank you, I'll give that a try.

I'm using a Vodafone SIM in UK.

Other than that, the sketches are as per the examples that come with the library.

I've also tried sending to the MKRNB1500 from different carriers and that works also.

There has been a lot of problems reported here on the forum about receiving SMS on the MKR NB 1500. What version of the modem firmware are you using?

Is your SIM a regular phone SIM, or is it an IoT SIM?

Did you get the SMS into a string working?

I don't know what firmware I have. It just worked straight away out of the box.

If you can tell me how to find the firmware version I'll let you know.

My SIM is a regular Vodafone SIM. Not IoT.

I've not tried the SMS into a string yet.

There is an example file for the MKR 1500 called SerialSARAPassthrough.
From IDE: File/Examples/MKRNB/Tools/SerialSaraPassthrough. Run that program and enter ati and ati9. This will display the firmware version and date.
This program also lets you try various "AT+" commands if you want to do some advanced stuff.

Ripvega,

I don't know much about Arduino hardware (yet), to answer your original code, you have to build a string in a buffer. Let's assume the string you want to use is 50, or less characters in length. The first thing would be to declare a string buffer.

char strBuffer[50];

Next, in your while loop you would have to add each character to the buffer as it is read from the message. You would need an index. The logic to build the buffer would look something like the following.

int i;

i = 0;
while ((character = sms.read()) != -1)
    {
       strBuffer[i] = character;
       i++;
      Serial.println((char)character);
    }
if (i < 50)
{
     strBuffer[i] = 0;
}
else
{
   strBuffer[49] = 0;
}

I hope this helps!