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
- 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
- Confirm you're able to build the application (see below example on how to build using Mbed CLI)
- Prepare application to use WiFi
- Review platform information and find out which WiFi driver should be used. You can look at the example applications for the corresponding platform you're using (
main.cppandmbed_app.json): - If the WiFi driver is not part of the Mbed OS platform, you may need to add the driver. Check the drivers in Mbed OS here
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.jsonfile 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);
- Write a small piece of code to handle a response on the socket. See the TCP socket API reference documentation. For example:
// 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.