Basic TCP Client Example
A TCP client that transmits sensor data to a remote TCP server
- SDK directory:
- Zentri App Store:
API Features Demonstrated
- TCP/TLS Client/Server API
- Network Interface API
- Event Handling API
- Peripherals/ADC API
- Peripherals/GPIO API
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.
The initialization file 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 and ADC data from the thermistor. Data is echoed back to the app and printed on the ZentriOS terminal.
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
zn_load_app_settings("settings.ini")loads and runs the initialization file.
(PLATFORM_THERMISTOR)initializes the ADC with the platform configuration for the thermistor.
- zn_network_register_event_handler registers the function
wlan_network_event_handlerto handle network events.
- 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_handlerevent to attempt connection to the
tcp_attempt_connect_handler function, in response to the
- calls zn_tcp_connect to connect to
- if the connection is unsuccessful, calls zn_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 zn_tcp_disconnect on the connection handle.
- Sets up an attempt to reconnect, calling zn_event_issue with 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 function, in response to a periodic
- calls zn_gpio_mask_get with a mask of
0xFFFFto 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.