This is an application to be used during a hands-on on Mbed OS sockets connectivity

Example application for network socket workshop

This is a simple TCP client application to demonstrate how easy is to modify is modify the network interface and get this working on an Mbed OS platform that supports WiFi connectivity.

The application is using the network-socket API and network-interfaces that Mbed OS provides.

Note: This example works out of the box on those platforms with an Ethernet connection (e.g. K64F, NUCLEO_F429, etc).

Hands-on exercise

  1. Import the application into your PC or Mbed Online Compiler. For example:

mbed import https://os.mbed.com/teams/Mbed-IoT-workshop/code/mbed-os-workshop-sockets
  1. Confirm you're able to build the application (see below example on how to build using Mbed CLI)
  2. Prepare application to use WiFi

For example:

mbed add esp8266-driver
  • Confirm address and port of Echo server. For example:

#define echo_server "192.168.1.148"
#define port 5001
  • Include header file (.h) for corresponding WiFi driver in main.cpp. For example using the ublox-EVK-ODIN-W2:

#include "OdinWiFiInterface.h"
  • Define network object for WiFi and pass corresponding parameters (UART, SPI, etc). Some of this configuration may be available in the mbed_app.json file of a reference application. For example, this is how to initialize the WiFI interface to use two UART serial signals with an ESP8266 module:

ESP8266Interface net(D8, D2);

The following example is how to modify the existing code to use the OdinWifiInterface with the ublox-EVK-ODIN-W2:

Change the following line in main.cpp:

EthernetInterface net;

To use the OdinWifiInterface:

OdinWifiInterface net;
  • Connect to WiFi network using the corresponding credentials (SSID, Password, Security). For example:

Change the following line:

int ret = net.connect();

To use the Wifi object created above:

int ret = net.connect(SSID, Password, NSAPI_SECURITY_WPA_WPA2);

// TODO: write receive function
int rcount = socket.recv(rbuffer, sizeof rbuffer);
  • Optionally, you may want to change the message you send to the echo server to see your traffic on the echo server:

 // Send text
        char sbuffer[] = "Mbed is awesome from YOUR NAME!.\r\n";
  • Be sure to close your Wifi connection at the end of the example by changing the following line:

net.disconnect();

Building using Mbed CLI

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

For example, building for NUCLEO_F429ZI using GCC:

mbed compile -t GCC_ARM -m NUCLEO_F429ZI

Expected output

Use your favorite serial port terminal to establish a serial connection with your platform (e.g. Putty, TeraTerm, CoolTerm).

Find out which one is the Mbed Serial interface by using mbed detect

Note: The default serial port baud rate is 9600 bit/s.

Simple TCP socket example
Device IP address: 192.168.1.230
Echo server IP address: 192.168.1.148
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Sent 19 [Mbed is awesome.]
Received 19 [Mbed is awesome.]
Done. Exit.

Troubleshooting

If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.

Files at this revision

API Documentation at this revision

Comitter:
MarceloSalazar
Date:
Tue Oct 30 16:55:50 2018 +0000
Parent:
2:c1729e59c719
Commit message:
Add public echo server

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Oct 30 10:52:58 2018 +0000
+++ b/main.cpp	Tue Oct 30 16:55:50 2018 +0000
@@ -7,8 +7,14 @@
 EthernetInterface net;
 
 // TODO: confirm IP address and port of TCP echo server
-#define echo_server "192.168.1.3"
-#define port 5001
+
+// Internal echo server
+// #define echo_server "192.168.1.2"
+// #define port 5001
+
+// Public echo server
+#define echo_server "echo.mbedcloudtesting.com"
+#define port 7
 
 // Socket demo
 int main() {
@@ -56,9 +62,11 @@
         // Receive a response and print out
         char rbuffer[64];
 
-        // TODO: write receive function
+        int rcount = 0;
 
-        int rcount = 0; // ....
+        // TODO: write receive function
+        // ....
+
         printf("Received %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
     }