def __init__(self, use_debug=False): self.use_debug = use_debug self.subscription_ids = {} self.subscription_messages_on_reconnect = [] self.ble = BLERadio() self.uart_server = UARTService() self.advertisement = ProvideServicesAdvertisement(self.uart_server) self.chunk_size = 20 # adafruit_ble can only write in 20 byte chunks self.recv_msg_cache = ""
def __init__(self, onConnectionStateChanged, onAdvertising=None, onWrite=None): self._ble = BLERadio() self._uart_server = UARTService() self._onAdvertising = onAdvertising self._onWrite = onWrite self._advertisement = ProvideServicesAdvertisement(self._uart_server) self._isAdvertising = False self._onAdvertising = onAdvertising self._onWrite = onWrite self.__oldConnectionState = False self._onConnectionStateChanged = onConnectionStateChanged self._enabled = False
def __init__(self): # Set up watchdog timer self.watchdog = microcontroller.watchdog self.watchdog.deinit() self.watchdog.timeout = WATCHDOG_TIMEOUT self.watchdog.mode = WATCHDOG_MODE # Set up heartbeat output (i.e red LED) self._heartbeat = digitalio.DigitalInOut(HEARTBEAT_PIN) self._heartbeat.direction = digitalio.Direction.OUTPUT self._heartbeat_duration = HEARTBEAT_DURATION # Set up I2C bus i2c = busio.I2C(board.SCL, board.SDA) # Set up SPI bus spi = busio.SPI(board.SCK, board.MOSI, board.MISO) # Set up real time clock as source for time.time() or time.localtime() calls. print("Initialising real time clock.\n\n\n\n") clock = PCF8523(i2c) rtc.set_time_source(clock) print("Initialising display.\n\n\n\n") self.display = Display(self, DISPLAY_TIMEOUT, i2c, spi) print("Initialising lights.\n\n\n\n") self.lights = Lights(LIGHTS_ON_TIME, LIGHTS_OFF_TIME, LIGHTS_ENABLE_PIN, LIGHTS_DISABLE_PIN) print("Initialising feeder.\n\n\n\n") self.feeder = Feeder(FEEDING_TIMES, PORTIONS_PER_MEAL, FEEDER_MOTOR, FEEDER_STEPS_PER_ROTATION, FEEDER_STEP_DELAY, FEEDER_STEP_STYLE, i2c) print("Initialising temperature sensors.\n\n\n\n") ow_bus = OneWireBus(OW_PIN) self.water_sensor = TemperatureSensor(ow_bus, WATER_SN, WATER_OFFSET) self.air_sensor = TemperatureSensor(ow_bus, AIR_SN, AIR_OFFSET) # Set up SD card print("Setting up logging.\n\n\n\n") cs = digitalio.DigitalInOut(SD_CS) sdcard = SDCard(spi, cs) vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") self._log_data = LOG_DATA self._log_interval = time_tuple_to_secs(LOG_INTERVAL) self._last_log = None print("Initialising Bluetooth.\n\n\n\n") self._ble = BLERadio() self._ble._adapter.name = BLE_NAME self._ble_uart = UARTService() self._ble_ad = ProvideServicesAdvertisement(self._ble_uart)
def run(cycles_per_second=30, device_name=None): ble = BLERadio() if device_name: ble.name = device_name uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) expected_seconds_per_tick = 1 / cycles_per_second while True: print("waiting for bluetooth connection") ble.start_advertising(advertisement) while not ble.connected: _tick(expected_seconds_per_tick=expected_seconds_per_tick) print("connected to bluetooth") smartphone._set_uart(uart=uart) while ble.connected: _tick(expected_seconds_per_tick=expected_seconds_per_tick) print("lost bluetooth connection") smartphone._unset_uart()
def __init__(self, pin=board.D13, sample_num=20, use_ble=False): # Might not want to use BLE, perhaps for testing purposes self.use_ble = use_ble self.sample_num = sample_num try: self.pulses = pulseio.PulseIn(pin, self.sample_num) except ValueError as e: raise ValueError(e) self.pulses.pause() # The samples list holds the readings that will be used in calculating the distance. Pulse readings that # are determined to be way off (say 655355....) will not be included so len(samples) is <= sample_num. self.samples = [] if self.use_ble: # Set up BLE based on Adafruit's CircuitPython libraries. self.ble = BLERadio() uart_service = UARTService() advertisement = ProvideServicesAdvertisement(uart_service) # Advertise when not connected. self.ble.start_advertising(advertisement)
def connect(): """Connect two boards using the first Nordic UARTService the client finds over Bluetooth Low Energy. No timeouts, will wait forever.""" new_conn = None new_uart = None if master_device: # Master code while new_uart is None: d_print("Disconnected, scanning") for advertisement in ble.start_scan(ProvideServicesAdvertisement, timeout=BLESCAN_TIMEOUT): d_print(2, advertisement.address, advertisement.rssi, "dBm") if UARTService not in advertisement.services: continue d_print(1, "Connecting to", advertisement.address) ble.connect(advertisement) break for conns in ble.connections: if UARTService in conns: d_print("Found UARTService") new_conn = conns new_uart = conns[UARTService] break ble.stop_scan() else: # Slave code new_uart = UARTService() advertisement = ProvideServicesAdvertisement(new_uart) d_print("Advertising") ble.start_advertising(advertisement) # Is there a conn object somewhere here?? while not ble.connected: pass return (new_conn, new_uart)
#导入相关模块 import board, neopixel from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.color_packet import ColorPacket #构建蓝牙对象 ble = BLERadio() #定义广播名称 ble.name = '01Studio' #构建UART服务 uart_server = UARTService() #广播添加UART服务 advertisement = ProvideServicesAdvertisement(uart_server) #定义neopixel引脚,默认使用板载neopixel pixels = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.1) while True: # 广播 ble.start_advertising(advertisement) #等待连接 while not ble.connected: pass
""" import time import board import busio from adafruit_neotrellis.neotrellis import NeoTrellis from adafruit_neotrellis.multitrellis import MultiTrellis from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService from toy import Toy ble = BLERadio() uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) i2c = board.I2C() trelli = [ [ NeoTrellis(i2c, False, addr=0x2F), NeoTrellis(i2c, False, addr=0x2E), ], [ NeoTrellis(i2c, False, addr=0x31), NeoTrellis(i2c, False, addr=0x30), ], ] trellis = MultiTrellis(trelli)
# SPDX-License-Identifier: MIT # CircuitPython NeoPixel Color Picker Example import board import neopixel from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.color_packet import ColorPacket from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService ble = BLERadio() uart_service = UARTService() advertisement = ProvideServicesAdvertisement(uart_service) pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1) while True: # Advertise when not connected. ble.start_advertising(advertisement) while not ble.connected: pass ble.stop_advertising() while ble.connected: if uart_service.in_waiting: packet = Packet.from_stream(uart_service) if isinstance(packet, ColorPacket):
''' #导入相关模块 import time from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService #构建蓝牙对象 ble = BLERadio() #定义广播名称 ble.name='01Studio' #构建UART服务 Uart_Service = UARTService() #广播添加UART服务 advertisement = ProvideServicesAdvertisement(Uart_Service) while True: #发起广播 ble.start_advertising(advertisement) #等待连接 while not ble.connected: pass #连接蔡成功 while ble.connected:
from adafruit_bluefruit_connect.packet import Packet from jisforjt_cutebot_clue import cutebot, clue # Used to create random neopixel colors import random # Only the packet classes that are imported will be known to Packet. from adafruit_bluefruit_connect.button_packet import ButtonPacket ###################################################### # Variables ###################################################### ble = BLERadio() # Turn on Bluetooth uart_server = UARTService() # Turn on UART advertisement = ProvideServicesAdvertisement(uart_server) # Set up notice for other devices that Clue has a Bluetooth UART connection maxSpeed = 35 clue.sea_level_pressure = 1020 # Set sea level pressure for Clue's Altitude sensor. ###################################################### # Main Code ###################################################### while True: print("WAITING for BlueFruit device...") # Advertise when not connected. ble.start_advertising(advertisement) # Tell other devices that Clue has a Bluetooth UART connection. while not ble.connected: # Check to see if another device has connected with the Clue via Bluetooth.