Skip to content

Commit 0389ec5

Browse files
committed
Update the socket ping pong examples
1 parent 2add375 commit 0389ec5

File tree

2 files changed

+676
-93
lines changed

2 files changed

+676
-93
lines changed

examples/SARA-R5_Example10_SocketPingPong/SARA-R5_Example10_SocketPingPong.ino

Lines changed: 109 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
1111
This example demonstrates how to transfer data from one SARA-R5 to another using TCP sockets.
1212
13-
This example includes the code from Example7_ConfigurePacketSwitchedData to let you see the SARA-R5's IP address.
13+
The PDP profile is read from NVM. Please make sure you have run examples 4 & 7 previously to set up the profile.
14+
1415
If you select "Ping":
1516
The code asks for the IP Address of the "Pong" SARA-R5
1617
The code then opens a TCP socket to the "Pong" SARA-R5 using port number TCP_PORT
@@ -85,6 +86,12 @@ SARA_R5 mySARA;
8586
// Change the pin number if required.
8687
//SARA_R5 mySARA(34);
8788

89+
// Create a SARA_R5 object to use throughout the sketch
90+
// If you are using the LTE GNSS Breakout, and have access to the SARA's RESET_N pin, you can pass that to the library too
91+
// allowing it to do an emergency shutdown if required.
92+
// Change the pin numbers if required.
93+
//SARA_R5 mySARA(34, 35); // PWR_ON, RESET_N
94+
8895
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
8996

9097
unsigned int TCP_PORT = 1200; // Change this if required
@@ -212,7 +219,7 @@ void setup()
212219

213220
// Wait for user to press key to begin
214221
Serial.println(F("SARA-R5 Example"));
215-
Serial.println(F("Press any key to begin"));
222+
Serial.println(F("Wait until the SARA's NI LED lights up - then press any key to begin"));
216223

217224
while (!Serial.available()) // Wait for the user to press a key (send any serial character)
218225
;
@@ -253,86 +260,22 @@ void setup()
253260
; // Do nothing more
254261
}
255262

256-
int minCID = SARA_R5_NUM_PDP_CONTEXT_IDENTIFIERS; // Keep a record of the highest and lowest CIDs
257-
int maxCID = 0;
258-
259-
Serial.println(F("The available Context IDs are:"));
260-
Serial.println(F("Context ID:\tAPN Name:\tIP Address:"));
261-
for (int cid = 0; cid < SARA_R5_NUM_PDP_CONTEXT_IDENTIFIERS; cid++)
262-
{
263-
String apn = "";
264-
IPAddress ip(0, 0, 0, 0);
265-
mySARA.getAPN(cid, &apn, &ip);
266-
if (apn.length() > 0)
267-
{
268-
Serial.print(cid);
269-
Serial.print(F("\t"));
270-
Serial.print(apn);
271-
Serial.print(F("\t"));
272-
Serial.println(ip);
273-
}
274-
if (cid < minCID)
275-
minCID = cid; // Record the lowest CID
276-
if (cid > maxCID)
277-
maxCID = cid; // Record the highest CID
278-
}
279-
Serial.println();
280-
281-
Serial.println(F("Which Context ID do you want to use for your Packet Switched Data connection?"));
282-
Serial.println(F("Please enter the number (followed by LF / Newline): "));
283-
284-
char c = 0;
285-
bool selected = false;
286-
int selection = 0;
287-
while (!selected)
288-
{
289-
while (!Serial.available()) ; // Wait for a character to arrive
290-
c = Serial.read(); // Read it
291-
if (c == '\n') // Is it a LF?
292-
{
293-
if ((selection >= minCID) && (selection <= maxCID))
294-
{
295-
selected = true;
296-
Serial.println("Using CID: " + String(selection));
297-
}
298-
else
299-
{
300-
Serial.println(F("Invalid CID. Please try again:"));
301-
selection = 0;
302-
}
303-
}
304-
else
305-
{
306-
selection *= 10; // Multiply selection by 10
307-
selection += c - '0'; // Add the new digit to selection
308-
}
309-
}
310-
311-
// Deactivate the profile
263+
// Deactivate the profile - in case one is already active
312264
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_DEACTIVATE) != SARA_R5_SUCCESS)
313265
{
314266
Serial.println(F("Warning: performPDPaction (deactivate profile) failed. Probably because no profile was active."));
315267
}
316268

