Skip to content

Instantly share code, notes, and snippets.

@gbit-is
Created December 25, 2023 12:11
Show Gist options
  • Save gbit-is/a1c16c9dd50aad4835ee281e2a929703 to your computer and use it in GitHub Desktop.
Save gbit-is/a1c16c9dd50aad4835ee281e2a929703 to your computer and use it in GitHub Desktop.
duplo bluetooth attempt
import uasyncio as asyncio
import aioble
import bluetooth
from micropython import const
import time
import struct
print("All aboard")
PRIMARY = bluetooth.UUID("00001623-1212-efde-1623-785feabcd123")
CHAR = bluetooth.UUID("00001624-1212-efde-1623-785feabcd123")
# 00001624-1212-efde-1623-785feabcd123
async def find_train():
device_list = [ ]
async with aioble.scan(5000, interval_us=30000, window_us=30000, active=True) as scanner:
async for result in scanner:
name = result.name()
if name:
if "Train" in name:
print("choo choo Found")
return result.device
return None
async def main():
device = await find_train()
if not device:
print("Unable to connect to train :(")
return
try:
print("Connecting to", device)
connection = await device.connect()
except asyncio.TimeoutError:
print("Timeout during connection")
return
print("Choo Choo connected")
time.sleep(1)
async with connection:
not_connected = True
while not_connected:
try:
foo_service = await connection.service(PRIMARY)
time.sleep(1)
foo_characteristic = await foo_service.characteristic(CHAR)
print("Service ... serviced")
not_connected = False
except Exception as e:
print("service .... not serviced")
time.sleep(1)
time.sleep(2)
retry = 10
c = 0
#time.sleep(6)
# sound 129, 1, 17, 81, 1, 9
c1 = [ 129,0,0,81,0,90]
c2 = [ 129, 0, 17, 81, 0, 100 ]
comm = [ 0x81, 0x00, 0x11,0x51, 0x00,0x50 ]
asdf = comm
#foo_characteristic.connection.l2cap_connect()
print("..................................")
a = foo_characteristic.connection
for i in dir(a):
print(i)
print("===================================")
print(foo_characteristic.connection.pair())
foo_characteristic.write(comm)
time.sleep(1)
print("C")
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment