Skip to content

Commit f110a81

Browse files
committed
Add battery voltage and more examples.
1 parent 4819a77 commit f110a81

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

adafruit_ble_broadcastnet.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ class AdafruitSensorMeasurement(Advertisement):
149149
weight = ManufacturerDataField(0x0a14, "<f")
150150
"""Weight as a float in grams."""
151151

152+
battery_voltage = ManufacturerDataField(0x0a15, "<H")
153+
"""Battery voltage in millivolts. Saves two bytes over voltage and is more readable in bare
154+
packets."""
155+
152156
def __init__(self, *, sequence_number=None):
153157
super().__init__()
154158
if sequence_number:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import adafruit_ble_broadcastnet
2+
import analogio
3+
import board
4+
import math
5+
import microcontroller
6+
import time
7+
8+
print("This is BroadcastNet sensor:", adafruit_ble_broadcastnet.device_address)
9+
10+
battery = analogio.AnalogIn(board.VOLTAGE_MONITOR)
11+
divider_ratio = 2
12+
13+
while True:
14+
measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()
15+
battery_voltage = battery.value / 2**16 * divider_ratio * battery.reference_voltage
16+
measurement.battery_voltage = int(battery_voltage * 1000)
17+
measurement.temperature = microcontroller.cpu.temperature
18+
print(measurement)
19+
adafruit_ble_broadcastnet.broadcast(measurement)
20+
21+
time.sleep(30)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""This example uses the internal temperature sensor and reports the battery voltage. However, it
2+
reads the temperature more often but only reports it when it's changed by a degree since the last
3+
report. When doing a report it will actually do multiple broadcasts and wait 2 ** n readings
4+
until the next broadcast. The delay is reset every time the temp moves more than 1 degree."""
5+
6+
import adafruit_ble_broadcastnet
7+
import analogio
8+
import board
9+
import math
10+
import microcontroller
11+
import time
12+
13+
print("This is BroadcastNet sensor:", adafruit_ble_broadcastnet.device_address)
14+
15+
battery = analogio.AnalogIn(board.VOLTAGE_MONITOR)
16+
divider_ratio = 2
17+
18+
last_temperature = None
19+
consecutive = 1
20+
while True:
21+
temp = microcontroller.cpu.temperature
22+
if not last_temperature or abs(temp - last_temperature) > 1:
23+
consecutive = 1
24+
last_temperature = temp
25+
else:
26+
consecutive += 1
27+
28+
# Repeatedly broadcast identical values to help ensure it is picked up by the bridge. Perform
29+
# exponential backoff as we get more confidence.
30+
exp = int(math.log(consecutive, 2))
31+
if 2 ** exp == consecutive:
32+
measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()
33+
battery_voltage = battery.value / 2**16 * divider_ratio * battery.reference_voltage
34+
measurement.battery_voltage = int(battery_voltage * 1000)
35+
measurement.temperature = temp
36+
print(measurement)
37+
adafruit_ble_broadcastnet.broadcast(measurement)
38+
39+
time.sleep(1)
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
"""This is a basic sensor node that uses the internal temperature sensor."""
1+
"""This is a basic sensor node that uses the internal temperature sensor and reports it every 10
2+
seconds."""
23

34
import adafruit_ble_broadcastnet
45
import microcontroller
56
import time
67

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

9-
last_temperature = None
1010
while True:
11-
temp = microcontroller.cpu.temperature
12-
# Round the temperature to the nearest degree to reduce how often it is
13-
# broadcast.
14-
temp = round(temp, 0)
15-
if temp != last_temperature:
16-
measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()
17-
measurement.temperature = temp
18-
print(measurement)
19-
adafruit_ble_broadcastnet.broadcast(measurement)
20-
last_temperature = temp
11+
measurement = adafruit_ble_broadcastnet.AdafruitSensorMeasurement()
12+
measurement.temperature = microcontroller.cpu.temperature
13+
print(measurement)
14+
adafruit_ble_broadcastnet.broadcast(measurement)
2115
time.sleep(10)

0 commit comments

Comments
 (0)