In this tutorial, you’ll learn how to take sensor readings from a Device and send readings to your Notecard and the Blues Wireless Notehub. You'll use CircuitPython running on a Feather wired up to Notecarrier-AF hardware. If you would like to use a different language, board, or Notecarrier, modify the dropdowns at the top of this guide.
If you get stuck at any point during this tutorial, the full source for each example is available in the note-tutorials GitHub repo.
First, you’ll need to get all of your hardware connected. Follow the instructions below to connect your Feather, BME680 sensor, and Notecard mounted on a Notecarrier-AF.
In order to complete this guide, you’ll need the following:
- Notecard mounted to Notecarrier-AF.
Any CircuitPython-capable Microcontroller (MCU) with Feather headers. We’ll be using the Adafruit Feather M4 Express, but any MCU with a CircuitPython bootloader and binary will do. If using a different device, be sure to follow the instructions for burning the bootloader and flashing the CircuitPython binary to your device.
A text editor or IDE that works well with CircuitPython, like Mu or Visual Studio Code.
- Micro USB to USB-A cable.
- A Grove or STEMMA QT/Qwiic sensor of your choosing. We’ll be using the Adafruit BME680, but you’re welcome to use any sensor and adapt the code in this guide to read from your sensor instead.
- Plug your Adafruit BME680 into the STEMMA QT/Qwiic port on the Notecarrier-AF.
Now that your hardware is all connected, let’s create a new Notehub project to receive sensor readings from your Notecard.
Navigate to notehub.io and log-in, or create a new account.
Using the New Project card, give your project a name and ProductUID.
NOTE: The ProductUID must be globally unique, so we recommend a namespaced name like
"org.coca-cola.soda.vending-machine.v2"
.
Take note of your ProductUID. This identifier is used by Notehub to associate your Notecard to your project.
Now you’re ready to write some firmware. When communicating with the Notecard,
you can manually send requests using the Serial write
function and passing-in
JSON objects, or use the note-python library. The code snippets below show both,
so feel free to use the approach that works for you.
Install the Notecard Python Library
To use the
note-python
library, you’ll first need to download or clone it from the GitHub repo.Unzip the archive and copy the
notecard
directory into thelib
directory of yourCIRCUPTPY
mount.Add an
import
for the library at the top of yourcode.py
file.
import notecard
Set-up Your Notecard
- Add some additional imports to the top of your
code.py
file:
import board
import busio
import time
import json
- Add a definition for your ProductUID using the value you specified when creating your Notehub project.
productUID = "<com.your.product.uid>"
- Configure the I2C Bus and connection to your Notecard, and initialize the
connection to the Notecard using the
OpenI2C
function. Be sure to set thedebug
parameter toTrue
to see Notecard requests and responses.
port = busio.I2C(board.SCL, board.SDA)
card = notecard.OpenI2C(port, 0, 0, debug = True)
- Now, we’ll configure the Notecard. Using the
hub.set
request, we associate this Notecard with the ProductUID of your project and set the Notecard to operate incontinuous
mode, which indicates that the device should immediately make a connection to Notehub and keep it active.
req = {"req": "hub.set"}
req["product"] = productUID
req["mode"] = "continuous"
rsp = card.Transaction(req)
The lines above build-up a JSON object by adding two string values for product
and mode, and then fires the request off to the Notecard with the Transaction
function.
Save the
code.py
file to flash this code to your device.Using your IDE or tool of choice, open a Serial monitor to your CircuitPython device. If everything has been connected and configured properly, you’ll see a few debug messages, including the JSON object you sent, as well as the response from the Notecard
{}
.
Now that you’ve configured your MCU to communicate with the Notecard, let’s grab sensor readings from the BME680. First, you’ll want to grab a few libraries to use to interact with the device.
Navigate to the CircuitPython site and download the library bundle for the version of CircuitPython you’re using.
Unzip the archive.
Find the
adafuit_bme680.mpy
file andadafruit_bus_device
folders and copy both to thelib
directory of yourCIRCUITPY
mount. If thelib
directory doesn’t exist, you can create it.Now you’re reading to take readings from the BME680. Start by adding an
import
for the library to the top of yourcode.py
file
import adafruit_bme680
- Configure the I2C connection to your device and create a new instance of the sensor.
bmeSensor = adafruit_bme680.Adafruit_BME680_I2C(port)
- Now let's take a reading. Add a
while True
block to yourcode.py
if there’s not one already. Then take a temperature and humidity reading and print each to the console before sleeping for 15 seconds and repeating the process.
while True:
temp = bmeSensor.temperature
humidity = bmeSensor.humidity
print("\nTemperature: %0.1f C" % temp)
print("Humidity: %0.1f %%" % humidity)
time.sleep(15)
Now that we’re getting sensor readings, let’s send these to our Notecard.
- To send a sensor reading to the Notecard, we’ll need to construct a new JSON
request to the
note.add
API that includes a new Notefile name (sensors.qo
), sets thestart
field to true to instruct the Notecard to sync to Notehub immediately, and finally, sets thebody
to the sensor temperature and humidity. Add the following inwhile
loop right after theprint
commands to print out readings.
req = {"req": "note.add"}
req["file"] = "sensors.qo"
req["start"] = True
req["body"] = { "temp": temp, "humidity": humidity}
req = card.Transaction(req)
print(rsp)
Save this code to your device. After restart, the Serial monitor will update to display the response from the
note.add
request (the total number of Notes in the notefile) each time you add a new reading.
Once you start capturing readings, your Notecard will initiate a connection to Notehub and will start transferring Notes. Depending on signal strength and coverage in your area, it may take a few minutes for your Notecard to connect to Notehub and transfer data.
Return to notehub.io and open your project. You should see your notecard in the Devices view.
Now, click on the Events left menu item. Once your sensor Notes start syncing, they’ll show up here.
You’ve successfully connected your Feather and external sensor to your Notecard!
During this tutorial, you set your Notecard into continuous
mode, which
maintains an active cellular connection and enables faster syncs with Notehub.
It doesn't have much impact on data usage, but it will draw more power. If
you're connected to battery power, or want to transition your project to battery
power, then you can put your Notecard into periodic
mode with the following
request using the in-browser terminal or directly in your firmware.
{"req":"hub.set","mode":"periodic","outbound":60,"inbound":120}