Basic WebSocket Client Example

Use a websocket client to send sampled ADC and GPIO values to a remote server.

API Features Demonstrated

Platforms

This app works on:

Requirements and Prerequisites

This app requires a ZentriOS device with an ADC. For a device with no ADC, you can modify the code to substitute an appropriate data source.

Resources

The initialization file settings.ini is packaged into the .zap file downloaded to the device.

Description

This example app implements a websocket client that sends sampled ADC and GPIO values to the websocket.org echo server. The echoed text is received and displayed on the ZentriOS terminal.

Instructions for Use

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.

When the app runs, it displays output similar to the following:

> Attempting to connect to ws://echo.websocket.org
[Associating to Zentri_Guest]
> Obtaining IPv4 address via DHCP
IPv4 address: 192.168.6.113
[Associated]
> [Opening: echo.websocket.org:80]
> Resolving host: echo.websocket.org
Connecting (WS): 174.129.224.73:80
[2016-02-24 | 07:02:59: Opened: 0]
> Connected. Socket handle: 0
Network up
Echoed data: 000: gpios:35DA, adc: 1656mV
Echoed data: 001: gpios:F5DE, adc: 1656mV
Echoed data: 002: gpios:35DE, adc: 1655mV
Echoed data: 003: gpios:F5DE, adc: 1655mV
Echoed data: 004: gpios:35DE, adc: 1655mV
Echoed data: 005: gpios:F5DE, adc: 1655mV

Implementation

zn_app_init

zn_app_deinit

websocket_attempt_connect_handler

websocket_disconnect_handler

websocket_receive_handler

websocket_transmit_handler

wlan_network_event_handler

Source

See:

See also: