This is an example based on mbed-os cellular APIs that demonstrates a TCP or UDP echo transaction with a public echo server. Included TPB23 NB-IoT module which is can use LGU+ in Korea.

This example is showing how to use the NB-IoT(SECOM TPB23 module) of LGU plus network in Korea. The NB-IoT module' seller provided a document on how to start based on Mbed OS 5.11. (Created by CodeZoo) This guide has been supported to the latest os version which included 5.12 ~ 5.15.

  • if you need specific version of Mbed OS, please revision of TPB23_Driver and check it out.
  • Pelion ready example code is here!

Requirement

462 320 320

320

Tested with

1. Import the application into your desktop

mbed import http://os.mbed.com/users/Daniel_Lee/code/mbed-os-example-cellular-TPB23/

cd mbed-os-example-cellular-TPB23

2. Compile and program

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

3. Download binary to a target board

4. Result

mbed-os-example-cellular


Built: Aug  7 2019, 06:10:47


[MAIN], plmn: NULL
Establishing connection
[00000001ms][INFO][CELL]: New CellularContext  (20000a08)
[00000002ms][INFO][CELL]: CellularContext plmn NULL
[00000003ms][INFO][CELL]: CellularContext connect
[00000036ms][INFO][CELL]: RSSI -69 dBm
[00000036ms][INFO][CELL]: Start connecting (timeout 1 s)
[00000087ms][INFO][CELL]: RSSI -69 dBm
[00000116ms][INFO][CELL]: Modem ready
[00000146ms][INFO][CELL]: RSSI -69 dBm
[00000146ms][INFO][CELL]: Setup SIM (timeout 1 s)
[00001233ms][INFO][CELL]: Looked up APN internet
[00001362ms][ERR ][CELL]: AT error code 50
[00001362ms][WARN][CELL]: Packet domain event reporting set failed!
[00001393ms][INFO][CELL]: RSSI -65 dBm
[00001393ms][INFO][CELL]: Network registration (timeout 180 s)
[00001443ms][INFO][CELL]: Registering network => Attaching network
[00001473ms][INFO][CELL]: RSSI -65 dBm
[00001473ms][INFO][CELL]: Attaching network (timeout 60 s)
[00001601ms][INFO][CELL]: Found PDP context 1
[00001649ms][INFO][CELL]: Activate PDP context 1


Connection Established.
[00001671ms][INFO][CELL]: Socket 0 open
[00001707ms][INFO][CELL]: Socket 1 open
[00001748ms][INFO][CELL]: Socket create id: 1
[00001883ms][INFO][CELL]: Socket 1 sent 43 bytes to 8.8.8.8 port 53
[00004311ms][INFO][CELL]: Socket 1 recv 59 bytes from 8.8.8.8 port 53
[00004331ms][INFO][CELL]: Close socket: 1 error: 0
[00004331ms][INFO][CELL]: Socket 1 closed
[00004373ms][INFO][CELL]: Socket create id: 1
TCP: connected with echo.mbedcloudtesting.com server
[00004448ms][INFO][CELL]: Socket 0 sent 4 bytes to 52.215.34.155 port 7
TCP: Sent 4 Bytes to echo.mbedcloudtesting.com
[00007560ms][INFO][CELL]: Socket 0 recv 4 bytes
[00007580ms][INFO][CELL]: Close socket: 1 error: 0
[00007580ms][INFO][CELL]: Socket 0 closed
Received from echo server 4 Bytes
[00007581ms][INFO][CELL]: CellularContext disconnect()
[00007630ms][INFO][CELL]: cb: CellularContext disconnected


Success. Exiting
Committer:
Daniel_Lee
Date:
Mon Feb 10 12:52:15 2020 +0000
Revision:
47:006469f922f6
Parent:
45:63eba30af193
Update latest OS version 5.15

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 6:5678c0b6f74e 20 #include "CellularLog.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 // Number of retries /
mbed_official 0:4611f6cf2413 26 #define RETRY_COUNT 3
mbed_official 0:4611f6cf2413 27
mbed_official 28:232da3ce8a88 28 NetworkInterface *iface;
mbed_official 0:4611f6cf2413 29
mbed_official 0:4611f6cf2413 30 // Echo server hostname
mbed_official 11:23ea0907186e 31 const char *host_name = MBED_CONF_APP_ECHO_SERVER_HOSTNAME;
mbed_official 0:4611f6cf2413 32
mbed_official 0:4611f6cf2413 33 // Echo server port (same for TCP and UDP)
mbed_official 11:23ea0907186e 34 const int port = MBED_CONF_APP_ECHO_SERVER_PORT;
mbed_official 0:4611f6cf2413 35
mbed_official 6:5678c0b6f74e 36 static rtos::Mutex trace_mutex;
mbed_official 6:5678c0b6f74e 37
mbed_official 6:5678c0b6f74e 38 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 39 static void trace_wait()
mbed_official 6:5678c0b6f74e 40 {
mbed_official 6:5678c0b6f74e 41 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 42 }
mbed_official 6:5678c0b6f74e 43
mbed_official 6:5678c0b6f74e 44 static void trace_release()
mbed_official 6:5678c0b6f74e 45 {
mbed_official 6:5678c0b6f74e 46 trace_mutex.unlock();
mbed_official 6:5678c0b6f74e 47 }
mbed_official 6:5678c0b6f74e 48
mbed_official 6:5678c0b6f74e 49 static char time_st[50];
mbed_official 6:5678c0b6f74e 50
mbed_official 6:5678c0b6f74e 51 static char* trace_time(size_t ss)
mbed_official 6:5678c0b6f74e 52 {
mbed_official 6:5678c0b6f74e 53 snprintf(time_st, 49, "[%08llums]", Kernel::get_ms_count());
mbed_official 6:5678c0b6f74e 54 return time_st;
mbed_official 6:5678c0b6f74e 55 }
mbed_official 6:5678c0b6f74e 56
mbed_official 6:5678c0b6f74e 57 static void trace_open()
mbed_official 6:5678c0b6f74e 58 {
mbed_official 6:5678c0b6f74e 59 mbed_trace_init();
mbed_official 6:5678c0b6f74e 60 mbed_trace_prefix_function_set( &trace_time );
mbed_official 6:5678c0b6f74e 61
mbed_official 6:5678c0b6f74e 62 mbed_trace_mutex_wait_function_set(trace_wait);
mbed_official 6:5678c0b6f74e 63 mbed_trace_mutex_release_function_set(trace_release);
mbed_official 11:23ea0907186e 64
mbed_official 11:23ea0907186e 65 mbed_cellular_trace::mutex_wait_function_set(trace_wait);
mbed_official 11:23ea0907186e 66 mbed_cellular_trace::mutex_release_function_set(trace_release);
mbed_official 6:5678c0b6f74e 67 }
mbed_official 6:5678c0b6f74e 68
mbed_official 6:5678c0b6f74e 69 static void trace_close()
mbed_official 6:5678c0b6f74e 70 {
mbed_official 11:23ea0907186e 71 mbed_cellular_trace::mutex_wait_function_set(NULL);
mbed_official 11:23ea0907186e 72 mbed_cellular_trace::mutex_release_function_set(NULL);
mbed_official 11:23ea0907186e 73
mbed_official 6:5678c0b6f74e 74 mbed_trace_free();
mbed_official 6:5678c0b6f74e 75 }
mbed_official 6:5678c0b6f74e 76 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 77
mbed_official 2:0f644d6045cf 78 Thread dot_thread(osPriorityNormal, 512);
mbed_official 0:4611f6cf2413 79
mbed_official 6:5678c0b6f74e 80 void print_function(const char *format, ...)
mbed_official 0:4611f6cf2413 81 {
mbed_official 6:5678c0b6f74e 82 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 83 va_list arglist;
mbed_official 6:5678c0b6f74e 84 va_start( arglist, format );
mbed_official 6:5678c0b6f74e 85 vprintf(format, arglist);
mbed_official 6:5678c0b6f74e 86 va_end( arglist );
mbed_official 6:5678c0b6f74e 87 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 88 }
mbed_official 0:4611f6cf2413 89
mbed_official 0:4611f6cf2413 90 void dot_event()
mbed_official 0:4611f6cf2413 91 {
mbed_official 0:4611f6cf2413 92 while (true) {
mbed_official 26:348eec457e58 93 ThisThread::sleep_for(4000);
mbed_official 28:232da3ce8a88 94 if (iface && iface->get_connection_status() == NSAPI_STATUS_GLOBAL_UP) {
mbed_official 13:4bea5334b419 95 break;
mbed_official 13:4bea5334b419 96 } else {
mbed_official 6:5678c0b6f74e 97 trace_mutex.lock();
mbed_official 6:5678c0b6f74e 98 printf(".");
mbed_official 6:5678c0b6f74e 99 fflush(stdout);
mbed_official 6:5678c0b6f74e 100 trace_mutex.unlock();
mbed_official 0:4611f6cf2413 101 }
mbed_official 0:4611f6cf2413 102 }
mbed_official 0:4611f6cf2413 103 }
mbed_official 0:4611f6cf2413 104
mbed_official 0:4611f6cf2413 105 /**
mbed_official 0:4611f6cf2413 106 * Connects to the Cellular Network
mbed_official 0:4611f6cf2413 107 */
mbed_official 0:4611f6cf2413 108 nsapi_error_t do_connect()
mbed_official 0:4611f6cf2413 109 {
mbed_official 6:5678c0b6f74e 110 nsapi_error_t retcode = NSAPI_ERROR_OK;
mbed_official 0:4611f6cf2413 111 uint8_t retry_counter = 0;
mbed_official 0:4611f6cf2413 112
mbed_official 28:232da3ce8a88 113 while (iface->get_connection_status() != NSAPI_STATUS_GLOBAL_UP) {
mbed_official 11:23ea0907186e 114 retcode = iface->connect();
mbed_official 0:4611f6cf2413 115 if (retcode == NSAPI_ERROR_AUTH_FAILURE) {
mbed_official 0:4611f6cf2413 116 print_function("\n\nAuthentication Failure. Exiting application\n");
mbed_official 6:5678c0b6f74e 117 } else if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 118 print_function("\n\nConnection Established.\n");
mbed_official 6:5678c0b6f74e 119 } else if (retry_counter > RETRY_COUNT) {
mbed_official 6:5678c0b6f74e 120 print_function("\n\nFatal connection failure: %d\n", retcode);
mbed_official 6:5678c0b6f74e 121 } else {
mbed_official 6:5678c0b6f74e 122 print_function("\n\nCouldn't connect: %d, will retry\n", retcode);
mbed_official 0:4611f6cf2413 123 retry_counter++;
mbed_official 0:4611f6cf2413 124 continue;
mbed_official 0:4611f6cf2413 125 }
mbed_official 0:4611f6cf2413 126 break;
mbed_official 0:4611f6cf2413 127 }
mbed_official 6:5678c0b6f74e 128 return retcode;
mbed_official 0:4611f6cf2413 129 }
mbed_official 0:4611f6cf2413 130
mbed_official 0:4611f6cf2413 131 /**
mbed_official 0:4611f6cf2413 132 * Opens a UDP or a TCP socket with the given echo server and performs an echo
mbed_official 0:4611f6cf2413 133 * transaction retrieving current.
mbed_official 0:4611f6cf2413 134 */
mbed_official 0:4611f6cf2413 135 nsapi_error_t test_send_recv()
mbed_official 0:4611f6cf2413 136 {
mbed_official 0:4611f6cf2413 137 nsapi_size_or_error_t retcode;
mbed_official 0:4611f6cf2413 138 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 139 TCPSocket sock;
mbed_official 0:4611f6cf2413 140 #else
mbed_official 0:4611f6cf2413 141 UDPSocket sock;
mbed_official 0:4611f6cf2413 142 #endif
mbed_official 0:4611f6cf2413 143
mbed_official 11:23ea0907186e 144 retcode = sock.open(iface);
mbed_official 0:4611f6cf2413 145 if (retcode != NSAPI_ERROR_OK) {
mbed_official 21:e356e039f917 146 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 21:e356e039f917 147 print_function("TCPSocket.open() fails, code: %d\n", retcode);
mbed_official 21:e356e039f917 148 #else
mbed_official 6:5678c0b6f74e 149 print_function("UDPSocket.open() fails, code: %d\n", retcode);
mbed_official 21:e356e039f917 150 #endif
mbed_official 0:4611f6cf2413 151 return -1;
mbed_official 0:4611f6cf2413 152 }
mbed_official 0:4611f6cf2413 153
mbed_official 0:4611f6cf2413 154 SocketAddress sock_addr;
mbed_official 11:23ea0907186e 155 retcode = iface->gethostbyname(host_name, &sock_addr);
mbed_official 0:4611f6cf2413 156 if (retcode != NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 157 print_function("Couldn't resolve remote host: %s, code: %d\n", host_name, retcode);
mbed_official 0:4611f6cf2413 158 return -1;
mbed_official 0:4611f6cf2413 159 }
mbed_official 0:4611f6cf2413 160
mbed_official 0:4611f6cf2413 161 sock_addr.set_port(port);
mbed_official 0:4611f6cf2413 162
mbed_official 0:4611f6cf2413 163 sock.set_timeout(15000);
mbed_official 0:4611f6cf2413 164 int n = 0;
mbed_official 0:4611f6cf2413 165 const char *echo_string = "TEST";
mbed_official 0:4611f6cf2413 166 char recv_buf[4];
mbed_official 0:4611f6cf2413 167 #if MBED_CONF_APP_SOCK_TYPE == TCP
mbed_official 0:4611f6cf2413 168 retcode = sock.connect(sock_addr);
mbed_official 0:4611f6cf2413 169 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 170 print_function("TCPSocket.connect() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 171 return -1;
mbed_official 0:4611f6cf2413 172 } else {
mbed_official 6:5678c0b6f74e 173 print_function("TCP: connected with %s server\n", host_name);
mbed_official 0:4611f6cf2413 174 }
mbed_official 0:4611f6cf2413 175 retcode = sock.send((void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 176 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 177 print_function("TCPSocket.send() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 178 return -1;
mbed_official 0:4611f6cf2413 179 } else {
mbed_official 6:5678c0b6f74e 180 print_function("TCP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 181 }
mbed_official 0:4611f6cf2413 182
mbed_official 0:4611f6cf2413 183 n = sock.recv((void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 184 #else
mbed_official 0:4611f6cf2413 185
mbed_official 0:4611f6cf2413 186 retcode = sock.sendto(sock_addr, (void*) echo_string, sizeof(echo_string));
mbed_official 0:4611f6cf2413 187 if (retcode < 0) {
mbed_official 6:5678c0b6f74e 188 print_function("UDPSocket.sendto() fails, code: %d\n", retcode);
mbed_official 0:4611f6cf2413 189 return -1;
mbed_official 0:4611f6cf2413 190 } else {
mbed_official 6:5678c0b6f74e 191 print_function("UDP: Sent %d Bytes to %s\n", retcode, host_name);
mbed_official 0:4611f6cf2413 192 }
mbed_official 0:4611f6cf2413 193
mbed_official 0:4611f6cf2413 194 n = sock.recvfrom(&sock_addr, (void*) recv_buf, sizeof(recv_buf));
mbed_official 0:4611f6cf2413 195 #endif
mbed_official 0:4611f6cf2413 196
mbed_official 0:4611f6cf2413 197 sock.close();
mbed_official 0:4611f6cf2413 198
mbed_official 0:4611f6cf2413 199 if (n > 0) {
mbed_official 6:5678c0b6f74e 200 print_function("Received from echo server %d Bytes\n", n);
mbed_official 0:4611f6cf2413 201 return 0;
mbed_official 0:4611f6cf2413 202 }
mbed_official 0:4611f6cf2413 203
mbed_official 0:4611f6cf2413 204 return -1;
mbed_official 0:4611f6cf2413 205 }
mbed_official 0:4611f6cf2413 206
mbed_official 0:4611f6cf2413 207 int main()
mbed_official 0:4611f6cf2413 208 {
Daniel_Lee 45:63eba30af193 209 DigitalOut TPB23_RESET(A1);
Daniel_Lee 45:63eba30af193 210 TPB23_RESET = 0; /* 0: Standby 1: Reset */
Daniel_Lee 45:63eba30af193 211
mbed_official 6:5678c0b6f74e 212 print_function("\n\nmbed-os-example-cellular\n");
mbed_official 34:6f85d44597ac 213 print_function("\n\nBuilt: %s, %s\n", __DATE__, __TIME__);
mbed_official 34:6f85d44597ac 214 #ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_PLMN
mbed_official 36:6294a71c9e9e 215 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 216 #endif
mbed_official 34:6f85d44597ac 217
mbed_official 6:5678c0b6f74e 218 print_function("Establishing connection\n");
mbed_official 6:5678c0b6f74e 219 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 220 trace_open();
mbed_official 6:5678c0b6f74e 221 #else
mbed_official 6:5678c0b6f74e 222 dot_thread.start(dot_event);
mbed_official 6:5678c0b6f74e 223 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 27:97054be1a741 224
mbed_official 27:97054be1a741 225 // sim pin, apn, credentials and possible plmn are taken atuomtically from json when using get_default_instance()
mbed_official 28:232da3ce8a88 226 iface = NetworkInterface::get_default_instance();
mbed_official 11:23ea0907186e 227 MBED_ASSERT(iface);
mbed_official 11:23ea0907186e 228
mbed_official 6:5678c0b6f74e 229 nsapi_error_t retcode = NSAPI_ERROR_NO_CONNECTION;
mbed_official 0:4611f6cf2413 230
mbed_official 0:4611f6cf2413 231 /* Attempt to connect to a cellular network */
mbed_official 0:4611f6cf2413 232 if (do_connect() == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 233 retcode = test_send_recv();
mbed_official 0:4611f6cf2413 234 }
mbed_official 0:4611f6cf2413 235
mbed_official 26:348eec457e58 236 if (iface->disconnect() != NSAPI_ERROR_OK) {
mbed_official 26:348eec457e58 237 print_function("\n\n disconnect failed.\n\n");
mbed_official 26:348eec457e58 238 }
mbed_official 26:348eec457e58 239
mbed_official 6:5678c0b6f74e 240 if (retcode == NSAPI_ERROR_OK) {
mbed_official 6:5678c0b6f74e 241 print_function("\n\nSuccess. Exiting \n\n");
mbed_official 6:5678c0b6f74e 242 } else {
mbed_official 6:5678c0b6f74e 243 print_function("\n\nFailure. Exiting \n\n");
mbed_official 6:5678c0b6f74e 244 }
mbed_official 26:348eec457e58 245
mbed_official 6:5678c0b6f74e 246 #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 247 trace_close();
mbed_official 26:348eec457e58 248 #else
mbed_official 26:348eec457e58 249 dot_thread.terminate();
mbed_official 6:5678c0b6f74e 250 #endif // #if MBED_CONF_MBED_TRACE_ENABLE
mbed_official 6:5678c0b6f74e 251
mbed_official 6:5678c0b6f74e 252 return 0;
mbed_official 0:4611f6cf2413 253 }
mbed_official 0:4611f6cf2413 254 // EOF