This is an example based on mbed-os cellular APIs that demonstrates a TCP or UDP echo transaction with a public echo server.

This code is forked from https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-cellular/

This is an example showing how to use Skywire board with LE910-x module.

In this example, I had used the LE910-EUG module for 4G communication. Mbed OS code wasn't changed for LE910-x, because the Telit' HE910 is included mainline code on Mbed OS already. HE910 and LE910 are most similar. Therefore you can use LE910 via HE910 driver. Please check to configure of mbed_app.json, and initialize code for Skywire board of main.cpp.

Open mbed_app.json, you need to define a UART for the MCU to communicate with the xE910 module.

            "TELIT_HE910.tx"                 : "D1",
            "TELIT_HE910.rx"                 : "D0",
            "TELIT_HE910.provide-default"    : true

If you are using Pelion CM, make the following settings:

            "nsapi.default-cellular-apn"     : "\"stream.co.uk\"",
            "nsapi.default-cellular-username": "\"streamip\"",
            "nsapi.default-cellular-password": "\"streamip\"",

For your information, please see Pelion Connectivity Quick start guide.

320

You can find Skywire sensor shield information though NimbeLink site.

Tested with

  • DISCO_L475VG_IOT01A
  • K64F

1. Import the application into your desktop:

 mbed import https://os.mbed.com/users/Daniel_Lee/code/mbed-os-example-cellular-le910/

 cd mbed-os-example-cellular-le910

2. Compile and program:

mbed compile -t GCC_ARM -m DISCO_L475VG_IOT01A

(supported toolchains : GCC_ARM / ARM / IAR)

3. Download binary to a target board

4. Result

mbed-os-example-cellular


Built: Feb  7 2020, 07:02:27
Starting Skywire board with LE910-EUG Demo...
Waiting for Skywire to Boot...
Wait 15 seconds..
Establishing connection


Connection Established.
TCP: connected with echo.mbedcloudtesting.com server
TCP: Sent 4 Bytes to echo.mbedcloudtesting.com
Received from echo server 4 Bytes


Success. Exiting 
Committer:
mbed_official
Date:
Wed May 29 08:01:56 2019 +0100
Revision:
42:cab443dff75e
Parent:
40:c46ee5b71843
Child:
51:8d0212a4b805
Merge pull request #143 from statropy/statropy-patch-1

Use the actual length of the test string payload
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cellular

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:4611f6cf2413 1 /*
mbed_official 0:4611f6cf2413 2 * Copyright (c) 2017 ARM Limited. All rights reserved.
mbed_official 0:4611f6cf2413 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 0:4611f6cf2413 4 * Licensed under the Apache License, Version 2.0 (the License); you may
mbed_official 0:4611f6cf2413 5 * not use this file except in compliance with the License.
mbed_official 0:4611f6cf2413 6 * You may obtain a copy of the License at
mbed_official 0:4611f6cf2413 7 *
mbed_official 0:4611f6cf2413 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:4611f6cf2413 9 *
mbed_official 0:4611f6cf2413 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:4611f6cf2413 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
mbed_official 0:4611f6cf2413 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:4611f6cf2413 13 * See the License for the specific language governing permissions and
mbed_official 0:4611f6cf2413 14 * limitations under the License.
mbed_official 0:4611f6cf2413 15 */
mbed_official 0:4611f6cf2413 16
mbed_official 0:4611f6cf2413 17 #include "mbed.h"
mbed_official 0:4611f6cf2413 18 #include "common_functions.h"
mbed_official 40:c46ee5b71843 19 #include "CellularNonIPSocket.h"
mbed_official 40:c46ee5b71843 20 #include "CellularDevice.h"
mbed_official 0:4611f6cf2413 21 #include "UDPSocket.h"
mbed_official 6:5678c0b6f74e 22 #include "CellularLog.h"
mbed_official 0:4611f6cf2413 23
mbed_official 0:4611f6cf2413 24 #define UDP 0
mbed_official 0:4611f6cf2413 25 #define TCP 1
mbed_official 40:c46ee5b71843 26 #define NONIP 2
mbed_official 0:4611f6cf2413 27
mbed_official 0:4611f6cf2413 28 // Number of retries /
mbed_official 0:4611f6cf2413 29 #define RETRY_COUNT 3
mbed_official 0:4611f6cf2413 30
mbed_official 28:232da3ce8a88 31 NetworkInterface *iface;
mbed_official 0:4611f6cf2413 32
mbed_official 0:4611f6cf2413 33 // Echo server hostname
mbed_official 11:23ea0907186e 34 const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
mbed_official 0:4611f6cf2413 35
mbed_official 0:4611f6cf2413 36 // Echo server port (same for TCP and UDP)
mbed_official 11:23ea0907186e 37 const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
mbed_official 0:4611f6cf2413 38
mbed_official 6:5678c0b6f74e 39 static rtos::Mutex trace_mutex;
mbed_official 6:5678c0b6f74e 40
mbed_official 6:5678c0b6f74e 41 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 42 static void trace_wait()
mbed_official 6:5678c0b6f74e 43 {
mbed_official 6:5678c0b6f74e 44 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 45 }
mbed_official 6:5678c0b6f74e 46
mbed_official 6:5678c0b6f74e 47 static void trace_release()
mbed_official 6:5678c0b6f74e 48 {
mbed_official 6:5678c0b6f74e 49 trace_mutex.unlock();
mbed_official 6:5678c0b6f74e 50 }
mbed_official 6:5678c0b6f74e 51
mbed_official 6:5678c0b6f74e 52 static char time_st[50];
mbed_official 6:5678c0b6f74e 53
mbed_official 6:5678c0b6f74e 54 static char* trace_time(size_t ss)
mbed_official 6:5678c0b6f74e 55 {
mbed_official 6:5678c0b6f74e 56 snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
mbed_official 6:5678c0b6f74e 57 return time_st;
mbed_official 6:5678c0b6f74e 58 }
mbed_official 6:5678c0b6f74e 59
mbed_official 6:5678c0b6f74e 60 static void trace_open()
mbed_official 6:5678c0b6f74e 61 {
mbed_official 6:5678c0b6f74e 62 mbed_trace_init();
mbed_official 6:5678c0b6f74e 63 mbed_trace_prefix_function_set( &trace_time );
mbed_official 6:5678c0b6f74e 64
mbed_official 6:5678c0b6f74e 65 mbed_trace_mutex_wait_function_set(trace_wait);
mbed_official 6:5678c0b6f74e 66 mbed_trace_mutex_release_function_set(trace_release);
mbed_official 11:23ea0907186e 67
mbed_official 11:23ea0907186e 68 mbed_cellular_trace::mutex_wait_function_set(trace_wait);
mbed_official 11:23ea0907186e 69 mbed_cellular_trace::mutex_release_function_set(trace_release);
mbed_official 6:5678c0b6f74e 70 }
mbed_official 6:5678c0b6f74e 71
mbed_official 6:5678c0b6f74e 72 static void trace_close()
mbed_official 6:5678c0b6f74e 73 {
mbed_official 11:23ea0907186e 74 mbed_cellular_trace::mutex_wait_function_set(NULL);
mbed_official 11:23ea0907186e 75 mbed_cellular_trace::mutex_release_function_set(NULL);
mbed_official 11:23ea0907186e 76
mbed_official 6:5678c0b6f74e 77 mbed_trace_free();
mbed_official 6:5678c0b6f74e 78 }
mbed_official 6:5678c0b6f74e 79 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 80
mbed_official 2:0f644d6045cf 81 Thread dot_thread(osPriorityNormal, 512);
mbed_official 0:4611f6cf2413 82
mbed_official 6:5678c0b6f74e 83 void print_function(const char *format, ...)
mbed_official 0:4611f6cf2413 84 {
mbed_official 6:5678c0b6f74e 85 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 86 va_list arglist;
mbed_official 6:5678c0b6f74e 87 va_start( arglist, format );
mbed_official 6:5678c0b6f74e 88 vprintf(format, arglist);
mbed_official 6:5678c0b6f74e 89 va_end( arglist );
mbed_official 6:5678c0b6f74e 90 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 91 }
mbed_official 0:4611f6cf2413 92
mbed_official 0:4611f6cf2413 93 void dot_event()
mbed_official 0:4611f6cf2413 94 {
mbed_official 0:4611f6cf2413 95 while (true) {
mbed_official 26:348eec457e58 96 ThisThread::sleep_for(4000);
mbed_official 28:232da3ce8a88 97 if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) {
mbed_official 13:4bea5334b419 98 break;
mbed_official 13:4bea5334b419 99 } else {
mbed_official 6:5678c0b6f74e 100 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 101 printf(".");
mbed_official 6:5678c0b6f74e 102 fflush(stdout);
mbed_official 6:5678c0b6f74e 103 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 104 }
mbed_official 0:4611f6cf2413 105 }
mbed_official 0:4611f6cf2413 106 }
mbed_official 0:4611f6cf2413 107
mbed_official 0:4611f6cf2413 108 /**
mbed_official 0:4611f6cf2413 109 * Connects to the Cellular Network
mbed_official 0:4611f6cf2413 110 */
mbed_official 0:4611f6cf2413 111 nsapi_error_t do_connect()
mbed_official 0:4611f6cf2413 112 {
mbed_official 6:5678c0b6f74e 113 nsapi_error_t retcode = NSAPI_ERROR_OK;
mbed_official 0:4611f6cf2413 114 uint8_t retry_counter = 0;
mbed_official 0:4611f6cf2413 115
mbed_official 28:232da3ce8a88 116 while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
mbed_official 11:23ea0907186e 117 retcode = iface->connect();
mbed_official 0:4611f6cf2413 118 if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
mbed_official 0:4611f6cf2413 119 print_function("\n\nAuthentication Failure. Exiting application\n");
mbed_official 6:5678c0b6f74e 120 } else if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 121 print_function("\n\nConnection Established.\n");
mbed_official 6:5678c0b6f74e 122 } else if (retry_counter > RETRY_COUNT) {
mbed_official 6:5678c0b6f74e 123 print_function("\n\nFatal connection failure: %d\n", retcode);
mbed_official 6:5678c0b6f74e 124 } else {
mbed_official 6:5678c0b6f74e 125 print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
mbed_official 0:4611f6cf2413 126 retry_counter++;
mbed_official 0:4611f6cf2413 127 continue;
mbed_official 0:4611f6cf2413 128 }
mbed_official 0:4611f6cf2413 129 break;
mbed_official 0:4611f6cf2413 130 }
mbed_official 6:5678c0b6f74e 131 return retcode;
mbed_official 0:4611f6cf2413 132 }
mbed_official 0:4611f6cf2413 133
mbed_official 0:4611f6cf2413 134 /**
mbed_official 40:c46ee5b71843 135 * Opens:
mbed_official 40:c46ee5b71843 136 * - UDP or TCP socket with the given echo server and performs an echo
mbed_official 40:c46ee5b71843 137 * transaction retrieving current.
mbed_official 40:c46ee5b71843 138 * - Cellular Non-IP socket for which the data delivery path is decided
mbed_official 40:c46ee5b71843 139 * by network's control plane CIoT optimisation setup, for the given APN.
mbed_official 0:4611f6cf2413 140 */
mbed_official 0:4611f6cf2413 141 nsapi_error_t test_send_recv()
mbed_official 0:4611f6cf2413 142 {
mbed_official 0:4611f6cf2413 143 nsapi_size_or_error_t retcode;
mbed_official 0:4611f6cf2413 144 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 145 TCPSocket sock;
mbed_official 40:c46ee5b71843 146 #elif MBED_CONF_APP_SOCK_TYPE == UDP
mbed_official 0:4611f6cf2413 147 UDPSocket sock;
mbed_official 40:c46ee5b71843 148 #elif MBED_CONF_APP_SOCK_TYPE == NONIP
mbed_official 40:c46ee5b71843 149 CellularNonIPSocket sock;
mbed_official 0:4611f6cf2413 150 #endif
mbed_official 0:4611f6cf2413 151
mbed_official 40:c46ee5b71843 152 #if MBED_CONF_APP_SOCK_TYPE == NONIP
mbed_official 40:c46ee5b71843 153 retcode = sock.open((CellularContext*)iface);
mbed_official 40:c46ee5b71843 154 #else
mbed_official 11:23ea0907186e 155 retcode = sock.open(iface);
mbed_official 40:c46ee5b71843 156 #endif
mbed_official 40:c46ee5b71843 157
mbed_official 0:4611f6cf2413 158 if (retcode != NSAPI_ERROR_OK) {
mbed_official 21:e356e039f917 159 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 21:e356e039f917 160 print_function("TCPSocket.open() fails, code: %d\n", retcode);
mbed_official 40:c46ee5b71843 161 #elif MBED_CONF_APP_SOCK_TYPE == UDP
mbed_official 6:5678c0b6f74e 162 print_function("UDPSocket.open() fails, code: %d\n", retcode);
mbed_official 40:c46ee5b71843 163 #elif MBED_CONF_APP_SOCK_TYPE == NONIP
mbed_official 40:c46ee5b71843 164 print_function("CellularNonIPSocket.open() fails, code: %d\n", retcode);
mbed_official 21:e356e039f917 165 #endif
mbed_official 0:4611f6cf2413 166 return -1;
mbed_official 0:4611f6cf2413 167 }
mbed_official 0:4611f6cf2413 168
mbed_official 40:c46ee5b71843 169 int n = 0;
mbed_official 40:c46ee5b71843 170 const char *echo_string = "TEST";
mbed_official 40:c46ee5b71843 171 char recv_buf[4];
mbed_official 40:c46ee5b71843 172
mbed_official 40:c46ee5b71843 173 sock.set_timeout(15000);
mbed_official 40:c46ee5b71843 174
mbed_official 40:c46ee5b71843 175 #if MBED_CONF_APP_SOCK_TYPE == NONIP
mbed_official 42:cab443dff75e 176 retcode = sock.send((void*) echo_string, strlen(echo_string));
mbed_official 40:c46ee5b71843 177 if (retcode < 0) {
mbed_official 40:c46ee5b71843 178 print_function("CellularNonIPSocket.send() fails, code: %d\n", retcode);
mbed_official 40:c46ee5b71843 179 return -1;
mbed_official 40:c46ee5b71843 180 } else {
mbed_official 40:c46ee5b71843 181 print_function("CellularNonIPSocket: Sent %d Bytes\n", retcode);
mbed_official 40:c46ee5b71843 182 }
mbed_official 40:c46ee5b71843 183
mbed_official 40:c46ee5b71843 184 n = sock.recv((void*) recv_buf, sizeof(recv_buf));
mbed_official 40:c46ee5b71843 185
mbed_official 40:c46ee5b71843 186 #else
mbed_official 40:c46ee5b71843 187
mbed_official 0:4611f6cf2413 188 SocketAddress sock_addr;
mbed_official 11:23ea0907186e 189 retcode = iface->gethostbyname(host_name, &sock_addr);
mbed_official 0:4611f6cf2413 190 if (retcode != NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 191 print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
mbed_official 0:4611f6cf2413 192 return -1;
mbed_official 0:4611f6cf2413 193 }
mbed_official 0:4611f6cf2413 194
mbed_official 0:4611f6cf2413 195 sock_addr.set_port(port);
mbed_official 0:4611f6cf2413 196
mbed_official 0:4611f6cf2413 197 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 198 retcode = sock.connect(sock_addr);
mbed_official 0:4611f6cf2413 199 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 200 print_function("TCPSocket.connect() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 201 return -1;
mbed_official 0:4611f6cf2413 202 } else {
mbed_official 6:5678c0b6f74e 203 print_function("TCP: connected with %s server\n", host_name);
mbed_official 0:4611f6cf2413 204 }
mbed_official 42:cab443dff75e 205 retcode = sock.send((void*) echo_string, strlen(echo_string));
mbed_official 0:4611f6cf2413 206 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 207 print_function("TCPSocket.send() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 208 return -1;
mbed_official 0:4611f6cf2413 209 } else {
mbed_official 6:5678c0b6f74e 210 print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 211 }
mbed_official 0:4611f6cf2413 212
mbed_official 0:4611f6cf2413 213 n = sock.recv((void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 214 #else
mbed_official 0:4611f6cf2413 215
mbed_official 42:cab443dff75e 216 retcode = sock.sendto(sock_addr, (void*) echo_string, strlen(echo_string));
mbed_official 0:4611f6cf2413 217 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 218 print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 219 return -1;
mbed_official 0:4611f6cf2413 220 } else {
mbed_official 6:5678c0b6f74e 221 print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 222 }
mbed_official 0:4611f6cf2413 223
mbed_official 0:4611f6cf2413 224 n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 225 #endif
mbed_official 40:c46ee5b71843 226 #endif
mbed_official 0:4611f6cf2413 227
mbed_official 0:4611f6cf2413 228 sock.close();
mbed_official 0:4611f6cf2413 229
mbed_official 0:4611f6cf2413 230 if (n > 0) {
mbed_official 6:5678c0b6f74e 231 print_function("Received from echo server %d Bytes\n", n);
mbed_official 0:4611f6cf2413 232 return 0;
mbed_official 0:4611f6cf2413 233 }
mbed_official 0:4611f6cf2413 234
mbed_official 0:4611f6cf2413 235 return -1;
mbed_official 0:4611f6cf2413 236 }
mbed_official 0:4611f6cf2413 237
mbed_official 0:4611f6cf2413 238 int main()
mbed_official 0:4611f6cf2413 239 {
mbed_official 6:5678c0b6f74e 240 print_function("\n\nmbed-os-example-cellular\n");
mbed_official 34:6f85d44597ac 241 print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__);
mbed_official 34:6f85d44597ac 242 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
mbed_official 36:6294a71c9e9e 243 print_function("\n\n[MAIN], plmn: %s\n", (MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN ? MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN : "NULL"));
mbed_official 34:6f85d44597ac 244 #endif
mbed_official 34:6f85d44597ac 245
mbed_official 6:5678c0b6f74e 246 print_function("Establishing connection\n");
mbed_official 6:5678c0b6f74e 247 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 248 trace_open();
mbed_official 6:5678c0b6f74e 249 #else
mbed_official 6:5678c0b6f74e 250 dot_thread.start(dot_event);
mbed_official 6:5678c0b6f74e 251 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 27:97054be1a741 252
mbed_official 40:c46ee5b71843 253 #if MBED_CONF_APP_SOCK_TYPE == NONIP
mbed_official 40:c46ee5b71843 254 iface = CellularContext::get_default_nonip_instance();
mbed_official 40:c46ee5b71843 255 #else
mbed_official 40:c46ee5b71843 256 iface = CellularContext::get_default_instance();
mbed_official 40:c46ee5b71843 257 #endif
mbed_official 40:c46ee5b71843 258
mbed_official 11:23ea0907186e 259 MBED_ASSERT(iface);
mbed_official 11:23ea0907186e 260
mbed_official 40:c46ee5b71843 261 // sim pin, apn, credentials and possible plmn are taken automatically from json when using NetworkInterface::set_default_parameters()
mbed_official 40:c46ee5b71843 262 iface->set_default_parameters();
mbed_official 40:c46ee5b71843 263
mbed_official 6:5678c0b6f74e 264 nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
mbed_official 0:4611f6cf2413 265
mbed_official 0:4611f6cf2413 266 /* Attempt to connect to a cellular network */
mbed_official 0:4611f6cf2413 267 if (do_connect() == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 268 retcode = test_send_recv();
mbed_official 0:4611f6cf2413 269 }
mbed_official 0:4611f6cf2413 270
mbed_official 26:348eec457e58 271 if (iface->disconnect() != NSAPI_ERROR_OK) {
mbed_official 26:348eec457e58 272 print_function("\n\n disconnect failed.\n\n");
mbed_official 26:348eec457e58 273 }
mbed_official 26:348eec457e58 274
mbed_official 6:5678c0b6f74e 275 if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 276 print_function("\n\nSuccess. Exiting \n\n");
mbed_official 6:5678c0b6f74e 277 } else {
mbed_official 6:5678c0b6f74e 278 print_function("\n\nFailure. Exiting \n\n");
mbed_official 6:5678c0b6f74e 279 }
mbed_official 26:348eec457e58 280
mbed_official 6:5678c0b6f74e 281 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 282 trace_close();
mbed_official 26:348eec457e58 283 #else
mbed_official 26:348eec457e58 284 dot_thread.terminate();
mbed_official 6:5678c0b6f74e 285 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 286
mbed_official 6:5678c0b6f74e 287 return 0;
mbed_official 0:4611f6cf2413 288 }
mbed_official 0:4611f6cf2413 289 // EOF