10
10
11
11
This example demonstrates how to transfer data from one SARA-R5 to another using TCP sockets.
12
12
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
+
14
15
If you select "Ping":
15
16
The code asks for the IP Address of the "Pong" SARA-R5
16
17
The code then opens a TCP socket to the "Pong" SARA-R5 using port number TCP_PORT
@@ -85,6 +86,12 @@ SARA_R5 mySARA;
85
86
// Change the pin number if required.
86
87
// SARA_R5 mySARA(34);
87
88
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
+
88
95
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
89
96
90
97
unsigned int TCP_PORT = 1200 ; // Change this if required
@@ -212,7 +219,7 @@ void setup()
212
219
213
220
// Wait for user to press key to begin
214
221
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" ));
216
223
217
224
while (!Serial.available ()) // Wait for the user to press a key (send any serial character)
218
225
;
@@ -253,86 +260,22 @@ void setup()
253
260
; // Do nothing more
254
261
}
255
262
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:\t APN Name:\t IP 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
312
264
if (mySARA.performPDPaction (0 , SARA_R5_PSD_ACTION_DEACTIVATE) != SARA_R5_SUCCESS)
313
265
{
314
266
Serial.println (F (" Warning: performPDPaction (deactivate profile) failed. Probably because no profile was active." ));
315
267
}
316
268
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)
328
271
{
329
- Serial.println (F (" setPDPconfiguration (set protocol type ) failed! Freezing..." ));
272
+ Serial.println (F (" performPDPaction (load from NVM ) failed! Freezing..." ));
330
273
while (1 )
331
274
; // Do nothing more
332
275
}
333
276
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);
336
279
337
280
// Activate the profile
338
281
if (mySARA.performPDPaction (0 , SARA_R5_PSD_ACTION_ACTIVATE) != SARA_R5_SUCCESS)
@@ -342,19 +285,17 @@ void setup()
342
285
; // Do nothing more
343
286
}
344
287
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
+ // }
350
293
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\n My IP Address is: " ));
298
+ Serial.println (myAddress.toString ());
358
299
359
300
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
360
301
@@ -378,9 +319,9 @@ void setup()
378
319
Serial.println (F (" 2: Pong" ));
379
320
Serial.println (F (" \r\n Please enter the number (followed by LF / Newline): " ));
380
321
381
- c = 0 ;
382
- selected = false ;
383
- selection = 0 ;
322
+ char c = 0 ;
323
+ bool selected = false ;
324
+ int selection = 0 ;
384
325
while (!selected)
385
326
{
386
327
while (!Serial.available ()) ; // Wait for a character to arrive
@@ -401,7 +342,7 @@ void setup()
401
342
selection = 0 ;
402
343
}
403
344
}
404
- else
345
+ else if ((c >= ' 0 ' ) && (c <= ' 9 ' ))
405
346
{
406
347
selection = c - ' 0' ; // Store a single digit
407
348
}
@@ -443,17 +384,15 @@ void setup()
443
384
field++; // Increment the field
444
385
val = 0 ; // Reset the value
445
386
}
446
- else
387
+ else if ((c >= ' 0 ' ) && (c <= ' 9 ' ))
447
388
{
448
389
val *= 10 ; // Multiply by 10
449
390
val += c - ' 0' ; // Add the digit
450
391
}
451
392
}
452
393
453
- char theAddressString[16 ];
454
- sprintf (theAddressString, " %d.%d.%d.%d" , theAddress[0 ], theAddress[1 ], theAddress[2 ], theAddress[3 ]);
455
394
Serial.print (F (" Remote address is " ));
456
- Serial.println (theAddressString );
395
+ Serial.println (theAddress. toString () );
457
396
458
397
// Open the socket
459
398
socketNum = mySARA.socketOpen (SARA_R5_TCP);
@@ -468,7 +407,7 @@ void setup()
468
407
Serial.println (socketNum);
469
408
470
409
// 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)
472
411
{
473
412
Serial.println (F (" socketConnect failed! Freezing..." ));
474
413
while (1 )
@@ -519,6 +458,8 @@ void setup()
519
458
520
459
}
521
460
461
+ // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
462
+
522
463
void loop ()
523
464
{
524
465
mySARA.bufferedPoll (); // Process the backlog (if any) and any fresh serial data
@@ -527,6 +468,7 @@ void loop()
527
468
{
528
469
if (pingCount >= pingPongLimit)
529
470
{
471
+ printSocketParameters (socketNum);
530
472
mySARA.socketClose (socketNum); // Close the socket - no more pings will be sent
531
473
while (1 )
532
474
mySARA.bufferedPoll (); // Do nothing more except process any received data
@@ -537,9 +479,83 @@ void loop()
537
479
{
538
480
if (millis () > (startTime + timeLimit))
539
481
{
482
+ printSocketParameters (socketNum);
540
483
mySARA.socketClose (socketNum); // Close the socket - no more pongs will be sent
541
484
while (1 )
542
485
mySARA.bufferedPoll (); // Do nothing more except process any received data
543
486
}
544
487
}
545
488
}
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