Skip to content

Commit 3f0e6f2

Browse files
committed
Add deleteSMSmessage etc.. Update Example5
1 parent 339e90a commit 3f0e6f2

File tree

4 files changed

+162
-8
lines changed

4 files changed

+162
-8
lines changed

examples/SARA-R5_Example5_ReceiveSMS/SARA-R5_Example5_ReceiveSMS.ino

Lines changed: 130 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4442
void 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

9293
void 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\nNumber 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\nMessage deleted!\r\n"));
187+
printReadMessages = true;
188+
}
189+
else
190+
{
191+
Serial.println(F("\r\nMessage 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\nRead messages deleted!\r\n"));
199+
printReadMessages = true;
200+
}
201+
else
202+
{
203+
Serial.println(F("\r\nMessages 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\nRead and sent messages deleted!\r\n"));
211+
printReadMessages = true;
212+
}
213+
else
214+
{
215+
Serial.println(F("\r\nMessages 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\nRead, sent and unsent messages deleted!\r\n"));
223+
printReadMessages = true;
224+
}
225+
else
226+
{
227+
Serial.println(F("\r\nMessages 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\nAll messages deleted!\r\n"));
235+
printReadMessages = true;
236+
}
237+
else
238+
{
239+
Serial.println(F("\r\nMessages not deleted!\r\n"));
240+
}
241+
}
242+
else
243+
Serial.println(F("\r\nExit...\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
}

keywords.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ setSMSMessageFormat KEYWORD2
9797
sendSMS KEYWORD2
9898
getPreferredMessageStorage KEYWORD2
9999
readSMSmessage KEYWORD2
100+
deleteSMSmessage KEYWORD2
101+
deleteReadSMSmessages KEYWORD2
102+
deleteReadSentSMSmessages KEYWORD2
103+
deleteReadSentUnsentSMSmessages KEYWORD2
104+
deleteAllSMSmessages KEYWORD2
100105
setBaud KEYWORD2
101106
setFlowControl KEYWORD2
102107
setGpioMode KEYWORD2

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ SARA_R5_error_t SARA_R5::getPreferredMessageStorage(int *used, int *total, Strin
18861886
{
18871887
if (_printDebug == true)
18881888
{
1889-
_debugPort->print(F("getPreferredMessageStorage: memory: "));
1889+
_debugPort->print(F("getPreferredMessageStorage: memory1 (read and delete): "));
18901890
_debugPort->print(memory);
18911891
_debugPort->print(F(" used: "));
18921892
_debugPort->print(u);
@@ -2010,6 +2010,25 @@ SARA_R5_error_t SARA_R5::readSMSmessage(int location, String *unread, String *fr
20102010
return err;
20112011
}
20122012

2013+
SARA_R5_error_t SARA_R5::deleteSMSmessage(int location, int deleteFlag)
2014+
{
2015+
char *command;
2016+
SARA_R5_error_t err;
2017+
2018+
command = sara_r5_calloc_char(strlen(SARA_R5_DELETE_MESSAGE) + 12);
2019+
if (command == NULL)
2020+
return SARA_R5_ERROR_OUT_OF_MEMORY;
2021+
if (deleteFlag == 0)
2022+
sprintf(command, "%s=%d", SARA_R5_DELETE_MESSAGE, location);
2023+
else
2024+
sprintf(command, "%s=%d,%d", SARA_R5_DELETE_MESSAGE, location, deleteFlag);
2025+
2026+
err = sendCommandWithResponse(command, SARA_R5_RESPONSE_OK, NULL, SARA_R5_55_SECS_TIMEOUT);
2027+
2028+
free(command);
2029+
return err;
2030+
}
2031+
20132032
SARA_R5_error_t SARA_R5::setBaud(unsigned long baud)
20142033
{
20152034
SARA_R5_error_t err;

src/SparkFun_u-blox_SARA-R5_Arduino_Library.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
// Timing
7979
#define SARA_R5_STANDARD_RESPONSE_TIMEOUT 1000
8080
#define SARA_R5_10_SEC_TIMEOUT 10000
81+
#define SARA_R5_55_SECS_TIMEOUT 55000
8182
#define SARA_R5_2_MIN_TIMEOUT 120000
8283
#define SARA_R5_3_MIN_TIMEOUT 180000
8384
#define SARA_R5_SET_BAUD_TIMEOUT 500
@@ -122,6 +123,7 @@ const char SARA_R5_SEND_TEXT[] = "+CMGS"; // Send SMS message
122123
const char SARA_R5_NEW_MESSAGE_IND[] = "+CNMI"; // New [SMS] message indication
123124
const char SARA_R5_PREF_MESSAGE_STORE[] = "+CPMS"; // Preferred message storage
124125
const char SARA_R5_READ_TEXT_MESSAGE[] = "+CMGR"; // Read message
126+
const char SARA_R5_DELETE_MESSAGE[] = "+CMGD"; // Delete message
125127
// V24 control and V25ter (UART interface)
126128
const char SARA_R5_FLOW_CONTROL[] = "&K"; // Flow control
127129
const char SARA_R5_COMMAND_BAUD[] = "+IPR"; // Baud rate
@@ -599,6 +601,11 @@ class SARA_R5 : public Print
599601
SARA_R5_error_t sendSMS(String number, String message);
600602
SARA_R5_error_t getPreferredMessageStorage(int *used, int *total, String memory = "ME");
601603
SARA_R5_error_t readSMSmessage(int location, String *unread, String *from, String *dateTime, String *message);
604+
SARA_R5_error_t deleteSMSmessage(int location, int deleteFlag = 0); // Default to deleting the single message at the specified location
605+
SARA_R5_error_t deleteReadSMSmessages(void) { return (deleteSMSmessage( 1, 1 )); }; // Delete all the read messages from preferred storage
606+
SARA_R5_error_t deleteReadSentSMSmessages(void) { return (deleteSMSmessage( 1, 2 )); }; // Delete the read and sent messages from preferred storage
607+
SARA_R5_error_t deleteReadSentUnsentSMSmessages(void) { return (deleteSMSmessage( 1, 3 )); }; // Delete the read, sent and unsent messages from preferred storage
608+
SARA_R5_error_t deleteAllSMSmessages(void) { return (deleteSMSmessage( 1, 4 )); }; // Delete the read, sent, unsent and unread messages from preferred storage
602609

603610
// V24 Control and V25ter (UART interface) AT commands
604611
SARA_R5_error_t setBaud(unsigned long baud);

0 commit comments

Comments
 (0)