Skip to content

Instantly share code, notes, and snippets.

@mjvo
Last active March 15, 2024 12:10
Show Gist options
  • Save mjvo/f0b03b12cc6dbc3c5bd4d3593f92b972 to your computer and use it in GitHub Desktop.
Save mjvo/f0b03b12cc6dbc3c5bd4d3593f92b972 to your computer and use it in GitHub Desktop.
CircuitPython Code: Web Bluetooth in p5js with Adafruit Circuit Playground Bluefruit (UART)
# SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
# Web Bluetooth in p5js with Adafruit Circuit Playground Bluefruit (UART)
# https://editor.p5js.org/mjvo/sketches/vcoGZWBax
import board
import time
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)
while True:
# Advertise when not connected.
ble.start_advertising(advertisement)
while not ble.connected:
pass
ble.stop_advertising()
while ble.connected:
s = uart_service.readline()
if s:
try:
result = str(eval(s))
except Exception as e:
result = repr(e)
print(s)
result = "Msg from Bluefruit"
uart_service.write(result.encode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment