Accelerometer with IRQ Handler
Display accelerometer output on web page served by ZentriOS device.
- SDK directory:
- Zentri App Store:
API Features Demonstrated
- Event Handling API
- JSON Utility
- Sensor Library
- Peripherals/GPIO API
- Protocols/HTTP Server API
- Settings API
This app works on:
- AMW004 - Zentri Mackerel evaluation board - additional hardware required
- AMW106 - Zentri Moray evaluation board - additional hardware required
- NXP-SCCK - NXP Secure Cloud Connectivity Kit - additional hardware required
Requirements and Prerequisites
This app requires a ZentriOS device, such as an AMWx06-E03 (Moray), connected via the I2C interface to an MPU-9250 accelerometer sensor from Invensense. See Connecting a Zentri AMW106 to an InvenSense MPU-9250.
The file settings.ini is used to initialize the device.
The file accelerometer.html is served by the device HTTP Server to display a graph of accelerometer output.
This app uses an interrupt to update accelerometer status without polling. A web page displays accelerometer data, received via WebSocket from the server in real time.
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.
On downloading and running the app, the terminal output is similar to:
> Starting Accelerometer App Interrupt Pin - GPIO9 To query current sensor readings: http://accelerometer.local/get_readings To view a realtime demonstration: http://accelerometer.local to view accelerometer data Initialisation successful!
When you move the accelerometer beyond the threshold acceleration, the output is similar to:
Checking interrupt status.... Getting data.... X: 206 mG Y: 1153 mG Z: 1582 mG Response written to listener (0x80000000)
To view the web page, connect a web browser to the URL reported on the ZentriOS terminal console when the app initialises. ZentriOS device and browser device must be connected to the same network.
The ZentriOS terminal output indicates a client connection, e.g.
[2015-12-09 | 05:01:50: Opened: 0] > Stream listener (0x80000000) registered
MAX_NUM_CLIENTS (default 8, ZentriOS HTTP server stream maximum) clients can connect to the HTTP server and view the web page.
The web page updates only when there is an interrupt indicating data is available. You have to move the accelerometer above the threshold acceleration to make the web page display scroll and display new data.
The call to
zn_load_app_settings loads and runs settings.ini. A separate call to zn_settings_get_str sets up the device's MDNS address.
The call to zn_hs_register_dynamic_page registers a callback for the dynamic page, served by the ZentriOS HTTP server from the device.
The call to zn_hs_stream_register_callback registers a callback to be called in response to a request from a request from a HTTP server client.
An interrupt GPIO is registered to notify when accelerometer data is available to be read.
This cleans up before exiting. It disables the GPIO IRQ, and unregisters the HTTP stream callback.
This returns true so that the event loop sleeps until an IRQ from the accelerometer GPIO.
To free the IRQ thread for further interrupts, the IRQ handler immediately passes execution from the IRQ thread context to the ZAP thread context, by issuing the
The ZAP context event handler calls
read_accel_data() to read data from the accelerometer sensor.
This is called from
The call to sensor_has_new_data ensures that new data is available from the accelerometer.
The call to sensor_get_data reads data from the accelerometer.
It logs the data on the ZentriOS terminal, then encodes the data as json. It then writes the data to each of the registered listeners.
This is called in response to request for the /get_readings URL on the device HTTP server.
This function registers stream listeners in response to a WebSocket message for stream
accelerometer with method
It removes stream listeners in response to an
unlisten method message.
Start button. Clicking
Stop closes the WebSocket. Opening the WebSocket registers the client in the app
ws.onmessage function responds to receiving a WebSocket message from the server, by plotting the x,y and z data received.