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

First, set up CircuitPython on the CLUE following the instructions on this page.

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.mpy
  • adafruit_bus_device
  • adafruit_clue.mpy
  • adafruit_register
  • adafruit_lis3mdl.mpy
  • adafruit_lsm6ds.mpy
  • adafruit_sht31d.mpy
  • neopixel.mpy

Text Editor

Adafruit recommends using the Mu editor for using your CircuitPython code with the CLUE 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 CLUE's CIRCUITPY drive as code.py

"""This uses the CLUE as a Bluetooth LE sensor node."""

import time
from adafruit_clue import clue
import adafruit_ble_broadcastnet

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

while True:
    measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()

    measurement.temperature = clue.temperature
    measurement.pressure = clue.pressure
    measurement.relative_humidity = clue.humidity
    measurement.acceleration = clue.acceleration
    measurement.magnetic = clue.magnetic

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

How It Works

When this code runs on the CLUE board, it'll first import the time, clue, and adafruit_ble_broadcastnet libraries.

Then, it will print the unique ID for the board based on the board's BLE adapter's 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!
Download: file
import time
from adafruit_clue import clue
import adafruit_ble_broadcastnet

print("This is BroadcastNet CLUE 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 = clue.temperature

We then print the combined measurement to the screen and broadcast it.

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+)

Even if you only measure one data point, there will always also be an additional data point sent for the 'missed-message-count' value used as a type of error check. This counts toward the rate limit on AIO.
Download: file
while True:
    measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()

    measurement.temperature = clue.temperature
    measurement.pressure = clue.pressure
    measurement.relative_humidity = clue.humidity
    measurement.acceleration = clue.acceleration
    measurement.magnetic = clue.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!

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

This page (Prepare the CLUE) was last updated on Apr 07, 2021.

Text editor powered by tinymce.