Basic TCP Client Example

A TCP client that transmits sensor data to a remote TCP server

API Features Demonstrated

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:

In wlan_network_event_handler:

The tcp_attempt_connect_handler function, in response to the tcp_attempt_connect_handler event:

The tcp_disconnect_handler function, in response to a TCP disconnection event:

The tcp_receive_handler function, in response to a data received event:

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,

Source

See:

See also: