code.py
 1  # SPDX-FileCopyrightText: 2021 Brent Rubell for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  import board
 6  from adafruit_ble import BLERadio
 7  from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
 8  from adafruit_ble.services.nordic import UARTService
 9  from adafruit_airlift.esp32 import ESP32
10  
11  # If you are using an AirLift FeatherWing or AirLift Bitsy Add-On,
12  # use the pin settings below:
13  esp32 = ESP32(
14      reset=board.D12,
15      gpio0=board.D10,
16      busy=board.D11,
17      chip_select=board.D13,
18      tx=board.TX,
19      rx=board.RX,
20  )
21  
22  # If you are using an AirLift Breakout, comment out the DEFAULT lines
23  # above and uncomment the lines below:
24  # esp32 = ESP32(
25  #    reset=board.GP16,
26  #    gpio0=board.GP9,
27  #    busy=board.GP14,
28  #    chip_select=board.GP13,
29  #    tx=board.GP0,
30  #    rx=board.GP1,
31  # )
32  
33  
34  adapter = esp32.start_bluetooth()
35  
36  ble = BLERadio(adapter)
37  uart = UARTService()
38  advertisement = ProvideServicesAdvertisement(uart)
39  
40  while True:
41      ble.start_advertising(advertisement)
42      print("waiting to connect")
43      while not ble.connected:
44          pass
45      print("connected: trying to read input")
46      while ble.connected:
47          # Returns b'' if nothing was read.
48          one_byte = uart.read(1)
49          if one_byte:
50              print(one_byte)
51              uart.write(one_byte)