Basic TCP Client Example
A TCP client that transmits sensor data to a remote TCP server
- SDK directory:
API Features Demonstrated
This app works on:
Requirements and Prerequisites
Requires a Gecko OS evaluation board, such as a WGM160P_eval.
Other Gecko OS hardware can also be used by changing the source of the logged data to suit your device.
The initialization file resources/settings.ini is packaged with the app and downloaded to the device.
This app connects to the Zentri test echo server at test.zentri.com, and transmits GPIO data. Data is echoed back to the app and printed on the Gecko OS terminal.
Open a Gecko OS serial terminal to the device. See Getting Started, Opening a Gecko OS Terminal.
This app assumes your Gecko OS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the Gecko OS terminal, run the commands:
network_up -s save
See the Wi-Fi Command API documentation for network_up and save.
No interaction is required. Output is similar to:
> Network is down - Restarting Network... Attempting to connect to test.zentri.com:50007 Connected. Socket handle: 0 Network down Network up Attempting to connect to test.zentri.com:50007 Connected. Socket handle: 1 Rx data: gpios:F5DA Rx data: gpios:35DA Rx data: gpios:35DE
gos_load_app_settings("settings.ini")loads and runs the initialization file.
- gos_network_register_event_handler registers the function
wlan_network_event_handlerto handle network events.
- if the network is not up the handler issues the gos_network_up command to bring the network up.
- if the network is already up, the handler issues a
tcp_attempt_connect_handlerevent to attempt connection to the
tcp_attempt_connect_handler function, in response to the
- calls gos_tcp_connect to connect to
- if the connection is unsuccessful, calls gos_event_register_timed to issue the
ATTEMPT_CONNECT_PERIODmilliseconds, and returns
- if the connection is successful,
tcp_disconnect_handler function, in response to a TCP disconnection event:
- Attempts to display any remaining data with a call to
- Cleans up by calling gos_tcp_disconnect on the connection handle.
- Sets up an attempt to reconnect, calling gos_event_issue with the
tcp_receive_handler function, in response to a data received event:
- Calls gos_tcp_read to read the data from the TCP stream handle until failure or no more data is available.
- Logs the data read to the Gecko OS terminal console.
Note that the data received is echoed from the Gecko OS echo server.
tcp_transmit_handler function, in response to a periodic
- calls gos_gpio_mask_get with a mask of
0xFFFFto retrieve all available GPIO values
- formats the data into an output string
- calls gos_tcp_write to write the data to the TCP stream.