317-
// Map PSD profile 0 to the selected CID
318-
if (mySARA.setPDPconfiguration(0, SARA_R5_PSD_CONFIG_PARAM_MAP_TO_CID, selection) != SARA_R5_SUCCESS)
319-
{
320-
Serial.println(F("setPDPconfiguration (map to CID) failed! Freezing..."));
321-
while (1)
322-
; // Do nothing more
323-
}
324-
325-
// Set the protocol type - this needs to match the defined IP type for the CID (as opposed to what was granted by the network)
326-
if (mySARA.setPDPconfiguration(0, SARA_R5_PSD_CONFIG_PARAM_PROTOCOL, SARA_R5_PSD_PROTOCOL_IPV4V6_V4_PREF) != SARA_R5_SUCCESS)
327-
// You _may_ need to change the protocol type: ----------------------------------------^
269+
// Load the profile from NVM - these were saved by a previous example
270+
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_LOAD) != SARA_R5_SUCCESS)
328271
{
329-
Serial.println(F("setPDPconfiguration (set protocol type) failed! Freezing..."));
272+
Serial.println(F("performPDPaction (load from NVM) failed! Freezing..."));
330273
while (1)
331274
; // Do nothing more
332275
}
333276

334-
// Set a callback to process the results of the PSD Action
335-
mySARA.setPSDActionCallback(&processPSDAction);
277+
// Set a callback to process the results of the PSD Action - OPTIONAL
278+
//mySARA.setPSDActionCallback(&processPSDAction);
336279

337280
// Activate the profile
338281
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_ACTIVATE) != SARA_R5_SUCCESS)
@@ -342,19 +285,17 @@ void setup()
342285
; // Do nothing more
343286
}
344287

345-
for (int i = 0; i < 100; i++) // Wait for up to a second for the PSD Action URC to arrive
346-
{
347-
mySARA.bufferedPoll(); // Keep processing data from the SARA so we can process the PSD Action
348-
delay(10);
349-
}
288+
//for (int i = 0; i < 100; i++) // Wait for up to a second for the PSD Action URC to arrive - OPTIONAL
289+
//{
290+
// mySARA.bufferedPoll(); // Keep processing data from the SARA so we can process the PSD Action
291+
// delay(10);
292+
//}
350293

351-
// Save the profile to NVM - so we can load it again in the later examples
352-
if (mySARA.performPDPaction(0, SARA_R5_PSD_ACTION_STORE) != SARA_R5_SUCCESS)
353-
{
354-
Serial.println(F("performPDPaction (save to NVM) failed! Freezing..."));
355-
while (1)
356-
; // Do nothing more
357-
}
294+
//Print the dynamic IP Address (for profile 0)
295+
IPAddress myAddress;
296+
mySARA.getNetworkAssignedIPAddress(0, &myAddress);
297+
Serial.print(F("\r\nMy IP Address is: "));
298+
Serial.println(myAddress.toString());
358299

359300
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
360301

@@ -378,9 +319,9 @@ void setup()
378319
Serial.println(F("2: Pong"));
379320
Serial.println(F("\r\nPlease enter the number (followed by LF / Newline): "));
380321

381-
c = 0;
382-
selected = false;
383-
selection = 0;
322+
char c = 0;
323+
bool selected = false;
324+
int selection = 0;
384325
while (!selected)
385326
{
386327
while (!Serial.available()) ; // Wait for a character to arrive
@@ -401,7 +342,7 @@ void setup()
401342
selection = 0;
402343
}
403344
}
404-
else
345+
else if ((c >= '0') && (c <= '9'))
405346
{
406347
selection = c - '0'; // Store a single digit
407348
}
@@ -443,17 +384,15 @@ void setup()
443384
field++; // Increment the field
444385
val = 0; // Reset the value
445386
}
446-
else
387+
else if ((c >= '0') && (c <= '9'))
447388
{
448389
val *= 10; // Multiply by 10
449390
val += c - '0'; // Add the digit
450391
}
451392
}
452393

453-
char theAddressString[16];
454-
sprintf(theAddressString, "%d.%d.%d.%d", theAddress[0], theAddress[1], theAddress[2], theAddress[3]);
455394
Serial.print(F("Remote address is "));
456-
Serial.println(theAddressString);
395+
Serial.println(theAddress.toString());
457396

458397
// Open the socket
459398
socketNum = mySARA.socketOpen(SARA_R5_TCP);
@@ -468,7 +407,7 @@ void setup()
468407
Serial.println(socketNum);
469408

