This repository supports the Thingy:91 from Nordic Semiconductor and is intented to be used in conjunction with the front end and back end repositories found here.
The sample is developed based on the AWS IoT sample developed by Nordic Semiconductor, but extends the functionality by integrating sensors and persistent storage by integrating an FCB backend utilising the Zephyr Settings subsystem.
The firmware exclusively supports the Thingy:91 board and the board should be enclosed in a 3D printed dodecahedron as shown here for the orientation tracking to work.
To acces the project locally, you need to clone the project. This can be done by the command: git clone https://github.com/IT2901Nordic2024/firmware.git
Before flashing this sample you should follow the steps outlined in the setup and configuration section of the AWS IoT library. For the 5th step, the CONFIG_AWS_IOT_BROKER_HOST_NAME
, CONFIG_AWS_IOT_CLIENT_ID_STATIC
and CONFIG_MQTT_HELPER_SEC_TAG
should all be set in prj.conf before flashing the sample to your board.
By using the NRF connect vs-code extension: https://marketplace.visualstudio.com/items?itemName=nordic-semiconductor.nrf-connect
you can create a new build configuration.
These are the settings you have to select to ensure the build configuration is correct.
- Board: thingy91_nrf9160_ns
- Configuration: prj.conf
- Kconfig fragments: boards/thingy91_nrf9160_ns.config
- Devicetree overlay: boards/thingy91_nrf9160_ns.overlay
After building the firmware we recommend you use the Erase and flash to device
function when flashing a new build, especially if you have already run this firmware previously. This is because the Settings subsystem can create undefined behaviour when the storage is not erased before flashing.
The sample consists of two main parts, the AWS IoT communication handlers and the side orientation handlers. Most of the functionality lies within main.c where the accelerometer is polled every 100 milliseconds and each second the median of the last 10 accelerometer values is used to find which side is currently oriented upwards. If the current side has a habit stored it will load its type and either enable the counter or begin time tracking depending on what type of habit it is.
The settings_defs folder describe how habits are stored using the Settings subsystem. The ext_sensors folder is imported from Asset Tracker v2 and is currently not in use as we poll the accelerometer directly, but it makes it easier to implement new habit types if desired.
When using the sample, the device will first connect to the AWS IoT broker host and subscribe to the device's delta topic to find the desired state from the back end. If there is a delta it will then parse the delta message and save each side to its local configuration and respond to AWS with the reported changes to clear the delta state.
When a user orients the device with a habit side up it will then either enable the counter if the habit is of type COUNT or begin tracking time if the habit is of type TIME. When time tracking it will continue tracking the time until the device is reoriented at which point it will send the start and stop timestamp to AWS using Protocol Buffers.
When counting the user has to initiate a high G impact by smacking the device on a surface such as a table which increments the count. If the count has not been incremented further within 5 seconds the current count is sent using Protobuf and the counter is reset to 0. The counter system is enabled until the device is oriented to a new side.