/ examples / ble_bluefruit_connect_plotter.py
ble_bluefruit_connect_plotter.py
 1  # CircuitPython Bluefruit LE Connect Plotter Example
 2  
 3  import time
 4  import board
 5  import analogio
 6  import adafruit_thermistor
 7  from adafruit_ble import BLERadio
 8  from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
 9  from adafruit_ble.services.nordic import UARTService
10  
11  ble = BLERadio()
12  uart_server = UARTService()
13  advertisement = ProvideServicesAdvertisement(uart_server)
14  
15  thermistor = adafruit_thermistor.Thermistor(board.TEMPERATURE, 10000, 10000, 25, 3950)
16  light = analogio.AnalogIn(board.LIGHT)
17  
18  
19  def scale(value):
20      """Scale the light sensor values from 0-65535 (AnalogIn range)
21      to 0-50 (arbitrarily chosen to plot well with temperature)"""
22      return value / 65535 * 50
23  
24  
25  while True:
26      # Advertise when not connected.
27      ble.start_advertising(advertisement)
28      while not ble.connected:
29          pass
30      ble.stop_advertising()
31  
32      while ble.connected:
33          print((scale(light.value), thermistor.temperature))
34          uart_server.write("{},{}\n".format(scale(light.value), thermistor.temperature))
35          time.sleep(0.1)