nRF5 IoT SDK
v0.9.0
|
This example demonstrates how the lwIP stack can be used on Nordic's 6lowpan interface to listen on a TCP port. Port number 9000 is used in this example. The TCP listen port is not bound to a particular address and hence it is possible to connect on the TCP port on a link-local or on global address(es). Once the TCP connection is established, the server waits for requests from the client in the format specified in section TCP Request Format. When receiving a request, the response is sent in the TCP Response Format format.
In order to demonstrate data exchange on the TCP port, the application is designed to expect an 8-byte echo request in the following format from the TCP Client.
Field | Sequence number in uint32 format | 'P' | 'i' | 'n' | 'g' |
---|---|---|---|---|---|
Size (octets) | 4 (Network Order) | 1 | 1 | 1 | 1 |
In response to a request from the client, this application responds back to the request with a 8-byte response packet of following format:
Field | Requested Sequence number in uint32 format | 'P' | 'o' | 'n' | 'g' |
---|---|---|---|---|---|
Size (octets) | 4 (Network Order) | 1 | 1 | 1 | 1 |
The TCP client could be a PC application communicating to the UDP server on the kit as, shown in Figure 1 below.
Or, the UDP client could be the example client application included in the SDK sending requests to the server, as shown in Figure 2.
This section summarizes the usage of nRF5x resources and common modules in the examples apart from the IoT 6lowpan and lwIP stack library.
Module | Inclusion/Usage | Description |
---|---|---|
Timer | 1 | One timer is used to periodically service the lwIP, the interval is 200 ms. |
Button | 0 | No buttons are used in the example. |
LEDs | 4 | LEDs are used as an indicator of the application states. See LED assignments. |
Adv Data Encoder | Yes | The device name used is 'LwIPTCPServer', IPSP Service UUID is included in the UUID list. |
Scheduler | Yes | Scheduler is used for processing stack events. |
UART Trace | Included not enabled | Tracing is included but not enabled by default. |
The name of the example is iot_lwip_tcp_server. The source code and project file for this example can be found at: <InstallFolder>/examples/iot/tcp/server
See below for a short state diagram that describes the application states.
Application State | LED 1 State | LED 2 State | LED 3 State | LED 4 State |
---|---|---|---|---|
Idle | OFF | OFF | OFF | OFF |
Advertising | ON | OFF | OFF | OFF |
IPv6 Interface Up | OFF | ON | OFF | OFF |
IPv6 Interface Down | ON | OFF | OFF | OFF |
TCP Connected | OFF | OFF | ON | OFF |
TCP Ping | Refer to table below | Refer to table below | Refer to table below | Refer to table below |
ASSERT | ON | ON | ON | ON |
By using the LEDs on the board, the remainder after division of the received Sequence number by 16 is displayed in the following manner.
Value | Binary | LED 1 State | LED 2 State | LED 3 State | LED 4 State |
---|---|---|---|---|---|
0 | 00000000 | OFF | OFF | OFF | OFF |
1 | 00000001 | ON | OFF | OFF | OFF |
2 | 00000010 | OFF | ON | OFF | OFF |
3 | 00000011 | ON | ON | OFF | OFF |
4 | 00000100 | OFF | OFF | ON | OFF |
5 | 00000101 | ON | OFF | ON | OFF |
6 | 00000110 | OFF | ON | ON | OFF |
7 | 00000111 | ON | ON | ON | OFF |
8 | 00001000 | OFF | OFF | OFF | ON |
9 | 00001001 | ON | OFF | OFF | ON |
A | 00001010 | OFF | ON | OFF | ON |
B | 00001011 | ON | ON | OFF | ON |
C | 00001100 | OFF | OFF | ON | ON |
D | 00001101 | ON | OFF | ON | ON |
E | 00001110 | OFF | ON | ON | ON |
F | 00001111 | ON | ON | ON | ON |
See Connecting devices to the router for a list of relevant Linux commands.
Below is a Python client example that connects to the server application, sends 100 ping requests and then exits. The server address used here is an example address and will need to be modified based on the server address of the nRF5x that runs the lwIP TCP server application.
sysctl -w net.ipv6.conf.all.forwarding=1
.