Basic UDP Client Example
Send GPIO data from the device to a UDP server, and receive echoed data from server.
- SDK directory:
apps/basic/udp_client
- Zentri App Store:
ZENTRI-BAUDPCLIENT
API Features Demonstrated
Platforms
This app works on:
- AMW004 - Zentri Mackerel evaluation board
- AMW106 - Zentri Moray evaluation board
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires a ZentriOS device.
Description
The app connects to the UDP echo server at test.zentri.com
and periodically sends GPIO values. The remote server echoes the values back, and the app displays the values 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.
The app displays output similar to the following:
> Connecting to remote UDP server test.zentri.com:50007
Connected!
Transmitting data every 5000ms
Rx data: gpios:1472610
Rx data: gpios:1433652
Rx data: gpios:1433610
...
Implementation
zn_app_init
:
- calls zn_network_register_event_handler to register the network event handler
wlan_network_event_handler
, to bring the network up if necessary. - calls zx_udp_connect to connect to the UDP server (the test.zentri.com echo server) and capture the stream handle.
- calls zn_udp_register_receive_event_handler to register the UDP stream receive event handler as
udp_receive_handler
- calls zn_event_register_periodic to set up a periodic transmit handler,
udp_transmit_handler
wlan_network_event_handler
:
- calls zx_network_up if the network is down
udp_receive_handler
:
- Calls zx_udp_read until failure or no more bytes can be read
- Logs to the console any messages echoed from the echo server.
udp_transmit_handler
:
- calls zn_gpio_mask_get with a mask of
0xFFFF
to retrieve all available GPIO values - formats the data into a string and puts the string into transmit buffer
- calls zx_udp_write to send the buffer contents to the echo server
zn_app_deinit
:
- calls zn_network_close_all_streams to clean up
Source
See: