Table of Contents

Example Summary

The WiFi Ethernet Socket showcases how SlNetSock API’s will choose the correct interface on an automatic setting. This is done by opening up a TCP server using a Python script, then opening up a client connection every five seconds. When the client connects to the server, it sends ‘hello’ to the server, either over Ethernet or WiFi. It prioritizes Ethernet over WiFi, but if Ethernet is not available, it uses WiFi. The server then responds back with a ‘world-n’, where n is the number of times a client has connected to the server while the server has been running.

Hardware Prerequisites

You must have a switch with an available Ethernet port. For this example we will connect the device via Ethernet and WiFi to the same network. So the switch and access point must be on the same network.

Software Prerequisites

This example has been validated against the latest software versions listed in the Release Notes.

For more information on how to import this project into your IDE workspace and build/run, please refer to the Quick Start Guide.

Download Python 3 so that you can run the server script, tcp_server.py. Python 3.6 was used to test this code.

Provisioning Method

Before compiling the example, you need to update the network_if.h file for wireless access point parameters. Look at the example below of a WPA2 secured network with SSID name F4153-2.4 and password msp432iot.

/* AP SSID                                                                    */
#define SSID_NAME               "F4153-2.4"
/* Security type (OPEN or WEP or WPA)                                         */
#define SECURITY_TYPE           SL_WLAN_SEC_TYPE_WPA_WPA2
/* Password of the secured AP                                                 */
#define SECURITY_KEY            "msp432iot"
#define SSID_LEN_MAX            32
#define BSSID_LEN_MAX           6

Please refer to the provisioning example and documentation for different ways to get your access point information onto your device.

Usage

Open the code example, and go to the C file wifi_ethernet_sockets.h. Go to the #define IPADDRESS statement in the file, and change the IP address listed to match the one running the TCP server. Also, do the same on tcp_server.py, around line 12.

Open a command prompt and step into your directory that has tcp_server.py. Then type into your command window python tcp_server.py to run the server script.

Make sure that you start the python server script

  1. While connected to the same network the embedded device will be running on
  2. Before you start the embedded device

Once the example code starts the output to the terminal will notify the user of the IP addresses for the Wi-Fi and Ethernet interfaces. These will be two separate IP addresses because each interface is running independently creating a TCP client, one at a time (i.e. whichever interface has higher priority will create the first client). You will also see several messages from the Ethernet and Wi-Fi stacks as they start up and connect to the network.

TCP Echo Initialization

Now that the device has started up you can begin to tinker with the connections. Every 5 seconds a new client connection will be made to the server. In this example’s current state, Ethernet has higher priority, so the Ethernet address will be the default IP address used to make clients. If you disconnect Ethernet, Wi-Fi will used instead. Once you reconnect Ethernet, however, Ethernet will be used once again.

Software Design

The TCP socket example application works by starting a python TCP server script, then creating Ethernet and Wi-Fi clients to connect to the server. Both the Wi-Fi and Ethernet interfaces use the SlNetSock APIs. Each interface has it’s own thread that is creating and connecting clients to the TCP server.

Once the first client connects to the server, the client sends ‘hello’, and the server in reply, sends ‘world-1’. Each successive client will do the same, whether it be over Ethernet or Wi-Fi, but each new client will increment the number in the message sent by the server until the server resets.

This example showcases SlNetSock APIs choosing the right interface based on priority and connectivity. Ethernet has higher priority, so SlNetSock chooses Ethernet to send the message. If Ethernet is disconnected, it will choose Wi-Fi to send messages, but once Ethernet is reconnected, SlNetSock APIs will go back to using Ethernet to create clients.