Skip to content

Instantly share code, notes, and snippets.

@dhalbert
Created August 30, 2024 23:10
Show Gist options
  • Save dhalbert/6227c52acaba1cd02e2fa2cfcc46610e to your computer and use it in GitHub Desktop.
Save dhalbert/6227c52acaba1cd02e2fa2cfcc46610e to your computer and use it in GitHub Desktop.
ESP32-S3 Adafruit BLE read/write Characteristic
import board
import os
import time
from adafruit_ble import Service
from adafruit_ble import BLERadio
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.int import Uint16Characteristic
from adafruit_ble.uuid import VendorUUID
class CountdownClockService(Service):
"""Countdown Clock Service"""
uuid = VendorUUID("F1B8ED4E-648B-4689-8A0C-4930BC3A2325")
countdownStart = Uint16Characteristic(
# max_value=9 * 60 + 59,
uuid=VendorUUID("955CB941-F229-4AAB-81FB-44349AFB6CA7"),
properties=Characteristic.WRITE | Characteristic.READ,
)
def __init__(self, countdownStart=10, service=None) -> None:
super().__init__(
manufacturer="Latency: Zero, LLC",
software_revision="1.0",
model_number="CC",
serial_number="12345",
firmware_revision="1.0",
hardware_revision="1.0",
service=service,
)
ble = BLERadio()
connection = None
while True:
if not connection:
print("Scanning...")
for adv in ble.start_scan(ProvideServicesAdvertisement):
if CountdownClockService in adv.services:
connection = ble.connect(adv)
print("Connected")
break
ble.stop_scan()
if connection and connection.connected:
service = connection[CountdownClockService]
while connection.connected:
print("adding 5 to countdownStart")
service.countdownStart = service.countdownStart + 5
print(f"{service.countdownStart=}")
time.sleep(0.2)
import board
import os
import time
from adafruit_ble import Service
from adafruit_ble import BLERadio
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.characteristics import Characteristic
from adafruit_ble.characteristics.int import Uint16Characteristic
from adafruit_ble.uuid import VendorUUID
class CountdownClockService(Service):
"""Countdown Clock Service"""
uuid = VendorUUID("F1B8ED4E-648B-4689-8A0C-4930BC3A2325")
countdownStart = Uint16Characteristic(
# max_value=9 * 60 + 59,
uuid=VendorUUID("955CB941-F229-4AAB-81FB-44349AFB6CA7"),
properties=Characteristic.WRITE | Characteristic.READ,
)
def __init__(self, countdownStart=10, service=None) -> None:
super().__init__(
manufacturer="Latency: Zero, LLC",
software_revision="1.0",
model_number="CC",
serial_number="12345",
firmware_revision="1.0",
hardware_revision="1.0",
service=service,
)
gBLE = BLERadio()
gBLE.name = "Countdown Clock BLE"
gService = CountdownClockService()
gAdv = ProvideServicesAdvertisement(gService)
gAdv.short_name = "ClockSvc"
def BLETask():
gBLE.stop_advertising()
while True:
gBLE.start_advertising(gAdv)
print("Advertising")
while not gBLE.connected:
time.sleep(0.100)
pass
print("Stopping Advertising")
gBLE.stop_advertising()
while gBLE.connected:
print("BLE connected")
cs = gService.countdownStart
if cs is not None:
print("decrementing countdownStart if > 0")
if cs > 0:
gService.countdownStart = gService.countdownStart - 1
print(f"countdown start is { cs }")
time.sleep(3)
while True:
BLETask()
print("main exited")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment