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:
Fri Mar 09 14:00:19 2018 +0000
Revision:
2:0f644d6045cf
Parent:
0:4611f6cf2413
Child:
6:5678c0b6f74e
Merge pull request #55 from ARMmbed/lib_and_server_update

lib and server update
.
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 0:4611f6cf2413 19 #include "UDPSocket.h"
mbed_official 0:4611f6cf2413 20 #include "OnboardCellularInterface.h"
mbed_official 0:4611f6cf2413 21
mbed_official 0:4611f6cf2413 22 #define UDP 0
mbed_official 0:4611f6cf2413 23 #define TCP 1
mbed_official 0:4611f6cf2413 24
mbed_official 0:4611f6cf2413 25 // SIM pin code goes here
mbed_official 0:4611f6cf2413 26 #ifndef MBED_CONF_APP_SIM_PIN_CODE
mbed_official 0:4611f6cf2413 27 # define MBED_CONF_APP_SIM_PIN_CODE "1234"
mbed_official 0:4611f6cf2413 28 #endif
mbed_official 0:4611f6cf2413 29
mbed_official 0:4611f6cf2413 30 #ifndef MBED_CONF_APP_APN
mbed_official 0:4611f6cf2413 31 # define MBED_CONF_APP_APN "internet"
mbed_official 0:4611f6cf2413 32 #endif
mbed_official 0:4611f6cf2413 33 #ifndef MBED_CONF_APP_USERNAME
mbed_official 0:4611f6cf2413 34 # define MBED_CONF_APP_USERNAME NULL
mbed_official 0:4611f6cf2413 35 #endif
mbed_official 0:4611f6cf2413 36 #ifndef MBED_CONF_APP_PASSWORD
mbed_official 0:4611f6cf2413 37 # define MBED_CONF_APP_PASSWORD NULL
mbed_official 0:4611f6cf2413 38 #endif
mbed_official 0:4611f6cf2413 39
mbed_official 0:4611f6cf2413 40 // Number of retries /
mbed_official 0:4611f6cf2413 41 #define RETRY_COUNT 3
mbed_official 0:4611f6cf2413 42
mbed_official 0:4611f6cf2413 43
mbed_official 0:4611f6cf2413 44
mbed_official 0:4611f6cf2413 45 // CellularInterface object
mbed_official 0:4611f6cf2413 46 OnboardCellularInterface iface;
mbed_official 0:4611f6cf2413 47
mbed_official 0:4611f6cf2413 48 // Echo server hostname
mbed_official 2:0f644d6045cf 49 const char *host_name = "echo.mbedcloudtesting.com";
mbed_official 0:4611f6cf2413 50
mbed_official 0:4611f6cf2413 51 // Echo server port (same for TCP and UDP)
mbed_official 0:4611f6cf2413 52 const int port = 7;
mbed_official 0:4611f6cf2413 53
mbed_official 0:4611f6cf2413 54 Mutex PrintMutex;
mbed_official 2:0f644d6045cf 55 Thread dot_thread(osPriorityNormal, 512);
mbed_official 0:4611f6cf2413 56
mbed_official 0:4611f6cf2413 57 #define PRINT_TEXT_LENGTH 128
mbed_official 0:4611f6cf2413 58 char print_text[PRINT_TEXT_LENGTH];
mbed_official 0:4611f6cf2413 59 void print_function(const char *input_string)
mbed_official 0:4611f6cf2413 60 {
mbed_official 0:4611f6cf2413 61 PrintMutex.lock();
mbed_official 0:4611f6cf2413 62 printf("%s", input_string);
mbed_official 0:4611f6cf2413 63 fflush(NULL);
mbed_official 0:4611f6cf2413 64 PrintMutex.unlock();
mbed_official 0:4611f6cf2413 65 }
mbed_official 0:4611f6cf2413 66
mbed_official 0:4611f6cf2413 67 void dot_event()
mbed_official 0:4611f6cf2413 68 {
mbed_official 0:4611f6cf2413 69
mbed_official 0:4611f6cf2413 70 while (true) {
mbed_official 0:4611f6cf2413 71 wait(4);
mbed_official 0:4611f6cf2413 72 if (!iface.is_connected()) {
mbed_official 0:4611f6cf2413 73 print_function(".");
mbed_official 0:4611f6cf2413 74 } else {
mbed_official 0:4611f6cf2413 75 break;
mbed_official 0:4611f6cf2413 76 }
mbed_official 0:4611f6cf2413 77 }
mbed_official 0:4611f6cf2413 78
mbed_official 0:4611f6cf2413 79 }
mbed_official 0:4611f6cf2413 80
mbed_official 0:4611f6cf2413 81
mbed_official 0:4611f6cf2413 82 /**
mbed_official 0:4611f6cf2413 83 * Connects to the Cellular Network
mbed_official 0:4611f6cf2413 84 */
mbed_official 0:4611f6cf2413 85 nsapi_error_t do_connect()
mbed_official 0:4611f6cf2413 86 {
mbed_official 0:4611f6cf2413 87 nsapi_error_t retcode;
mbed_official 0:4611f6cf2413 88 uint8_t retry_counter = 0;
mbed_official 0:4611f6cf2413 89
mbed_official 0:4611f6cf2413 90 while (!iface.is_connected()) {
mbed_official 0:4611f6cf2413 91
mbed_official 0:4611f6cf2413 92 retcode = iface.connect();
mbed_official 0:4611f6cf2413 93 if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
mbed_official 0:4611f6cf2413 94 print_function("\n\nAuthentication Failure. Exiting application\n");
mbed_official 0:4611f6cf2413 95 return retcode;
mbed_official 0:4611f6cf2413 96 } else if (retcode != NSAPI_ERROR_OK) {
mbed_official 0:4611f6cf2413 97 snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nCouldn't connect: %d, will retry\n", retcode);
mbed_official 0:4611f6cf2413 98 print_function(print_text);
mbed_official 0:4611f6cf2413 99 retry_counter++;
mbed_official 0:4611f6cf2413 100 continue;
mbed_official 0:4611f6cf2413 101 } else if (retcode != NSAPI_ERROR_OK && retry_counter > RETRY_COUNT) {
mbed_official 0:4611f6cf2413 102 snprintf(print_text, PRINT_TEXT_LENGTH, "\n\nFatal connection failure: %d\n", retcode);
mbed_official 0:4611f6cf2413 103 print_function(print_text);
mbed_official 0:4611f6cf2413 104 return retcode;
mbed_official 0:4611f6cf2413 105 }
mbed_official 0:4611f6cf2413 106
mbed_official 0:4611f6cf2413 107 break;
mbed_official 0:4611f6cf2413 108 }
mbed_official 0:4611f6cf2413 109
mbed_official 0:4611f6cf2413 110 print_function("\n\nConnection Established.\n");
mbed_official 0:4611f6cf2413 111
mbed_official 0:4611f6cf2413 112 return NSAPI_ERROR_OK;
mbed_official 0:4611f6cf2413 113 }
mbed_official 0:4611f6cf2413 114
mbed_official 0:4611f6cf2413 115 /**
mbed_official 0:4611f6cf2413 116 * Opens a UDP or a TCP socket with the given echo server and performs an echo
mbed_official 0:4611f6cf2413 117 * transaction retrieving current.
mbed_official 0:4611f6cf2413 118 */
mbed_official 0:4611f6cf2413 119 nsapi_error_t test_send_recv()
mbed_official 0:4611f6cf2413 120 {
mbed_official 0:4611f6cf2413 121 nsapi_size_or_error_t retcode;
mbed_official 0:4611f6cf2413 122 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 123 TCPSocket sock;
mbed_official 0:4611f6cf2413 124 #else
mbed_official 0:4611f6cf2413 125 UDPSocket sock;
mbed_official 0:4611f6cf2413 126 #endif
mbed_official 0:4611f6cf2413 127
mbed_official 0:4611f6cf2413 128 retcode = sock.open(&iface);
mbed_official 0:4611f6cf2413 129 if (retcode != NSAPI_ERROR_OK) {
mbed_official 0:4611f6cf2413 130 snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.open() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 131 print_function(print_text);
mbed_official 0:4611f6cf2413 132 return -1;
mbed_official 0:4611f6cf2413 133 }
mbed_official 0:4611f6cf2413 134
mbed_official 0:4611f6cf2413 135 SocketAddress sock_addr;
mbed_official 0:4611f6cf2413 136 retcode = iface.gethostbyname(host_name, &sock_addr);
mbed_official 0:4611f6cf2413 137 if (retcode != NSAPI_ERROR_OK) {
mbed_official 0:4611f6cf2413 138 snprintf(print_text, PRINT_TEXT_LENGTH, "Couldn't resolve remote host: %s, code: %d\n", host_name,
mbed_official 0:4611f6cf2413 139 retcode);
mbed_official 0:4611f6cf2413 140 print_function(print_text);
mbed_official 0:4611f6cf2413 141 return -1;
mbed_official 0:4611f6cf2413 142 }
mbed_official 0:4611f6cf2413 143
mbed_official 0:4611f6cf2413 144 sock_addr.set_port(port);
mbed_official 0:4611f6cf2413 145
mbed_official 0:4611f6cf2413 146 sock.set_timeout(15000);
mbed_official 0:4611f6cf2413 147 int n = 0;
mbed_official 0:4611f6cf2413 148 const char *echo_string = "TEST";
mbed_official 0:4611f6cf2413 149 char recv_buf[4];
mbed_official 0:4611f6cf2413 150 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 151 retcode = sock.connect(sock_addr);
mbed_official 0:4611f6cf2413 152 if (retcode < 0) {
mbed_official 0:4611f6cf2413 153 snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.connect() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 154 print_function(print_text);
mbed_official 0:4611f6cf2413 155 return -1;
mbed_official 0:4611f6cf2413 156 } else {
mbed_official 0:4611f6cf2413 157 snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: connected with %s server\n", host_name);
mbed_official 0:4611f6cf2413 158 print_function(print_text);
mbed_official 0:4611f6cf2413 159 }
mbed_official 0:4611f6cf2413 160 retcode = sock.send((void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 161 if (retcode < 0) {
mbed_official 0:4611f6cf2413 162 snprintf(print_text, PRINT_TEXT_LENGTH, "TCPSocket.send() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 163 print_function(print_text);
mbed_official 0:4611f6cf2413 164 return -1;
mbed_official 0:4611f6cf2413 165 } else {
mbed_official 0:4611f6cf2413 166 snprintf(print_text, PRINT_TEXT_LENGTH, "TCP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 167 print_function(print_text);
mbed_official 0:4611f6cf2413 168 }
mbed_official 0:4611f6cf2413 169
mbed_official 0:4611f6cf2413 170 n = sock.recv((void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 171 #else
mbed_official 0:4611f6cf2413 172
mbed_official 0:4611f6cf2413 173 retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 174 if (retcode < 0) {
mbed_official 0:4611f6cf2413 175 snprintf(print_text, PRINT_TEXT_LENGTH, "UDPSocket.sendto() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 176 print_function(print_text);
mbed_official 0:4611f6cf2413 177 return -1;
mbed_official 0:4611f6cf2413 178 } else {
mbed_official 0:4611f6cf2413 179 snprintf(print_text, PRINT_TEXT_LENGTH, "UDP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 180 print_function(print_text);
mbed_official 0:4611f6cf2413 181 }
mbed_official 0:4611f6cf2413 182
mbed_official 0:4611f6cf2413 183 n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 184 #endif
mbed_official 0:4611f6cf2413 185
mbed_official 0:4611f6cf2413 186 sock.close();
mbed_official 0:4611f6cf2413 187
mbed_official 0:4611f6cf2413 188 if (n > 0) {
mbed_official 0:4611f6cf2413 189 snprintf(print_text, PRINT_TEXT_LENGTH, "Received from echo server %d Bytes\n", n);
mbed_official 0:4611f6cf2413 190 print_function(print_text);
mbed_official 0:4611f6cf2413 191 return 0;
mbed_official 0:4611f6cf2413 192 }
mbed_official 0:4611f6cf2413 193
mbed_official 0:4611f6cf2413 194 return -1;
mbed_official 0:4611f6cf2413 195 }
mbed_official 0:4611f6cf2413 196
mbed_official 0:4611f6cf2413 197 int main()
mbed_official 0:4611f6cf2413 198 {
mbed_official 0:4611f6cf2413 199
mbed_official 0:4611f6cf2413 200 iface.modem_debug_on(MBED_CONF_APP_MODEM_TRACE);
mbed_official 0:4611f6cf2413 201 /* Set Pin code for SIM card */
mbed_official 0:4611f6cf2413 202 iface.set_sim_pin(MBED_CONF_APP_SIM_PIN_CODE);
mbed_official 0:4611f6cf2413 203
mbed_official 0:4611f6cf2413 204 /* Set network credentials here, e.g., APN*/
mbed_official 0:4611f6cf2413 205 iface.set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD);
mbed_official 0:4611f6cf2413 206
mbed_official 0:4611f6cf2413 207 print_function("\n\nmbed-os-example-cellular\n");
mbed_official 0:4611f6cf2413 208 print_function("Establishing connection ");
mbed_official 0:4611f6cf2413 209 dot_thread.start(dot_event);
mbed_official 0:4611f6cf2413 210
mbed_official 0:4611f6cf2413 211 /* Attempt to connect to a cellular network */
mbed_official 0:4611f6cf2413 212 if (do_connect() == NSAPI_ERROR_OK) {
mbed_official 0:4611f6cf2413 213 nsapi_error_t retcode = test_send_recv();
mbed_official 0:4611f6cf2413 214 if (retcode == NSAPI_ERROR_OK) {
mbed_official 0:4611f6cf2413 215 print_function("\n\nSuccess. Exiting \n\n");
mbed_official 0:4611f6cf2413 216 return 0;
mbed_official 0:4611f6cf2413 217 }
mbed_official 0:4611f6cf2413 218 }
mbed_official 0:4611f6cf2413 219
mbed_official 0:4611f6cf2413 220 print_function("\n\nFailure. Exiting \n\n");
mbed_official 0:4611f6cf2413 221 return -1;
mbed_official 0:4611f6cf2413 222 }
mbed_official 0:4611f6cf2413 223 // EOF