UDP Client

This application uses the Gecko OS UDP client command to send UDP messages to a UDP echo server on the internet.

The UDP echo server, located on the internet at test.zentri.com:50007 , simply echoes all received data back to the Gecko OS UDP client. Once the connection is established, stream_write is used to send data to the connection stream. With all data sent, stream_poll is used to check for received data on the connection stream, and stream_read is used to read data waiting on the stream.

You will only need one Silabs evaluation board for this example.


Connecting

Let's get started by connecting your board to a Wi-Fi network and opening the UDP connection stream. You can simply cut and paste the commands in the following table, but DON'T FORGET to replace your Wi-Fi network name and password for the defaults.

Gecko OS Commands Description

set wlan.ssid    YOUR_AP_NAME
set wlan.passkey YOUR_AP_PASSWORD
udp_client       test.zentri.com 50007

<- Set the name of your Wi-Fi AP
<- Set the password for your Wi-Fi AP
<- Open UDP client session

There is no need to bring up the WLAN network interface prior to making the UDP client connection. The UDP client (just like all commands that require network access) automatically connects to the network if a connection has not already been established.

The response is similar to the session shown below. The new UDP connection is assigned a stream handle, starting at 0 for the first UDP connection. Note that "connection" is used loosely here: UDP is a stateless protocol. However Gecko OS associates details of the connection with a stream, preserving some state.

Session log when opening the UDP client connection

> udpc test.zentri.com 50007
[Associating to Silabs]
Security type from probe: WPA2-AES
Obtaining IPv4 address via DHCP
IPv4 address: 10.5.6.108
[Associated]
[Opening: test.zentri.com:50007]
Resolving host: test.zentri.com
Connecting: 107.170.222.80:50007
[2015-03-31 | 06:48:15: Opened: 0]
0

Check the status of open streams before continuing. Use the stream_list command to get a list of open streams. The number in brackets appended to each line is the local port used for the UDP connection stream.

> stream_list
! # Type  Info
# 0 UDPC  test.zentri.com:50007 (30001)

Writing Data

Writing data to a stream is easy using the stream_write command. Copy and paste the following commands to write data to the open stream.

Gecko OS Commands Description

> stream_write 0 19
Hello from stream 0

<- Prepare to write 19 characters to UDP stream 0
<- Data to write

Checking for Data

To check for data on any stream (or all streams at once), use the stream_poll command as shown in the following session. If you don't want to poll for data, try assigning a GPIO to a stream to indicate when data is available. The udp_client command describes how to assign a GPIO.

Checking for data

> stream_poll 0
1
> stream_poll all
0,1

Reading Data

To read available data from any stream, use the stream_read command as shown in the following session. The following example reads up to 100 bytes from the stream.

Checking for data

> stream_read 0 100
Hello from stream 0

Supporting Gecko OS Editions and Versions

Change Log

Modified Changes
2015-Mar-31 Created from TCP client app note