SIM808 GSM trouble reading incoming SMS

Hi All,

I am currently trying to use an Arduino UNO with a sim808 gsm module and running a sample code from a sim900 library (which I read is compatible with minor adjustments, of which I adjusted). The issue I am having is when I receive an sms it will print on the Serial monitor a smsposition = -2, and then a sms position of 1 and then print the value of the sms_text which I had sent.

This happens occasionally yet at times the program works fine, and at times it'll pull sms which had been sent 30 mins ago. below are the code and a snippet of the serial monitor (the redacted portion is the sender's phone number)

#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

//debug begin
char sms_position;
char phone_number[20]; // array for the phone number string
char sms_text[100];
int i;
//debug end


void setup()
{
    //Serial connection.
    Serial.begin(9600);
    Serial.println("GSM Shield testing.");
    //Start configuration of shield with baudrate.
    //For http uses is raccomanded to use 4800 or slower.
    if (gsm.begin(4800)) 
    {
        Serial.println("\nstatus=READY");
        started=true;
    } 
    else 
        Serial.println("\nstatus=IDLE");

    if(started) 
    {
        //Enable this two lines if you want to send an SMS.
        //if (sms.SendSMS("3471234567", "Arduino SMS"))
        //Serial.println("\nSMS sent OK");
       
       //if NO SPACE ,you need delte SMS  from position 1 to position 20
       //please enable this four lines
       for(i=1;i<=20;i++)
       {
           sms.DeleteSMS(i);
       }
    }
    
};

void loop()
{
    if(started) 
    {
        //Read if there are messages on SIM card and print them.
        sms_position=sms.IsSMSPresent(SMS_UNREAD);
        if (sms_position) 
        {
            // read new SMS
            Serial.print("SMS postion:");
            Serial.println(sms_position,DEC);
            sms.GetSMS(sms_position, phone_number, sms_text, 100);
            // now we have phone number string in phone_num
            Serial.println(phone_number);
            // and SMS text in sms_text
            Serial.println(sms_text);
        }   
        else
        {
            Serial.println("NO NEW SMS,WAITTING");
        }     
        delay(1000);
    }
};

GSM Shield testing.
status=READY
SMS postion:1
Hi
SMS postion:2
Hello
SMS postion:3
200
SMS postion:-2
200
SMS postion:4

Shop

The above section was copied and pasted from the Serialmoniter, the issue is that sms position 3 should have printed "search" not "200" and position 4 should have contained "100", not shop. Shop was sent 10 messages ago.

and a snippet of the serial monitor (the redacted portion is the sender's phone number)

The serial monitor ONLY knows how to display text. Copy and paste (after editing, if needed) the TEXT, NOT a picture of text.

PaulS:
The serial monitor ONLY knows how to display text. Copy and paste (after editing, if needed) the TEXT, NOT a picture of text.

Thank you for the correction, I believe I have added it correctly.

I don't know what library you are using, but the fact that it returns a -2 when you ask if there is an SMS present, and you treat that as a valid position bothers me. If it were me, I'd only be trying to read text messages if the position value was sensible. -2 is not.

PaulS:
I don't know what library you are using, but the fact that it returns a -2 when you ask if there is an SMS present, and you treat that as a valid position bothers me. If it were me, I'd only be trying to read text messages if the position value was sensible. -2 is not.

Changed the type of variable sms_position from char to byte which excludes negative numbers and that seems to have solved the problem, below is the updated code for anyone who runs into this issue.

Here is a link to the library for reference: GitHub - MarcoMartines/GSM-GPRS-GPS-Shield: GSM/GPRS & GPS Shield Library for modules using SIM900/SIM908

#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"

//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;

//To change pins for Software Serial, use the two lines in GSM.cpp.

//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.

//Simple sketch to send and receive SMS.

int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];

//debug begin
byte sms_position;
char phone_number[20]; // array for the phone number string
char sms_text[100];
int i;
//debug end


void setup()
{
    //Serial connection.
    Serial.begin(9600);
    Serial.println("GSM Shield testing.");
    //Start configuration of shield with baudrate.
    //For http uses is raccomanded to use 4800 or slower.
    if (gsm.begin(4800)) 
    {
        Serial.println("\nstatus=READY");
        started=true;
    } 
    else 
        Serial.println("\nstatus=IDLE");

    if(started) 
    {
        //Enable this two lines if you want to send an SMS.
        //if (sms.SendSMS("3471234567", "Arduino SMS"))
        //Serial.println("\nSMS sent OK");
       
       //if NO SPACE ,you need delte SMS  from position 1 to position 20
       //please enable this four lines
       for(i=1;i<=20;i++)
       {
           sms.DeleteSMS(i);
       }
    }
    
};

void loop()
{
    if(started) 
    {
        //Read if there are messages on SIM card and print them.
        sms_position=sms.IsSMSPresent(SMS_UNREAD);
        if (sms_position) 
        {
            // read new SMS
            Serial.print("SMS postion:");
            Serial.println(sms_position,DEC);
            sms.GetSMS(sms_position, phone_number, sms_text, 100);
            // now we have phone number string in phone_num
            Serial.println(phone_number);
            // and SMS text in sms_text
            Serial.println(sms_text);
        }   
  
        delay(1000);
    }
};

Thank you for the guidance

Hi,
I am interested in activating the SIM808 and facing many issues too.
I see you might have solved this and got it working.
I am trying to activate it with direct AT+ commands, but it is not working properly.

Can you explain please - which libraries should I use -

sms.h
SIM900.h

What is the change needed from GSM.cpp for s/w serial ?

Tnx!