Basic WebSocket Client Example
Use a websocket client to send sampled ADC and GPIO values to a remote server.
- SDK directory:
apps/basic/websocket_client
- Zentri App Store:
ZENTRI-BAWEBSOCCLNT
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_network_register_event_handler sets up a handler that brings the network back up in the event of a network disconnection.
- zn_adc_init initializes the ADC
- zn_event_issue
(websocket_attempt_connect_handler, NULL, 0)
registers the attempt connection event, executes the associated handler, then unregisters it.
zn_app_deinit
- zn_network_close_all_streams cleans when the ZAP is shutting down
websocket_attempt_connect_handler
- Calls zn_websocket_connect to connect to the WebSocket echo server.
- If connecting to the WebSocket server fails, zn_event_register_timed sets up a timed event to try again after
ATTEMPT_CONNECT_PERIOD
. - zn_websocket_register_client_event_handlers sets up:
websocket_disconnect_handler
to run if the WebSocket disconnectswebsocket_receive_handler
to run when data is received on the WebSocket
- zn_event_register_periodic registers
websocket_transmit_handler
to run immediately, then everyTRANSMIT_PERIOD
milliseconds.
websocket_disconnect_handler
- Calls zn_event_unregister to turn off periodic transmission
- Calls
websocket_receive_handler
to read any remaining data - Calls zn_event_issue to run
websocket_attempt_connect_handler
and reinstate the connection
websocket_receive_handler
- Calls zn_websocket_read to read the data from the WebSocket echo server.
- logs the echoed data to the ZentriOS terminal console.
websocket_transmit_handler
- Calls zn_gpio_mask_get
(0xFFFF)
to read all GPIO values - Calls zn_adc_sample to read the ADC value in millivolts.
- Formats the data as a string
- Calls zn_websocket_write to send the data to the WebSocket echo server.
wlan_network_event_handler
- Calls zn_network_up to brings the network back up if it goes down
Source
See:
See also: