We'll set up the Feather Sense board to act as a sensor for the BroadcastNet.

First, set up CircuitPython on the Feather Sense following the instructions on this page. NOTE: This board is so new, the instructions are actually for the regular nRF52840 Feather, so when you go to the CircuitPython.org download page, be sure to use the download for the Sense board instead!

Libraries

Next, install the libraries needed. This guide page will show you where to download them.

You'll need the following libraries for this project:

  • adafruit_apds9960
  • adafruit_ble
  • adafruit_ble_broadcastnet
  • adafruit_bmp280
  • adafruit_bus_device
  • adafruit_register
  • adafruit_lis3mdl
  • adafruit_lsm6ds
  • adafruit_sht31d
  • neopixel.mpy

Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the Feather Sense board. You can get more info in this guide.

Alternatively, you can use any text editor that saves files.

Code.py

Copy the code shown below, paste it into Mu. Save the code from Mu to the Feather's CIRCUITPY drive as code.py

# SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""This uses the Feather Sense as a Bluetooth LE sensor node."""

import time
import adafruit_ble_broadcastnet
import board
import adafruit_lsm6ds   # accelerometer
import adafruit_sht31d   # humidity sensor
import adafruit_bmp280   # barometric sensor
import adafruit_lis3mdl  # magnetic sensor

i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller

sense_accel = adafruit_lsm6ds.LSM6DS33(i2c)
sense_humid = adafruit_sht31d.SHT31D(i2c)
sense_barometric = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
sense_magnet = adafruit_lis3mdl.LIS3MDL(i2c)

print("This is BroadcastNet Feather Sense sensor:", adafruit_ble_broadcastnet.device_address)

while True:
    measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()

    measurement.temperature = sense_barometric.temperature
    measurement.pressure = sense_barometric.pressure
    measurement.relative_humidity = sense_humid.relative_humidity
    measurement.acceleration = sense_accel.acceleration
    measurement.magnetic = sense_magnet.magnetic

    # print(measurement)
    adafruit_ble_broadcastnet.broadcast(measurement)
    time.sleep(60)

How It Works

When this code runs on the Feather Sense board, it'll first import the time, board, and adafruit_ble_broadcastnet libraries, along with the library for each sensor in the board that we're using.

Next, I2C is instantiated on the board for communications with the sensors.

We create an object (with a nice name!) for each sensor as well, and instruct them to use I2C.

Then, it will print the unique ID for the board based on the board's BLE adapter MAC address. This address will be used when messages are sent, so there won't be any clashes when multiple microcontroller boards are in range of the BroadcastNet base station.

NOTE: this is not a secure connection, so think of this system as "broadcasting" in the truest sense of the word!
import time
import adafruit_ble_broadcastnet
import board
import adafruit_lsm6ds   # accelerometer
import adafruit_sht31d   # humidity sensor
import adafruit_bmp280   # barometric sensor
import adafruit_lis3mdl  # magnetic sensor

i2c = board.I2C()

sense_accel = adafruit_lsm6ds.LSM6DS33(i2c)
sense_humid = adafruit_sht31d.SHT31D(i2c)
sense_barometric = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
sense_magnet = adafruit_lis3mdl.LIS3MDL(i2c)

print("This is BroadcastNet Feather Sense sensor:", adafruit_ble_broadcastnet.device_address)

Measurements

Next, we will have the main loop of the program. In it, the board sends out an advertisement to alert the base station central device that it is there.

Next we collect the different sensor readings such as measurement.temperature = sense_barometric.temperature (we're using the temperature sensor that is built onto the barometric pressure sensor package).

We then broadcast the combined measurement over BLE.

Finally, the code will sleep for a certain amount of time. Depending on your needs, this can be as frequent as every two seconds, if sending a single data point to Adafruit IO* (or every one second to AIO+) to many minutes or hours between sensor measurement broadcasts. In this case, it is pausing for 60 seconds between broadcasts.

 

*The data limit on Adafruit IO is 30 data points per minute and 60/min on Adafruit IO+)

measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()

    measurement.temperature = sense_barometric.temperature
    measurement.pressure = sense_barometric.pressure
    measurement.relative_humidity = sense_humid.relative_humidity
    measurement.acceleration = sense_accel.acceleration
    measurement.magnetic = sense_magnet.magnetic

    # print(measurement)
    adafruit_ble_broadcastnet.broadcast(measurement)
    time.sleep(60)

Once we set up the Raspberry Pi to act as a BroadcastNet base station, the CLUE sensor data will be able to make its way all the way to Adafruit IO!

Other Boards

If you choose to use a different Adafruit nRF52840 board and external sensors, the setup will be very nearly the same. Check the guide for the board and sensors for the particulars.

This guide was first published on Mar 07, 2020. It was last updated on Mar 29, 2024.

This page (Prepare the Feather Sense) was last updated on Mar 29, 2024.

Text editor powered by tinymce.