Basic TCP Client Example
A TCP client that transmits sensor data to a remote TCP server
- SDK directory:
apps/basic/tcp_client
- Zentri App Store:
ZENTRI-BATCPCLIENT
API Features Demonstrated
- TCP/TLS Client/Server API
- Network Interface API
- Event Handling API
- Peripherals/ADC API
- Peripherals/GPIO API
Platforms
This app works on:
Requirements and Prerequisites
This app requires a ZentriOS device with a thermistor and ADC, such as an AMW106-E03 Moray evaluation board.
Resources
The initialization file settings.ini is packaged with the app and downloaded to the device.
Description
This app connects to the Zentri test echo server at test.zentri.com, and transmits GPIO data and ADC data from the thermistor. Data is echoed back to the app and printed on the ZentriOS terminal.
Usage Instructions
Open a ZentriOS serial terminal to the device. See Getting Started, Opening a ZentriOS Terminal.
This app assumes your ZentriOS device is set up with the credentials to join your local network. If you have not already set up network credentials, on the ZentriOS 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, adc: 1642mV
Rx data: gpios:35DA, adc: 1641mV
Rx data: gpios:35DE, adc: 1642mV
Implementation
In zn_app_init
:
zn_load_app_settings("settings.ini")
loads and runs the initialization file.- zn_adc_init
(PLATFORM_THERMISTOR)
initializes the ADC with the platform configuration for the thermistor. - zn_network_register_event_handler registers the function
wlan_network_event_handler
to handle network events.
In wlan_network_event_handler
:
- if the network is not up the handler issues the zn_network_up command to bring the network up.
- if the network is already up, the handler issues a
tcp_attempt_connect_handler
event to attempt connection to theTCP_HOST
at test.zentri.com.
The tcp_attempt_connect_handler
function, in response to the tcp_attempt_connect_handler
event:
- calls zn_tcp_connect to connect to
TCP_HOST
. - if the connection is unsuccessful, calls zn_event_register_timed to issue the
tcp_attempt_connect_handler
afterATTEMPT_CONNECT_PERIOD
milliseconds, and returns - if the connection is successful,
- calls zn_tcp_register_client_event_handlers to register the disconnect handler
tcp_disconnect_handler
, and the data received handlertcp_receive_handler
. - calls zn_event_register_periodic to set up a periodic
tcp_transmit_handler
event
- calls zn_tcp_register_client_event_handlers to register the disconnect handler
The tcp_disconnect_handler
function, in response to a TCP disconnection event:
- Attempts to display any remaining data with a call to
tcp_receive_handler
. - Cleans up by calling zn_tcp_disconnect on the connection handle.
- Sets up an attempt to reconnect, calling zn_event_issue with the
tcp_attempt_connection_handler
event.
The tcp_receive_handler
function, in response to a data received event:
- Calls zn_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 ZentriOS terminal console.
Note that the data received is echoed from the ZentriOS echo server: ADC data sent in response to the periodic tcp_transmit_handler
event.
The tcp_transmit_handler
function, in response to a periodic tcp_transmit_handler
event,
- calls zn_gpio_mask_get with a mask of
0xFFFF
to retrieve all available GPIO values - calls the zn_adc_sample function to retrieve thermistor data from the ADC
- formats the data into an output string
- calls zn_tcp_write to write the data to the TCP stream.
Source
See:
See also: