some sms are not filtered

Hi I am filtering all new sms if senders number are registered. If not registered, sms will not display on my led matrix. How ever if an Operator SMS (SIM Provider Promo ads through sms) is recieved sms is still displaying.

My codes is working except if there is an operator sms recieved. Here is my code.

char smsbuffer[160];
char n[20];
const char adminNum[] = "+639212888844";

void checkSMS(void)
{
  if(started){
    int position = sms.IsSMSPresent(SMS_UNREAD);   //Call SMS_UNREAD from GSM Library to bring back new sms only that marked UNREAD
    if (position > 0) {
      sms.GetSMS(position, n, smsbuffer, 160);
      //Check if sender number is the admin. If not, Discard the sms. (n) is the sender's number holder.
      if(strncmp(n, adminNum, 12) == 0)
      {
        while(smsbuffer[r+1] != '\0')
        {
          r++;
        }
        r = 0;
        showMSG = 1;
      }
    }
  }
};

I suspect, the culprit is that Operator SMS has pre-defined sender's name. Like BUDDY Balance, SMARTLoad. and some number with 4 digits only.

Here is my code.

Wrong. That is only some of your code.

Coz that's the only part of GSM module codes. And where the error occured. I did not said Here is my Whole Sketch code. Most of the parts of my whole codes are not related to my question, so it is not necessary to include.

I just call that gsm codes in some part of my led matrix code.

check strcmp part, that's the place where filtering is happen.

Some parts are not related to GSM Module that's why I did not include it.

        while(smsbuffer[r+1] != '\0')
        {
          r++;
        }
        r = 0;

The first 4 lines hardly seem useful. Increment r until a NULL is found. Then, without doing anything more, set r to 0. Why?

What does showMsg having a value of 1 mean? Where is it set to some other value if the number is not the one in adminNum?

showMSG is a boolean with default 0 value. If showMSG is 0, the default Message will appear in my led matrix, if a new sms is arrive, the default message will be replace by smsbuffer content, showMSG will be value of 1 now indicating that led matrix code part will display sms. showMSG value will be change if sender's number matched.

showMSG is a boolean with default 0 value.

If showMsg IS a boolean, the values it is set to should be true or false, not 1.

Since your ratio of answers to questions is less than 1, you need to post all of your code, if you want help.

As you suggest, I revised my codes into this.

char smsbuffer[160];
char n[20];
const char adminNum[] = "+639212888844";
boolean showMSG = false;

void checkSMS(void)
{
  if(started){
    int position = sms.IsSMSPresent(SMS_UNREAD);   //Call SMS_UNREAD from GSM Library to bring back new sms only that marked UNREAD
    if (position > 0) {
      sms.GetSMS(position, n, smsbuffer, 160);
      //Check if sender number is the admin. If not, Discard the sms. (n) is the sender's number holder.
      if(strncmp(n, adminNum, 12) == 0)
      {
        showMSG = true;
      }
    }
  }
};

I can’t post all of my codes coz most of the parts are belongs to led matrix, to avoid misleading of topic. And not related to my question. I just call that function when pointer is reach in my led matrix.

And the main question is Why Some SMS are not filtered if that SMS are came from an Servive Provider.
While I am checking is first using strcmp.

And the main question is Why Some SMS are not filtered if that SMS are came from an Servive Provider.

To determine that, I’d print the phone number that the SMS came from. adminNum contains 13 characters. Why are you comparing 12?

Why are you setting showMsg to true, if the SMS came from that number? You implied that those were messages that you did not want to show.

aah...I see now where's my fault why those sms are displaying also.

Thanks Paul :)

EDIT:

void checkSMS(void)
{
  Serial.print("checkSMS called");
  if(started){
    int position = sms.IsSMSPresent(SMS_UNREAD);   //Call SMS_UNREAD from GSM Library to bring back new sms only that marked UNREAD
    if (position > 0) {
      sms.GetSMS(position, n, smsbuffer, 160);
      //Check if sender number is the admin. If not, Discard the sms. (n) is the sender's number holder.
      if(strcmp(n, adminNum)) == 0)
      {
        strcpy(sms_final, strdup(strcat(smsbuffer, "           #")));
        showMSG = true;
      }
      else
      {
        Serial.print("NotRegistered # ");
      }
    }
  }
};

