# The MIT License (MIT) # # Copyright (c) 2020 Scott Shawcroft for Adafruit Industries LLC # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ `adafruit_ble_broadcastnet` ================================================================================ Basic IOT over BLE advertisements. * Author(s): Scott Shawcroft """ import struct import os import time from micropython import const import adafruit_ble from adafruit_ble.advertising import Advertisement, LazyObjectField from adafruit_ble.advertising.standard import ManufacturerData, ManufacturerDataField __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE_BroadcastNet.git" _ble = adafruit_ble.BLERadio() # pylint: disable=invalid-name _sequence_number = 0 # pylint: disable=invalid-name def broadcast(measurement, *, broadcast_time=0.1, extended=False): """Broadcasts the given measurement for the given broadcast time. If extended is False and the measurement would be too long, it will be split into multiple measurements for transmission. """ global _sequence_number # pylint: disable=global-statement,invalid-name for submeasurement in measurement.split(252 if extended else 31): submeasurement.sequence_number = _sequence_number _ble.start_advertising(submeasurement, scan_response=None) time.sleep(broadcast_time) _ble.stop_advertising() _sequence_number = (_sequence_number + 1) % 256 # This line causes issues with Sphinx, so we won't run it in the CI if not hasattr(os, "environ") or ( "GITHUB_ACTION" not in os.environ and "READTHEDOCS" not in os.environ ): device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name *reversed( list( _ble._adapter.address.address_bytes # pylint: disable=protected-access ) ) ) """Device address as a string.""" _MANUFACTURING_DATA_ADT = const(0xFF) _ADAFRUIT_COMPANY_ID = const(0x0822) class AdafruitSensorMeasurement(Advertisement): """A collection of sensor measurements.""" # This prefix matches all match_prefixes = ( struct.pack("".format(self.__class__.__name__, " ".join(parts)) def split(self, max_packet_size=31): """Split the measurement into multiple measurements with the given max_packet_size. Yields each submeasurement.""" current_size = 8 # baseline for mfg data and sequence number if current_size + len(self.manufacturer_data) < max_packet_size: yield self return original_data = self.manufacturer_data.data submeasurement = None for key in original_data: value = original_data[key] entry_size = 2 + len(value) if not submeasurement or current_size + entry_size > max_packet_size: if submeasurement: yield submeasurement submeasurement = self.__class__() current_size = 8 submeasurement.manufacturer_data.data[key] = value current_size += entry_size if submeasurement: yield submeasurement return