NXP NFC 3D App
Sends board info to a cloud server, and streams sensor data to a webapp connected via softAP. The text on the LCD display can be updated via the webapp or from the cloud server.
- SDK directory:
apps/nxp/nfc3d
- Zentri App Store:
ZENTRI-NXP3D
API Features Demonstrated
- Event Handling API
- JSON Utility
- Network Interface API
- Peripherals/GPIO API
- Protocols/HTTP Server API
- Protocols/Websocket Client/Server API
- Settings/Module Specific API
Platforms
This app works on:
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires an NXP-SCCK with NFC tag, and a smartphone/tablet with NFC support.
Resources
The file nfc3d.ini is used to initialize the device. This is packaged with the .zap file downloaded to the device.
Resources include the Zentri cloud certificate, and html and javascript for the web app and associated 3D model.
Description
This app demonstrates:
- reading an NFC tag and posting the information from the device to the cloud
- sending control information from the cloud to the device
- reading sensor information from the device and streaming the output to a web browser with a local connection to the device soft AP
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.
Load the app.
The ZentriOS terminal displays output similar to the following:
> Starting NFC 3D Demo ...
- Initializing cloud services
Connected to cloud
[Success]
- Initializing SoftAP and HTTP Server
Display text updated
[Success]
- Initializing NFC tag
[Success]
Demo ready.
Instructions:
1. Download NFC Tag reader app
- From Android phone: Google Play: search: NXP TagInfo
2. Start app and select the 'NDEF' tab
3. Place phone over NFC tag
4. Click provided link in app
5. Follow instructions on webpage
Note that you may be able to move directly to the cloud app in the browser, by simply opening a web browser and scanning the device NFC tag. This depends on your mobile device configuration.
Otherwise, using an Android mobile device, from Google Play download and install the NXP TagInfo
app.
When the NXP TagInfo
app successfully scans the NFC tag on the NXP Cloud Connectivity Kit (CCK), it displays output similar to the following:
Touch the link displayed in the URI record URI field to go to the Zentri cloud service for this app.
The first page of the cloud service shows device details that the nfc3d app sends periodically to the cloud service:
Scroll down to the second page for a control to update the text shown on the NXP CCK LCD display. Type in text and click update to send the new text to the device.
The page also provides instructions for viewing the 3D Demo web app. Follow the instructions to connect to the device Soft AP and view the web app served from the device.
The web app displays data streamed from the device. It uses the accelerometer sensor to show a representation of the orientation of the Zentri module plugged into the CCK. It also displays the temperature calculated from the device thermometer sensor:
When you connect to the soft AP, the ZentriOS terminal output is similar to the following:
[2016-01-04 | 03:08:48: 34:23:BA:F6:E0:FE associated]
> [2016-01-04 | 03:09:43: Opened: 1]
> Accelerometer update interval: 100
Stream listener registered, streaming data
Thermometer stream listener registered, streaming data
Thermometer stream listener unregistered
Stream listener unregistered
[2016-01-04 | 03:11:23: Closed: 1]
Move the NXP CCK around, and its position is mirrored in real time by the 3D model displayed in the web app.
Implementation
Manages initialization.
zn_app_init
- The call to zn_gpio_init is necessary when using the RF interface and the I2C bus together.
- zn_load_app_settings loads the settings from nfc3d.ini.
cloud_init
: cloud interface initialization. See cloud_websocket.clocal_init
: local device output and sensors initialization. See local.cnfc_tag_init
: NFC tag initialization. See nfc_tag.cgenerate_204_init
: 204 status page initialization. See generate_204.c
Manages communication via websocket between device and cloud service.
Sends JSON data to cloud. The data includes various device details.
Reads JSON data from cloud, containing LCD message updates.
Declares websocket stream handle websocket_handle
shared by several functions
cloud_init
- zn_network_register_event_handler registers
network_event_handler
to handle network disconnection events - calls the
cloud_connect_event_handler
function.
cloud_post_update
- zn_event_trigger triggers
periodic_post_event_handler
to send device details to cloud.
cloud_connect_event_handler
- defines a websocket configuration - a set of parameters among which are the host URL and the device cloud security certificate
- zn_event_unregister unregisters
cloud_connect_event_handler
as an event handler - sprintf constructs cloud service websocket URL, including the device UUID.
- zn_network_up brings the network up or logs a message if unable to do so.
- zn_websocket_connect_with_config makes the websocket connection to the cloud service URL and sets
websocket_handle
to the stream, or logs a message if unable to do so. - zn_websocket_register_client_event_handlers sets up the websocket disconnect event handler
websocket_disconnect_handler
and the receive event handlerwebsocket_rx_handler
- zn_event_register_periodic sets up
periodic_post_event_handler
to post the device details to the cloud service periodically - if unable to join the network, logs a message and calls zn_event_register_timed to trigger a
cloud_connect_event_handler
event after an interval
network_event_handler
- If the network is down, logs a message that the network is down and calls
attempt_reconnect
websocket_disconnect_handler
- logs a message and calls
attempt_reconnect
attempt_reconnect
- zn_websocket_disconnect ensures websocket is disconnected
- zn_event_unregister unregisters the
periodic_post_event_handler
function, to be set up again bycloud_connect_event_handler
- zn_event_issue registers the
cloud_connect_event_handler
to execute immediately then automatically unregister
websocket_rx_handler
Responds to a receive event with JSON data from the cloud service - parses the LCD message from the JSON
- Sets up a json_parse_config_t configuration, with
json_parse_reader
as the reader function - json_parse_context_init sets up JSON parsing
- json_parse_chunked calls
json_parse_reader
to get the JSON data into the json_parse_config_t buffer and parses the JSON - json_context_get_value gets the value of the
lcd_msg
token - if parsing successful, calls
local_set_lcd_msg
to set the message in the device LCD display. See local.c
json_parse_reader
Reads websocket stream JSON data from cloud into the JSON config buffer. The JSON data contains LCD message text.
- zn_websocket_poll sets
available
to indicate whether data is available for streamwebsocket_handle
. - If not available, returns.
- Sets up a zos_websocket_frame_t
- zn_websocket_client_read_frame reads from the stream into the frame
memcpy
copies the frame payload into the data parameter, which corresponds to the config buffer set up inwebsocket_rx_handler
- continues until no more data is available
periodic_post_event_handler
- Calls
write_setting
,write_lcd_message
andwrite_tag_read_count
to send device details, including ZentriOS variable values and other settings, via websocket to cloud app.
write_setting
- Given ZentriOS variable name, formats variable value and sends via websocket to cloud service.
write_tag_read_count
- Formats tag read count and sends via websocket to cloud service.
write_lcd_message
- Formats LCD message read from
nfc3d_context
structure and sends to cloud service.
write_ap_clients
- Formats list of Soft AP clients and sends via websocket stream to cloud service.
Provides functions for sensors, LCD display and Soft AP.
local_init
initialise_sensors
initializes sensorsinitialise_display
initializes LCD display- zn_hs_stream_register_callback registers callbacks for HTTP Server accelerometer stream, thermometer stream, and display stream,to be called when a remote client issues a request.
- zn_network_restart starts the HTTP server and softAP (specified in nfc3d.ini)
- zn_network_register_softap_event_handler registers callback
softap_event_handler
for Soft AP events.
local_set_lcd_msg
- Centers the supplied message on the device LCD display
softap_event_handler
- Calls
cloud_post_update
(seecloud_websocket.c
) to send device details to cloud in response to soft AP events.
initialise_display
- Initializes LCD display and sets initial message to "NFC 3D Demo!"
initialize_sensors
- Initializes accelerometer and thermometer sensors
accelerometer_stream_callback
Depending on the stream method:
HS_STREAM_LISTEN
: registersaccelerometer_read_event_handler
as a periodic eventHS_STREAM_UNLISTEN
: unregistersaccelerometer_read_event_handler
HS_STREAM_READ
: reads new update interval and updates the read event handler period
thermometer_stream_callback
Depending on the stream method:
HS_STREAM_LISTEN
: registersthermometer_read_event_handler
as a periodic eventHS_STREAM_UNLISTEN
: unregistersthermometer_read_event_handler
HS_STREAM_READ
: reads new update interval and updates the read event handler period
display_stream_callback
For stream method HS_STREAM_READ
:
- zn_hs_stream_read reads the data and calls
local_set_lcd_msg
to set LCD display with the value
nfc_tag_init
HTTP_SERVER_DYNAMIC_PAGE callback for /generate_204
page is generate_204_callback
generate_204_init
- Sets up
/generate_204
page with HTTP_SERVER_REGISTER_DYNAMIC_PAGES
generate_204_callback
- Returns
204
status code - no content
Source
See:
Settings:
See also:
- cloud_rest.c - Alternative cloud interface. Requires editing
nfc_nxp.mk
make file.