This is an example based on mbed-os cellular APIs that demonstrates a TCP or UDP echo transaction with a public echo server. Especially, BG96 operation check based Mbed OS 5.11, tested via SK Telecom in Korea

This code could be access via Cat.M1(BG96 module) of SK telecom network in Korea. Need a WIZnet BG96 board and development board. Before using this code, must be checking your modem is authorized or not from SK telecom. This code is based on Mbed OS 5.11.5.

/media/uploads/Daniel_Lee/img_8560.jpg

Tested with

  • DISCOVERY_L485VG_IOT01A
  • K66F
  • K64F

1. Import the application into your desktop

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

 cd mbed-os-example-cellular-BG96_os511

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: Jun  4 2019, 13:45:53


[MAIN], plmn: (null)
Modem power on
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

5. Patched code

If need more information such as how to test, please look at https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-cellular/.

Committer:
Daniel_Lee
Date:
Tue Jun 04 07:20:23 2019 +0000
Revision:
46:81a8fcab4a6a
Parent:
45:dc095588e1b4
BG96 operation check based Mbed OS 5.11, tested via SK Telecom in Korea

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