Skip to content

Commit 82b48b3

Browse files
author
Cruz Monrreal
authored
Merge pull request #7 from ithinuel/cellular_tcp_example
Move example code from the doc
2 parents 72427b3 + 1f4ad77 commit 82b48b3

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

CellularTCP/main.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "mbed.h"
2+
#include "UDPSocket.h"
3+
#include "OnboardCellularInterface.h"
4+
5+
// SIM pin code goes here
6+
#define PIN_CODE "1234"
7+
8+
// Network credentials like APN go here, e.g.,
9+
// "apn, username, password"
10+
#define CREDENTIALS "internet"
11+
12+
// Number of retries /
13+
#define RETRY_COUNT 3
14+
15+
// Cellular network interface object
16+
OnboardCellularInterface iface;
17+
18+
// Echo server hostname
19+
const char *host_name = "echo.u-blox.com";
20+
21+
// Echo server TCP port
22+
const int port = 7;
23+
24+
/**
25+
* Connects to the Cellular Network
26+
*/
27+
nsapi_error_t do_connect()
28+
{
29+
nsapi_error_t retcode;
30+
uint8_t retry_counter = 0;
31+
32+
while (!iface.is_connected()) {
33+
retcode = iface.connect();
34+
if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
35+
printf("\n\nAuthentication Failure. Exiting application\n");
36+
break;
37+
} else if (retcode == NSAPI_ERROR_OK) {
38+
printf("\n\nConnection Established.\n");
39+
break;
40+
} else if (retry_counter > RETRY_COUNT) {
41+
printf("\n\nFatal connection failure: %d\n", retcode);
42+
break;
43+
}
44+
45+
printf("\n\nCouldn't connect: %d, will retry\n", retcode);
46+
retry_counter++;
47+
}
48+
49+
return retcode;
50+
}
51+
52+
/**
53+
* Opens a TCP socket with the given echo server and undergoes an echo
54+
* transaction.
55+
*/
56+
int test_send_recv()
57+
{
58+
nsapi_size_or_error_t retcode;
59+
60+
TCPSocket sock;
61+
62+
// Prepare the connection
63+
retcode = sock.open(&iface);
64+
if (retcode != NSAPI_ERROR_OK) {
65+
printf("TCPSocket.open() fails, code: %d\n", retcode);
66+
return -1;
67+
}
68+
69+
SocketAddress sock_addr;
70+
retcode = iface.gethostbyname(host_name, &sock_addr);
71+
if (retcode != NSAPI_ERROR_OK) {
72+
printf("Couldn't resolve remote host: %s, code: %d\n", host_name,
73+
retcode);
74+
return -1;
75+
}
76+
77+
sock_addr.set_port(port);
78+
79+
sock.set_timeout(15000);
80+
int n = 0;
81+
char *echo_string = "TEST";
82+
char recv_buf[4];
83+
84+
// Connect
85+
retcode = sock.connect(sock_addr);
86+
if (retcode < 0) {
87+
printf("TCPSocket.connect() fails, code: %d\n", retcode);
88+
return -1;
89+
} else {
90+
printf("TCP: connected with %s server\n", host_name);
91+
}
92+
93+
// Send some data
94+
retcode = sock.send((void*) echo_string, sizeof(echo_string));
95+
if (retcode < 0) {
96+
printf("TCPSocket.send() fails, code: %d\n", retcode);
97+
return -1;
98+
} else {
99+
printf("TCP: Sent %d Bytes to %s\n", retcode, host_name);
100+
}
101+
102+
// Wait for the echo
103+
n = sock.recv((void*) recv_buf, sizeof(recv_buf));
104+
105+
// Disconnect
106+
sock.close();
107+
108+
if (n > 0) {
109+
printf("Received from echo server %d Bytes\n", n);
110+
return 0;
111+
}
112+
113+
return -1;
114+
}
115+
116+
int main()
117+
{
118+
iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
119+
120+
/* Set Pin code for SIM card */
121+
iface.set_sim_pin(PIN_CODE);
122+
123+
/* Set network credentials here, for example the APN */
124+
iface.set_credentials(CREDENTIALS);
125+
126+
printf("\n\nmbed-os-example-cellular, Connecting...\n");
127+
128+
/* Attempt to connect to a cellular network */
129+
if (do_connect() == NSAPI_ERROR_OK) {
130+
int ret = test_send_recv();
131+
if (ret == 0) {
132+
printf("\n\nSuccess. Exiting \n\n");
133+
return 0;
134+
}
135+
}
136+
137+
printf("\n\nFailure. Exiting \n\n");
138+
return -1;
139+
}

CellularTCP/mbed-os.lib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/ARMmbed/mbed-os/#c53d51fe9220728bf8ed27afe7afc1ecc3f6f5d7

0 commit comments

Comments
 (0)