![]() |
nRF5 SDK
v14.2.0
|
Choose documentation: | nRF5 SDK | S112 SoftDevice API | S132 SoftDevice API | S140 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API |
This example demonstrates how the lwIP stack can be used on Nordic's 6lowpan interface to send data on a remote TCP port. Remote port number 9000 is assumed in this example. Local port number 9001 is used to send requests and receive responses. Request and response formats used for this example are described in sections TCP Request Format and TCP Response Format, respectively.
To request a TCP connection to the remote server, push Button 1.
Once the connection is successfully established (LED 3 lights up), you can request periodic sending of requests by pushing Button 2. This button is also used to stop the data when in transfer state. Therefore, depending on the state, you can use Button 2 to both start and stop sending the data.
The TCP server could be a PC application communicating to the TCP client on the kit, as shown in Figure 1 below.
Or, the TCP server could be the example server application included in the SDK responding to requests from this example application in Figure 2.
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 to a request with an 8-byte response packet in the following format:
Field | Requested Sequence number in uint32 format | 'P' | 'o' | 'n' | 'g' |
---|---|---|---|---|---|
Size (octets) | 4 (Network Order) | 1 | 1 | 1 | 1 |
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 | 3 | Timer for lwIP, button module, and application timer to send data periodically. |
Button | 2 | Buttons are used to control the application. See the overview section and Button assignments. |
LEDs | 2 | LEDs are used to indicate the application states. See LED assignments. |
Adv Data Encoder | Yes | The device name used is 'lwIPTCPClient', IPSP Service UUID is included in the UUID list. |
Scheduler | Yes | Scheduler is used for processing stack events. |
You can find the source code and the project file of the example in the following folder: <InstallFolder>\examples\iot\tcp\client
See below for a 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 |
Button | Mapped Action |
---|---|
1 | TCP connection request |
2 | TCP data transfer start/stop |
See Connecting devices to the router for a list of relevant Linux commands.
Below is a python server example that listens on port 9000 and sends back responses for requests received on the port. Request and response structure are according to the format specified in sections TCP Request Format and TCP Response Format respectively.
sysctl -w net.ipv6.conf.all.forwarding=1
.