Skip to content

Commit e31595c

Browse files
committed
Update SARA-R5_Example10_SocketPingPong.ino
1 parent e7aedc6 commit e31595c

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

examples/SARA-R5_Example10_SocketPingPong/SARA-R5_Example10_SocketPingPong.ino

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,50 @@
1212
1313
This example includes the code from Example7_ConfigurePacketSwitchedData to let you see the SARA-R5's IP address.
1414
If you select "Ping":
15-
The code asks for the IP Address of the SARA-R5 you want to communicate with
15+
The code asks for the IP Address of the "Pong" SARA-R5
1616
The code then opens a TCP socket to the "Pong" SARA-R5 using port number TCP_PORT
1717
The code sends an initial "Ping" using Write Socket Data (+USOWR)
1818
The code polls continuously. When a +UUSORD URC message is received, data is read and passed to the socketReadCallback.
1919
When "Pong" is received by the callback, the code sends "Ping" in reply
2020
The Ping-Pong repeats 100 times
21-
The socket is closed after the 100th ping is sent
21+
The socket is closed after the 100th Ping is sent
2222
If you select "Pong":
23-
The code opens a TCP socket and waits for data to arrive
23+
The code opens a TCP socket and waits for a connection and for data to arrive
2424
The code polls continuously. When a +UUSORD URC message is received, data is read and passed to the socketReadCallback.
2525
When "Ping" is received by the callback, the code sends "Pong" in reply
26-
The socket is closed after 600 seconds
26+
The socket is closed after 120 seconds
2727
Start the "Pong" first!
2828
29+
You may find that your service provider is blocking incoming TCP connections to the SARA-R5, preventing the "Pong" from working...
30+
If that is the case, you can use this code to play ping-pong with another computer acting as a TCP Echo Server.
31+
Here's a quick how-to (assuming you are familiar with Python):
32+
Open up a Python editor on your computer
33+
Grab yourself some simple TCP Echo Server code:
34+
The third example here works well: https://rosettacode.org/wiki/Echo_server#Python
35+
Log in to your router
36+
Find your local IP address (usually 192.168.0.something)
37+
Go into your router's Security / Port Forwarding settings:
38+
Create a new port forwarding rule
39+
The IP address is your local IP address
40+
Set the local port range to 1200-1200 (if you changed TCP_PORT, use that port number instead)
41+
Set the external port range to 1200-1200
42+
Set the protocol to TCP
43+
Enable the rule
44+
This will open up a direct connection from the outside world, through your router, to port 1200 on your computer
45+
Remember to lock it down again when you're done!
46+
Edit the Python code and replace 'localhost' with your local IP number:
47+
HOST = '192.168.0.nnn'
48+
Change the PORT to 1200:
49+
PORT = 1200
50+
Run the Python code
51+
Ask Google for your computer's public IP address:
52+
Google "what is my IP address"
53+
Run this code and choose the "Ping" option
54+
Enter your computer's public IP address when asked
55+
Sit back and watch the ping-pong!
56+
The code will stop after 100 Pings+Echos and 100 Pongs+Echos
57+
That's 400 TCP transfers in total!
58+
2959
Feel like supporting open source hardware?
3060
Buy a board from SparkFun!
3161
@@ -68,7 +98,7 @@ const int pingPongLimit = 100;
6898

6999
// Keep track of how long the socket has been open. "Pong" closes the socket when timeLimit (millis) is reached.
70100
unsigned long startTime;
71-
const unsigned long timeLimit = 600000; // 600 seconds
101+
const unsigned long timeLimit = 120000; // 120 seconds
72102

73103
#include <IPAddress.h> // Needed for sockets
74104
volatile int socketNum;
@@ -189,7 +219,7 @@ void setup()
189219
while (Serial.available()) // Empty the serial RX buffer
190220
Serial.read();
191221

192-
mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
222+
//mySARA.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
193223

194224
// For the MicroMod Asset Tracker, we need to invert the power pin so it pulls high instead of low
195225
// Comment the next line if required
@@ -397,11 +427,7 @@ void setup()
397427
if (c == '\n') // Is it a LF?
398428
{
399429
theAddress[field] = val; // Store the current value
400-
if ((field == 3)
401-
&& (theAddress[0] >= 0) && (theAddress[0] <= 255)
402-
&& (theAddress[1] >= 0) && (theAddress[1] <= 255)
403-
&& (theAddress[2] >= 0) && (theAddress[2] <= 255)
404-
&& (theAddress[3] >= 0) && (theAddress[3] <= 255))
430+
if (field == 3)
405431
selected = true;
406432
else
407433
{

0 commit comments

Comments
 (0)