Follow the Sparrow Quickstart pair your Sparrow reference node with the gateway and associate it with the Notehub project you just created. Note also that you only need one reference node for this project, so you don't need to pair both nodes that came with the dev kit. Make sure that you use the ProductUID generated in Notehub Setup when it comes time to issue the hub.set command in the quickstart.
After you've completed the quickstart, leave the Notecarrier and Sparrow Essentials board powered and connected. These two devices will act as our gateway to Notehub, and you won't need to touch them again. The rest of this guide will focus on the Sparrow Reference Sensor node.
This solution makes use of the ENS160 sensor board, which measures indoor air quality. Connect the ENS160 sensor to the Sparrow Reference Sensor board using the Qwiic cable:
Insert one end of the Qwiic cable into the port labelled Qwiic on the Sparrow Reference Sensor board
Insert the other end of the Qwiic cable into either of the Qwiic sockets on the ENS160 sensor board.
With the Sparrow Reference Sensor powered on, you will see a red LED light up on the ENS160 board, indicating that it is receiving power. To improve battery life, you may wish to cut the LED jumper to disable the LED. For more details, see the "LED Jumper" section in the Sparkfun ENS160 Qwiic Hookup Guide.
This section shows how you build the Indoor Air Quality firmware and flash it to the reference node.
You will need to fetch the dependencies required by the firmware. After cloning this repository, run these commands: git submodule update --init 30-indoor-air-quality-and-gas-leak/firmware/note-c and git submodule update --init 30-indoor-air-quality-and-gas-leak/firmware/sparrow-lora. This will fetch the note-c and sparrow-lora submodules that this project depends on.
Connect the STLINK-V3MINI to your development PC with a USB A to micro USB cable.
Connect the STLINK to your reference node with the 2x7 JTAG ribbon cable.
There are a few ways to build and flash the firmware onto the reference node. These are covered in the Sparrow Builder's Guide. Follow the steps in that guide and then return to these instructions.
Note: When building the firmware with VSCode, be sure the firmware folder is added at the root of the workspace. This ensures the project's tasks to compile the firmware are recognized by VSCode.
Build and flash the firmware using whichever method you chose when following the Sparrow Builder's Guide.
Open a terminal emulator and connect to the STLINK's serial connection to view the logs from the device. See the documentation here.
Start the program in debug mode (again, how you do this depends on the IDE: VS Code or STM32CubeIDE). In your terminal emulator, you should see something like this:
The firmware periodically monitors air quality and gas concentrations, and posts events with this information. Regular air quality monitoring events are not synced immediately to Notehub, and will be delivered as often as the Notecard is configured to sync with Notehub via the hub.set request.
Air quality events are posted to the Notefile *#aqi.qo, where * is replaced with the unique ID of the Sparrow node. An air quality event has these properties:
"app" : "nf30", // the application name"aqi" : 1, // Air Quality Index (1-5)"eco2" : 439, // CO2 concentration in parts-per-million (ppm)"tvoc" : 41// Total volatile organic compounds (VOCs) parts-per-billion (ppb)}
Each air quality reading is checked against the configured thresholds. If any reading is higher than the corresponding threshold, the event is sent immediately to Notehub as an alert. Alerts are distinguished from regular monitoring events by the alert property.
"alert" : 1, // Signifies an alert"app" : "nf30", // the application name"aqi" : 3, // Air Quality Index (1-5)"eco2" : 639, // CO2 concentration in parts-per-million (ppm)"tvoc" : 241// Total volatile organic compounds (VOCs) parts-per-billion (ppb)}
The alert property has these values:
alert:1: Indicates an alert condition regarding air quality has been detected. The event is immediately synced to Notehub. You can use this to signal an alert externally, such as notifying a pager other messaging service.
alert:2: Signifies that the alert is still ongoing. Subsequent alerts after the initial alert have the alert property set to 2, indicating that the alert is ongoing because air quality hasn't returned to normal levels. These events are not immediately sent to notehub, and are provided for continuous monitoring of air quality.
alert:3: Signifies a stand-down alert and that air quality has returned to normal levels. These events are sent immediately to notehub. This is typically used to send an external notification that air quality has returned to normal levels.
When air quality returns to normal, alert:3 is the last event sent with the alert property present. Subsequent events monitoring air quality do not have the alert property present until a new alert condition is detected.
Notehub supports forwarding data to a wide range of API endpoints by using the Route feature. This can be used to forward your air quality data to external dashboards and alerts to a realtime notification service. Here, we will use Twilio SMS API to send a notification of an alert to a phone number.
Fill out the required fields for the Twilio SMS route, including "from" and "to" phone numbers, where "from" is your virtual Twilio number, and "to" is the number of the phone that receives the alerts. We will not be using placeholders for these numbers, but will use a placeholder for the message, so set the message field to [.body.customMessage].
Under the "Filters" section, set "Notefiles" to "All Notefiles" (the default value.)
Under "Data", select "JSONata Expression" and copy and paste the contents of jsonata/route.jsonata into the text field "Insert your JSONata expression here".
You can optionally create a more recognizable name for the air quality monitors in the project, which is used in SMS alerts. To do this, configure an environment variable that lists the Node IDs and their names:
In Notehub, click "Events" and inspect the *#aqi events. The event filename looks like <NodeID>#aqi.qo ) - the node's ID is the string of characters before the # sign. Make a note of the node ID, or copy it to be pasted later.
Click the Notecard Device corresponding to the gateway.
Click the "Environment" tab.
Add an environment variable with the name node_names. The value of the environment variable should follow the format NodeID:FriendlyName;NoteID2:FriendlyName2;, that is, the node ID followed by a colon (:) followed by the name and finally a semi-colon (;). For example setting node_names to 203733589236550300220032:Main Office;203733589236550300220033:Workshop names the node with ID ending 32 as "Main Office" and the node with ID ending 33 as "Workshop".
Once the node names are configured, the name rather than the node ID will be used in SMS alerts.
This is an alert event (due to the presence of the alert property), which will result in an SMS message being sent to the phone number in the "to" field. Before sending the event, replace 1234 in the file property with the unique ID of your Node (which you can find in the *#aqi events in Notehub.)