Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 37c9ce3

Browse files
committedApr 11, 2024
Add option for deleting SMS
1 parent 07bf9d3 commit 37c9ce3

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
 

‎examples/DeleteSMS/DeleteSMS.ino

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "ArduinoCellular.h"
2+
3+
ArduinoCellular cellular = ArduinoCellular();
4+
5+
void printMessages(std::vector<SMS> msg){
6+
for(int i = 0; i < msg.size(); i++){
7+
Serial.println("SMS:");
8+
Serial.print("* Index: "); Serial.println(msg[i].index);
9+
Serial.print("* From: "); Serial.println(msg[i].sender);
10+
Serial.print("* Timestamp: "); Serial.println(msg[i].timestamp.getISO8601());
11+
Serial.println("* Message: "); Serial.println(msg[i].message);
12+
Serial.println("--------------------\n");
13+
}
14+
}
15+
16+
void setup(){
17+
Serial.begin(115200);
18+
while (!Serial);
19+
cellular.begin();
20+
delay(2000); // Give the modem some time to initialize
21+
}
22+
23+
void loop(){
24+
std::vector<SMS> readSMS = cellular.getReadSMS();
25+
if(readSMS.size() > 0){
26+
Serial.println("Read SMS:");
27+
printMessages(readSMS);
28+
}
29+
30+
std::vector<SMS> unreadSMS = cellular.getUnreadSMS();
31+
if(unreadSMS.size() > 0){
32+
Serial.println("Unread SMS:");
33+
printMessages(unreadSMS);
34+
}
35+
36+
// Prompt user which SMS to delete
37+
Serial.println("Enter the index of the SMS you want to delete:");
38+
39+
while(Serial.available() == 0);
40+
auto index = Serial.readStringUntil('\n').toInt();
41+
Serial.println("Deleting SMS...");
42+
43+
if(cellular.deleteSMS(index)){
44+
Serial.println("SMS deleted.");
45+
} else {
46+
Serial.println("Failed to delete SMS.");
47+
}
48+
}

‎src/ArduinoCellular.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,12 @@ std::vector<SMS> ArduinoCellular::getUnreadSMS(){
380380
}
381381
}
382382

383+
bool ArduinoCellular::deleteSMS(uint16_t index){
384+
String command = "+CMGD=" + String(index);
385+
String response = sendATCommand(const_cast<char *>(command.c_str()));
386+
return response.indexOf("OK") != -1;
387+
}
388+
383389
void ArduinoCellular::setDebugStream(Stream &stream){
384390
this->debugStream = &stream;
385391
}

‎src/ArduinoCellular.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,14 @@ class ArduinoCellular {
176176
*/
177177
std::vector<SMS> getUnreadSMS();
178178

179+
/**
180+
* @brief Deletes an SMS message at the specified index.
181+
*
182+
* @param index The index of the SMS message to delete.
183+
* @return True if the SMS message was successfully deleted, false otherwise.
184+
*/
185+
bool deleteSMS(uint16_t index);
186+
179187
/**
180188
* @brief Sends an AT command to the modem and waits for a response, then returns the response.
181189
* @param command The AT command to send.

0 commit comments

Comments
 (0)
Please sign in to comment.