First let me preface this by stating that I'm not using that library - I don't like it for many reasons I describe below, so take this as exploration ideas.
don't do thisSim800l Sim800l; //to declare the library
The comment is wrong. you do not declare the library, you are creating an instance of the class Sim800l which you name Sim800l. As said before, this is regarded as bad programing practice because it's easy to confuse if you are talking to the class or instance later on (even if the compiler would understand most of the time).
-->**action:**suggest you change that name to Sim800l_GSM_Module
for example and fix the commentSim800l Sim800l_GSM_Module; //to declare an instance variable to use the methods in the library
Similarly the comment for Sim800l.begin(); // initializate the library.
is not about the library but your instance of the class.
-->action: read about object oriented programing, class and object or instances. Change the instance variable name to something meaningful and modify your code accordingly.
What the readSms() function does in the class is to first send AT+CMGF=1
to the module to sets the GSM modem in SMS Text Mode (In Text Mode, SMS messages are represented as readable text) and checks if this leads to an Error (checks if the answer contain ER as in ERROR). If there was no error it sends AT+CMGR=1
(where 1 is your index variable) to read an SMS message at a certain location of the message storage area. It will return something like this
{
[color=blue]+CMGR: "REC READ","+5531985644009",,"16/09/16,10:12:05+32"
SMS text body goes here.
OK[/color]
}
without the red brackets which I added just for readability.
the code in the class then checks if the string "CMGR:" is part of the answer and if it is assumes it's good and returns the full buffer to you.
But if there is an error like there is no SMS at that location, then an error message is returned and this is implementation dependant, the AT spec does not tells you exactly what will be returned.
-->action: I would encourage you to test what answer you get if there is not SMS to see if your code does the right thing.
when you delete the SMS, I noticed that in the library, the code for deleting SMS is written like this:
SIM.print(F("at+cmgda=\"del all\"\n\r"));
some poor implementations of the AT specification do not like non caps letters. I'd be tempted to modify in the library into
SIM.print(F("AT+CMGDA=\"DEL ALL\"\n\r"));
Also at that stage the library assumes the chip is in SMS text mode. if it is in PDU mode what would need to be sent is "Delete all SMS" instead of "DEL ALL". if your unit is in the wrong mode for whatever reason then that command will fail. So either you modify the library to force SMS text mode (send AT+CMGF=1
)
but that might not be your problem; Another challenge I see is timing. the delete SMS command takes 5 seconds to execute for 1 message and up to 25s to delete 50 messages; The library implements in its _readSerial
code a statically configured timeout of 156 seconds waiting for some sign of life on the serial port and when there is, reads it in one go with the readString() method call. I hate this as it leads to disappointment and timeout() etc...
-->action: In your code you should check the value returned by delAllSms(). It will return true if deleting the SMS has worked or false otherwise.
Talking about timing management: I notice you ask to send an SMS right after detecting the word FATURA and with no delay you ask to delete the SMS. depending on how things work with the Software Serial port and your unit, you might want to give enough time for the SMS to be sent out before sending new commands on the Software Serial Port.
In general if you saturate the buffer of the Serial line, then you will loose infos. The receive buffer is small, only 64 bytes - and the SMS info you read is larger. so better read SMS quickly when they arrive...
-->action: be mindful of timing. Don't create delays() just for the sake of it.
Last but not least - and I know it's not directly your choice because the library drives you there, but don't use Strings with a capital S ([b]String textSms, numberSms;[/b])
because on processor with very limited HEAP size and no dynamic memory management, you are fragmenting your Heap which might lead to situation where memory is no longer available after a while. if you plan to have this code running without reset for a long time, then it's a best practice to get rid of all the Strings.
-->action: rewrite the Sim800l library without using Strings at all. rewrite your code appropriately. You will learn a lot that way.
When I code I don't use that library. if you read the library you can see it's just about sending to the serial line AT commands and then reading what's coming back at you.
-->action: I would encourage you to read the great article about Serial Input Basics by Robin and use that directly into your code. this way you'll be 100% in control of what's going on.
You will find the version 1.10 of the AT command set here which has been updated on 2016-05-18. make sure your firmware is also recent on your unit obviously.
there is also a good read here
hope this helps.