@@ -39,8 +39,6 @@ SARA_R5 mySARA;
3939// Change the pin number if required.
4040// SARA_R5 mySARA(34);
4141
42- int previousUsed = 0 ; // Store the previous number of used memory locations
43-
4442void setup ()
4543{
4644 String currentOperator = " " ;
@@ -87,10 +85,16 @@ void setup()
8785 while (1 )
8886 ; // Do nothing more
8987 }
88+
89+ while (Serial.available ()) // Empty the serial RX buffer
90+ Serial.read ();
9091}
9192
9293void loop ()
9394{
95+ static bool printReadMessages = true ; // Print all messages once. Then only print new messages. Unless a message is deleted.
96+ static int previousUsed = -1 ; // Store the previous number of used memory locations
97+
9498 // Read the number of used and total messages
9599 int used;
96100 int total;
@@ -100,9 +104,9 @@ void loop()
100104 }
101105 else
102106 {
103- if (used > previousUsed) // Has a new message arrived?
107+ if (( used != previousUsed) || printReadMessages) // Has a new message arrived? Or was the delete menu opened ?
104108 {
105- Serial.print (F (" Number of used memory locations: " ));
109+ Serial.print (F (" \r\n Number of used memory locations: " ));
106110 Serial.println (used);
107111 Serial.print (F (" Total number of memory locations: " ));
108112 Serial.println (total);
@@ -118,11 +122,13 @@ void loop()
118122 String dateTime = " " ;
119123 String message = " " ;
120124 // Read the message from this location. Reading from empty message locations returns an ERROR
125+ // unread can be: "REC UNREAD", "REC READ", "STO UNSENT", "STO SENT"
126+ // If the location is empty, readSMSmessage will return a SARA_R5_ERROR_UNEXPECTED_RESPONSE
121127 if (mySARA.readSMSmessage (memoryLocation, &unread, &from, &dateTime, &message) == SARA_R5_SUCCESS)
122128 {
123- if (unread == " REC UNREAD" ) // Only print new (previously-unread) messages. Comment this line to display all messages.
129+ if (printReadMessages || ( unread == " REC UNREAD" ))
124130 {
125- Serial.print (F (" Message index : " ));
131+ Serial.print (F (" Message location : " ));
126132 Serial.println (memoryLocation);
127133 Serial.print (F (" Status: " ));
128134 Serial.println (unread);
@@ -138,12 +144,129 @@ void loop()
138144 memoryLocation++; // Move on to the next memory location
139145 }
140146
147+ printReadMessages = false ;
141148 previousUsed = used; // Update previousUsed
142149
143150 Serial.println (F (" Waiting for a new message..." ));
144151 Serial.println ();
152+ Serial.println (F (" Hit any key to delete a message..." ));
153+ Serial.println ();
145154 }
146155 }
147156
148- delay (5000 ); // Check for new messages every 5 seconds
157+ int delayCount = 0 ;
158+ while (delayCount < 5000 )
159+ {
160+ delay (1 ); // Delay for five seconds, unless the user presses a key
161+ delayCount++;
162+
163+ if (Serial.available ())
164+ {
165+ Serial.println (F (" To delete a single message: enter its location followed by LF / Newline" ));
166+ Serial.println (F (" To delete all read messages: enter r followed by LF / Newline" ));
167+ Serial.println (F (" To delete all read and sent messages: enter s followed by LF / Newline" ));
168+ Serial.println (F (" To delete all read, sent and unsent messages: enter u followed by LF / Newline" ));
169+ Serial.println (F (" To delete all messages, including unread messages: enter a followed by LF / Newline" ));
170+ Serial.println (F (" To exit: enter LF / Newline" ));
171+
172+ Serial.read (); // Read and discard the char that opened the menu
173+
174+ int location = 0 ;
175+ bool selected = false ;
176+ while (!selected)
177+ {
178+ while (!Serial.available ()) ; // Wait for a character to arrive
179+ char c = Serial.read (); // Read it
180+ if (c == ' \n ' ) // Is it a LF?
181+ {
182+ if ((location >= 1 ) && (location <= total)) // Delete a single message at location
183+ {
184+ if (mySARA.deleteSMSmessage (location) == SARA_R5_SUCCESS)
185+ {
186+ Serial.println (F (" \r\n Message deleted!\r\n " ));
187+ printReadMessages = true ;
188+ }
189+ else
190+ {
191+ Serial.println (F (" \r\n Message not deleted!\r\n " ));
192+ }
193+ }
194+ else if (location == 1001 ) // r
195+ {
196+ if (mySARA.deleteReadSMSmessages () == SARA_R5_SUCCESS)
197+ {
198+ Serial.println (F (" \r\n Read messages deleted!\r\n " ));
199+ printReadMessages = true ;
200+ }
201+ else
202+ {
203+ Serial.println (F (" \r\n Messages not deleted!\r\n " ));
204+ }
205+ }
206+ else if (location == 1002 ) // s
207+ {
208+ if (mySARA.deleteReadSentSMSmessages () == SARA_R5_SUCCESS)
209+ {
210+ Serial.println (F (" \r\n Read and sent messages deleted!\r\n " ));
211+ printReadMessages = true ;
212+ }
213+ else
214+ {
215+ Serial.println (F (" \r\n Messages not deleted!\r\n " ));
216+ }
217+ }
218+ else if (location == 1003 ) // u
219+ {
220+ if (mySARA.deleteReadSentUnsentSMSmessages () == SARA_R5_SUCCESS)
221+ {
222+ Serial.println (F (" \r\n Read, sent and unsent messages deleted!\r\n " ));
223+ printReadMessages = true ;
224+ }
225+ else
226+ {
227+ Serial.println (F (" \r\n Messages not deleted!\r\n " ));
228+ }
229+ }
230+ else if (location == 1004 ) // a
231+ {
232+ if (mySARA.deleteAllSMSmessages () == SARA_R5_SUCCESS)
233+ {
234+ Serial.println (F (" \r\n All messages deleted!\r\n " ));
235+ printReadMessages = true ;
236+ }
237+ else
238+ {
239+ Serial.println (F (" \r\n Messages not deleted!\r\n " ));
240+ }
241+ }
242+ else
243+ Serial.println (F (" \r\n Exit...\r\n " ));
244+ selected = true ;
245+ }
246+ else if ((c >= ' 0' ) && (c <= ' 9' ))
247+ {
248+ location *= 10 ; // Multiply by 10
249+ location += c - ' 0' ; // Add the digit
250+ }
251+ else if (c == ' r' )
252+ {
253+ location = 1001 ;
254+ }
255+ else if (c == ' s' )
256+ {
257+ location = 1002 ;
258+ }
259+ else if (c == ' u' )
260+ {
261+ location = 1003 ;
262+ }
263+ else if (c == ' a' )
264+ {
265+ location = 1004 ;
266+ }
267+ }
268+
269+ delayCount = 5000 ;
270+ }
271+ }
149272}
0 commit comments