Skip to content

Commit dc56f10

Browse files
committed
Add SMS examples
1 parent 318b305 commit dc56f10

File tree

2 files changed

+385
-0
lines changed

2 files changed

+385
-0
lines changed
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
#include "SparkFun_u-blox_Cellular_Arduino_Library.h"
2+
3+
// Uncomment the line below that you need for Serial on your platform
4+
#define mySerial Serial1
5+
// SoftwareSerial mySerial(16, 17);
6+
7+
// Uncomment the module you're using. If your module is not listed below, then
8+
// it's not supported for this example
9+
UBX_CELL myModule; // This example works with all modules, so the base class can be used
10+
// UBX_CELL myModule; // Base SARA-R5 class
11+
// UBX_CELL00S myModule;
12+
// UBX_CELL00S_01B myModule;
13+
// UBX_CELL00S_61B myModule;
14+
// UBX_CELL10M8S_61B myModule;
15+
// UBX_CELL10S myModule;
16+
// LARA_R6 myModule; // Base LARA-R6 class
17+
// LARA_R6001 myModule;
18+
// LARA_R6001D myModule;
19+
// LARA_R6401 myModule;
20+
// LARA_R6401D myModule;
21+
// LARA_R6801_00B myModule;
22+
// LARA_R6801D myModule;
23+
24+
void setup()
25+
{
26+
String currentOperator = "";
27+
28+
Serial.begin(115200); // Start the serial console
29+
30+
// Wait for user to press key to begin
31+
Serial.println(F("u-blox Cellular Example 5 - Receive SMS"));
32+
Serial.println(F("Press any key to begin"));
33+
34+
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
35+
;
36+
while (Serial.available()) // Empty the serial RX buffer
37+
Serial.read();
38+
39+
Serial.println(F("Beginning..."));
40+
41+
// myModule.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
42+
43+
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
44+
// Uncomment the next line if required
45+
// myModule.invertPowerPin(true);
46+
47+
// Initialize the module
48+
if (myModule.begin(mySerial, UBX_CELL_DEFAULT_BAUD_RATE) )
49+
{
50+
Serial.println(F("Module connected!"));
51+
}
52+
else
53+
{
54+
Serial.println(F("Unable to communicate with the module."));
55+
Serial.println(F("Manually power-on (hold the module's On button for 3 seconds) and try again."));
56+
while (1) ; // Loop forever on fail
57+
}
58+
Serial.println();
59+
60+
// First check to see if we're connected to an operator:
61+
if (myModule.getOperator(&currentOperator) == UBX_CELL_SUCCESS)
62+
{
63+
Serial.print(F("Connected to: "));
64+
Serial.println(currentOperator);
65+
}
66+
else
67+
{
68+
Serial.print(F("The SARA is not yet connected to an operator. Please use the previous examples to connect. Or wait and retry. Freezing..."));
69+
while (1)
70+
; // Do nothing more
71+
}
72+
73+
while (Serial.available()) // Empty the serial RX buffer
74+
Serial.read();
75+
}
76+
77+
void loop()
78+
{
79+
static bool printReadMessages = true; // Print all messages once. Then only print new messages. Unless a message is deleted.
80+
static int previousUsed = -1; // Store the previous number of used memory locations
81+
82+
// Read the number of used and total messages
83+
int used;
84+
int total;
85+
if (myModule.getPreferredMessageStorage(&used, &total) != UBX_CELL_SUCCESS)
86+
{
87+
Serial.println(F("An error occurred when trying to read ME memory!"));
88+
}
89+
else
90+
{
91+
if ((used != previousUsed) || printReadMessages) // Has a new message arrived? Or was the delete menu opened?
92+
{
93+
Serial.print(F("\r\nNumber of used memory locations: "));
94+
Serial.println(used);
95+
Serial.print(F("Total number of memory locations: "));
96+
Serial.println(total);
97+
Serial.println();
98+
99+
int memoryLocation = 0;
100+
int foundMessages = 0;
101+
// Keep reading until we find all the messages or we reach the end of the memory
102+
while ((foundMessages < used) && (memoryLocation <= total))
103+
{
104+
String unread = "";
105+
String from = "";
106+
String dateTime = "";
107+
String message = "";
108+
// Read the message from this location. Reading from empty message locations returns an ERROR
109+
// unread can be: "REC UNREAD", "REC READ", "STO UNSENT", "STO SENT"
110+
// If the location is empty, readSMSmessage will return a UBX_CELL_ERROR_UNEXPECTED_RESPONSE
111+
if (myModule.readSMSmessage(memoryLocation, &unread, &from, &dateTime, &message) == UBX_CELL_SUCCESS)
112+
{
113+
if (printReadMessages || (unread == "REC UNREAD"))
114+
{
115+
Serial.print(F("Message location: "));
116+
Serial.println(memoryLocation);
117+
Serial.print(F("Status: "));
118+
Serial.println(unread);
119+
Serial.print(F("Originator: "));
120+
Serial.println(from);
121+
Serial.print(F("Date and time: "));
122+
Serial.println(dateTime);
123+
Serial.println(message);
124+
Serial.println();
125+
}
126+
foundMessages++; // We found a message
127+
}
128+
memoryLocation++; // Move on to the next memory location
129+
}
130+
131+
printReadMessages = false;
132+
previousUsed = used; // Update previousUsed
133+
134+
Serial.println(F("Waiting for a new message..."));
135+
Serial.println();
136+
Serial.println(F("Hit any key to delete a message..."));
137+
Serial.println();
138+
}
139+
}
140+
141+
int delayCount = 0;
142+
while (delayCount < 5000)
143+
{
144+
delay(1); // Delay for five seconds, unless the user presses a key
145+
delayCount++;
146+
147+
if (Serial.available())
148+
{
149+
Serial.println(F("To delete a single message: enter its location followed by LF / Newline"));
150+
Serial.println(F("To delete all read messages: enter r followed by LF / Newline"));
151+
Serial.println(F("To delete all read and sent messages: enter s followed by LF / Newline"));
152+
Serial.println(F("To delete all read, sent and unsent messages: enter u followed by LF / Newline"));
153+
Serial.println(F("To delete all messages, including unread messages: enter a followed by LF / Newline"));
154+
Serial.println(F("To exit: enter LF / Newline"));
155+
156+
Serial.read(); // Read and discard the char that opened the menu
157+
158+
int location = 0;
159+
bool selected = false;
160+
while (!selected)
161+
{
162+
while (!Serial.available()) ; // Wait for a character to arrive
163+
char c = Serial.read(); // Read it
164+
if (c == '\n') // Is it a LF?
165+
{
166+
if ((location >= 1) && (location <= total)) // Delete a single message at location
167+
{
168+
if (myModule.deleteSMSmessage(location) == UBX_CELL_SUCCESS)
169+
{
170+
Serial.println(F("\r\nMessage deleted!\r\n"));
171+
printReadMessages = true;
172+
}
173+
else
174+
{
175+
Serial.println(F("\r\nMessage not deleted!\r\n"));
176+
}
177+
}
178+
else if (location == 1001) // r
179+
{
180+
if (myModule.deleteReadSMSmessages() == UBX_CELL_SUCCESS)
181+
{
182+
Serial.println(F("\r\nRead messages deleted!\r\n"));
183+
printReadMessages = true;
184+
}
185+
else
186+
{
187+
Serial.println(F("\r\nMessages not deleted!\r\n"));
188+
}
189+
}
190+
else if (location == 1002) // s
191+
{
192+
if (myModule.deleteReadSentSMSmessages() == UBX_CELL_SUCCESS)
193+
{
194+
Serial.println(F("\r\nRead and sent messages deleted!\r\n"));
195+
printReadMessages = true;
196+
}
197+
else
198+
{
199+
Serial.println(F("\r\nMessages not deleted!\r\n"));
200+
}
201+
}
202+
else if (location == 1003) // u
203+
{
204+
if (myModule.deleteReadSentUnsentSMSmessages() == UBX_CELL_SUCCESS)
205+
{
206+
Serial.println(F("\r\nRead, sent and unsent messages deleted!\r\n"));
207+
printReadMessages = true;
208+
}
209+
else
210+
{
211+
Serial.println(F("\r\nMessages not deleted!\r\n"));
212+
}
213+
}
214+
else if (location == 1004) // a
215+
{
216+
if (myModule.deleteAllSMSmessages() == UBX_CELL_SUCCESS)
217+
{
218+
Serial.println(F("\r\nAll messages deleted!\r\n"));
219+
printReadMessages = true;
220+
}
221+
else
222+
{
223+
Serial.println(F("\r\nMessages not deleted!\r\n"));
224+
}
225+
}
226+
else
227+
Serial.println(F("\r\nExit...\r\n"));
228+
selected = true;
229+
}
230+
else if ((c >= '0') && (c <= '9'))
231+
{
232+
location *= 10; // Multiply by 10
233+
location += c - '0'; // Add the digit
234+
}
235+
else if (c == 'r')
236+
{
237+
location = 1001;
238+
}
239+
else if (c == 's')
240+
{
241+
location = 1002;
242+
}
243+
else if (c == 'u')
244+
{
245+
location = 1003;
246+
}
247+
else if (c == 'a')
248+
{
249+
location = 1004;
250+
}
251+
}
252+
253+
delayCount = 5000;
254+
}
255+
}
256+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include "SparkFun_u-blox_Cellular_Arduino_Library.h"
2+
3+
// Uncomment the line below that you need for Serial on your platform
4+
#define mySerial Serial1
5+
// SoftwareSerial mySerial(16, 17);
6+
7+
// Uncomment the module you're using. If your module is not listed below, then
8+
// it's not supported for this example
9+
UBX_CELL myModule; // This example works with all modules, so the base class can be used
10+
// UBX_CELL myModule; // Base SARA-R5 class
11+
// UBX_CELL00S myModule;
12+
// UBX_CELL00S_01B myModule;
13+
// UBX_CELL00S_61B myModule;
14+
// UBX_CELL10M8S_61B myModule;
15+
// UBX_CELL10S myModule;
16+
// LARA_R6 myModule; // Base LARA-R6 class
17+
// LARA_R6001 myModule;
18+
// LARA_R6001D myModule;
19+
// LARA_R6401 myModule;
20+
// LARA_R6401D myModule;
21+
// LARA_R6801_00B myModule;
22+
// LARA_R6801D myModule;
23+
24+
void setup()
25+
{
26+
String currentOperator = "";
27+
28+
Serial.begin(115200); // Start the serial console
29+
30+
// Wait for user to press key to begin
31+
Serial.println(F("u-blox Cellular Example 6 - Send SMS"));
32+
Serial.println(F("Press any key to begin"));
33+
34+
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
35+
;
36+
while (Serial.available()) // Empty the serial RX buffer
37+
Serial.read();
38+
39+
Serial.println(F("Beginning..."));
40+
41+
// myModule.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
42+
43+
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
44+
// Uncomment the next line if required
45+
// myModule.invertPowerPin(true);
46+
47+
// Initialize the module
48+
if (myModule.begin(mySerial, UBX_CELL_DEFAULT_BAUD_RATE) )
49+
{
50+
Serial.println(F("Module connected!"));
51+
}
52+
else
53+
{
54+
Serial.println(F("Unable to communicate with the module."));
55+
Serial.println(F("Manually power-on (hold the module's On button for 3 seconds) and try again."));
56+
while (1) ; // Loop forever on fail
57+
}
58+
Serial.println();
59+
60+
// First check to see if we're connected to an operator:
61+
if (myModule.getOperator(&currentOperator) == UBX_CELL_SUCCESS)
62+
{
63+
Serial.print(F("Connected to: "));
64+
Serial.println(currentOperator);
65+
}
66+
else
67+
{
68+
Serial.print(F("The SARA is not yet connected to an operator. Please use the previous examples to connect. Or wait and retry. Freezing..."));
69+
while (1)
70+
; // Do nothing more
71+
}
72+
73+
Serial.println();
74+
Serial.println(F("*** Set the Serial Monitor line ending to Newline ***"));
75+
}
76+
77+
void loop()
78+
{
79+
String destinationNumber = "";
80+
String message = "";
81+
boolean keepGoing = true;
82+
83+
Serial.println();
84+
Serial.println(F("Enter the destination number (followed by LF / Newline): "));
85+
86+
while (keepGoing)
87+
{
88+
if (Serial.available())
89+
{
90+
char c = Serial.read();
91+
if (c == '\n')
92+
{
93+
keepGoing = false; // Stop if we receive a newline
94+
}
95+
else
96+
{
97+
destinationNumber += c; // Add serial characters to the destination number
98+
}
99+
}
100+
}
101+
102+
keepGoing = true;
103+
Serial.println();
104+
Serial.println(F("Enter the message (followed by LF): "));
105+
106+
while (keepGoing)
107+
{
108+
if (Serial.available())
109+
{
110+
char c = Serial.read();
111+
if (c == '\n')
112+
{
113+
keepGoing = false; // Stop if we receive a newline
114+
}
115+
else
116+
{
117+
message += c; // Add serial characters to the destination number
118+
}
119+
}
120+
}
121+
122+
// Once we receive a newline, send the text.
123+
Serial.println("Sending: \"" + message + "\" to " + destinationNumber);
124+
// Call myModule.sendSMS(String number, String message) to send an SMS message.
125+
if (myModule.sendSMS(destinationNumber, message) == UBX_CELL_SUCCESS)
126+
{
127+
Serial.println(F("sendSMS was successful"));
128+
}
129+
}

0 commit comments

Comments
 (0)