470409
// Connect to the remote IP Address
471-
if (mySARA.socketConnect(socketNum, (const char *)theAddressString, TCP_PORT) != SARA_R5_SUCCESS)
410+
if (mySARA.socketConnect(socketNum, theAddress, TCP_PORT) != SARA_R5_SUCCESS)
472411
{
473412
Serial.println(F("socketConnect failed! Freezing..."));
474413
while (1)
@@ -519,6 +458,8 @@ void setup()
519458

520459
}
521460

461+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
462+
522463
void loop()
523464
{
524465
mySARA.bufferedPoll(); // Process the backlog (if any) and any fresh serial data
@@ -527,6 +468,7 @@ void loop()
527468
{
528469
if (pingCount >= pingPongLimit)
529470
{
471+
printSocketParameters(socketNum);
530472
mySARA.socketClose(socketNum); // Close the socket - no more pings will be sent
531473
while (1)
532474
mySARA.bufferedPoll(); // Do nothing more except process any received data
@@ -537,9 +479,83 @@ void loop()
537479
{
538480
if (millis() > (startTime + timeLimit))
539481
{
482+
printSocketParameters(socketNum);
540483
mySARA.socketClose(socketNum); // Close the socket - no more pongs will be sent
541484
while (1)
542485
mySARA.bufferedPoll(); // Do nothing more except process any received data
543486
}
544487
}
545488
}
489+
490+
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
491+
492+
// Print the socket parameters
493+
// Note: the socket must be open. ERRORs will be returned if the socket is closed.
494+
void printSocketParameters(int socket)
495+
{
496+
Serial.println(F("Socket parameters:"));
497+
498+
Serial.print(F("Socket type: "));
499+
int socketType;
500+
mySARA.querySocketType(socket, &socketType);
501+
if (socketType == SARA_R5_TCP)
502+
Serial.println(F("TCP"));
503+
else if (socketType == SARA_R5_UDP)
504+
Serial.println(F("UDP"));
505+
else
506+
Serial.println(F("UNKNOWN! (Error!)"));
507+
508+
Serial.print(F("Last error: "));
509+
int lastError;
510+
mySARA.querySocketLastError(socket, &lastError);
511+
Serial.println(lastError);
512+
513+
Serial.print(F("Total bytes sent: "));
514+
uint32_t bytesSent;
515+
mySARA.querySocketTotalBytesSent(socket, &bytesSent);
516+
Serial.println(bytesSent);
517+
518+
Serial.print(F("Total bytes received: "));
519+
uint32_t bytesReceived;
520+
mySARA.querySocketTotalBytesReceived(socket, &bytesReceived);
521+
Serial.println(bytesReceived);
522+
523+
Serial.print(F("Remote IP Address: "));
524+
IPAddress remoteAddress;
525+
int remotePort;
526+
mySARA.querySocketRemoteIPAddress(socket, &remoteAddress, &remotePort);
527+
Serial.println(remoteAddress.toString());
528+
529+
Serial.print(F("Socket status (TCP sockets only): "));
530+
int socketStatus;
531+
mySARA.querySocketStatusTCP(socket, &socketStatus);
532+
if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_INACTIVE)
533+
Serial.println(F("INACTIVE"));
534+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_LISTEN)
535+
Serial.println(F("LISTEN"));
536+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_SYN_SENT)
537+
Serial.println(F("SYN_SENT"));
538+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_SYN_RCVD)
539+
Serial.println(F("SYN_RCVD"));
540+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_ESTABLISHED)
541+
Serial.println(F("ESTABLISHED"));
542+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_FIN_WAIT_1)
543+
Serial.println(F("FIN_WAIT_1"));
544+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_FIN_WAIT_2)
545+
Serial.println(F("FIN_WAIT_2"));
546+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_CLOSE_WAIT)
547+
Serial.println(F("CLOSE_WAIT"));
548+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_CLOSING)
549+
Serial.println(F("CLOSING"));
550+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_LAST_ACK)
551+
Serial.println(F("LAST_ACK"));
552+
else if (socketStatus == SARA_R5::TCP_SOCKET_STATUS_TIME_WAIT)
553+
Serial.println(F("TIME_WAIT"));
554+
else
555+
Serial.println(F("UNKNOWN! (Error!)"));
556+
557+
Serial.print(F("Unacknowledged outgoing bytes: "));
558+
uint32_t bytesUnack;
559+
mySARA.querySocketOutUnackData(socket, &bytesUnack);
560+
Serial.println(bytesUnack);
561+
}

0 commit comments

Comments
 (0)