I hope this will work. As of now the hardware is not in my hands now, so I can't test the codes.

I hope this will work.

I wouldn't count on it. I see several problems.

First, strcmp(n, adminNum) returns +1, 0, or -1, depending on the relationship, lexicographically, between n and adminNum. Zero mean that they are the same. Now, you've said that it is the messages from that number that you don't want to show. So, it appears that you would want to set showMsg to false in the if body, not true.

Second:

        strcpy(sms_final, strdup(strcat(smsbuffer, "           #")));

strdup() returns a pointer to the memory it allocated. You are discarding that pointer. You'll never be able to free that memory , then. It will be permanently leaked.

There is no reason for duplicating the array that strcat() returns the pointer to.

Finally, the semicolon at the end of the function is not necessary.

You're right paul, codes not working, ouch.

So what's the better way to trap that sms from sim provider, using that code logic, I don't want those sms to be displayed on my led matrix, only the sms came from that adminNum.

And, how can I add " #" at the end of sms before I call it to display in led matrix.

So what's the better way to trap that sms from sim provider, using that code logic, I don't want those sms to be displayed on my led matrix, only the sms came from that adminNum.

I would expect that that function would always set showMsg, if there is a complete message. That it does not looks like a problem to me.

Have you removed the strdup() call?

After receiving my first sms, showMSG will remain true. Unless my arduino reset. Mybe this should work now. I hope so, I can't test it now until an operator sms is received.

void checkSMS(void)
{
  Serial.print("checkSMS called");
  if(started){
    int position = sms.IsSMSPresent(SMS_UNREAD);   //Call SMS_UNREAD from GSM Library to bring back new sms only that marked UNREAD
    if (position > 0) {
      sms.GetSMS(position, n, smsbuffer, 160);
      //Check if sender number is the admin. If not, Discard the sms. (n) is the sender's number holder.
      if(strncmp(n, adminNum, sizeof(adminNum)) == 0)
      {
        strcpy(sms_final, strcat(smsbuffer, "           #"));
        showMSG = true;
      }
      else
      {
        Serial.print("NotRegistered # ");
      }
    }
  }
}

I just strcpy() smsbuffer to sms_final, so that sms in led will not change until strcmp is true.

aybyd: After receiving my first sms, showMSG will remain true. Unless my arduino reset. Mybe this should work now. I hope so, I can't test it now until an operator sms is received.

void checkSMS(void)
{
  Serial.print("checkSMS called");
  if(started){
    int position = sms.IsSMSPresent(SMS_UNREAD);   //Call SMS_UNREAD from GSM Library to bring back new sms only that marked UNREAD
    if (position > 0) {
      sms.GetSMS(position, n, smsbuffer, 160);
      //Check if sender number is the admin. If not, Discard the sms. (n) is the sender's number holder.
      if(strncmp(n, adminNum, sizeof(adminNum)) == 0)
      {
        strcpy(sms_final, strcat(smsbuffer, "           #"));
        showMSG = true;
      }
      else
      {
        Serial.print("NotRegistered # ");
        showMSG=false;
      }
    }
  }
}

I just strcpy() smsbuffer to sms_final, so that sms in led will not change until strcmp is true.

I can't see where you reset showMSG value, maybe you should add the line I've put under "Serial.print("NotRegistered");" : showMSG=false;?

if that doesn't work, add some Serial.print() , printing sender number "n" (don't like this name for a char array ;) ) and adminNUM to check you compare the right things ;)

Reset means, physically reset my arduino. If I set my showMSG to false, if unregistered sender is recieved, A default message will reappear on my led matrix. This will remain true if an sms is recieve.

aybyd:
Reset means, physically reset my arduino.
If I set my showMSG to false, if unregistered sender is recieved, A default message will reappear on my led matrix. This will remain true if an sms is recieve.

then I don’t see the purpose of showMSG .
you’ve only posted a little part of your sketch, which doesn’t show where it is used (only where it i set), where the SMS from ‘unregistered’ is discarded etc…
If a SMS from adminNUM is received, showMSG is true again
Whether the default message appears or not on the led matrix is up to you, your sketch only does what you tell it to do . You could display the message when an unwanted sms comes, but only during a little time etc…